blob: 3c01047dea63c9fd5f482ed2cdc9929935530953 [file] [log] [blame]
Chris Masone8fe2c7e2011-06-09 15:51:19 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_ERROR_
6#define SHILL_ERROR_
7
8#include <string>
9
10#include <base/basictypes.h>
11
12namespace DBus {
13class Error;
14} // namespace DBus
15
16namespace shill {
17
18class Error {
19 public:
20 enum Type {
Darin Petkove4c0ace2011-08-24 10:32:46 -070021 kSuccess = 0, // No error.
22 kAlreadyConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070023 kAlreadyExists,
24 kInProgress,
25 kInternalError,
26 kInvalidArguments,
27 kInvalidNetworkName,
28 kInvalidPassphrase,
29 kInvalidProperty,
Darin Petkove4c0ace2011-08-24 10:32:46 -070030 kNoCarrier,
31 kNotConnected,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070032 kNotFound,
Darin Petkove4c0ace2011-08-24 10:32:46 -070033 kNotImplemented,
34 kNotOnHomeNetwork,
35 kNotRegistered,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070036 kNotSupported,
Darin Petkove4c0ace2011-08-24 10:32:46 -070037 kOperationAborted,
38 kOperationTimeout,
39 kPassphraseRequired,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070040 kPermissionDenied,
Chris Masone8fe2c7e2011-06-09 15:51:19 -070041 kNumErrors
42 };
43
Darin Petkove4c0ace2011-08-24 10:32:46 -070044 Error(); // Success by default.
45 explicit Error(Type type); // Uses the default message for |type|.
46 Error(Type type, const std::string &message);
47 ~Error();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070048
Darin Petkove4c0ace2011-08-24 10:32:46 -070049 void Populate(Type type); // Uses the default message for |type|.
50 void Populate(Type type, const std::string &message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070051
Gaurav Shah1b7a6162011-11-09 11:41:01 -080052 void Reset();
53
Darin Petkove5bc2cb2011-12-07 14:47:32 +010054 void CopyFrom(const Error &error);
55
mukesh agrawal7a4e4002011-09-06 11:26:05 -070056 // Sets the DBus |error| and returns true if Error represents failure.
57 // Leaves |error| unchanged, and returns false, otherwise.
58 bool ToDBusError(::DBus::Error *error) const;
Darin Petkove4c0ace2011-08-24 10:32:46 -070059
60 Type type() const { return type_; }
61 const std::string &message() const { return message_; }
62
63 bool IsSuccess() const { return type_ == kSuccess; }
64 bool IsFailure() const { return !IsSuccess(); }
65
66 static std::string GetName(Type type);
67 static std::string GetDefaultMessage(Type type);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070068
Paul Stewartbe005172011-11-02 18:10:29 -070069 // Log an error message. If |error| is non-NULL, also populate it.
70 static void PopulateAndLog(Error *error, Type type,
71 const std::string &message);
72
Chris Masone8fe2c7e2011-06-09 15:51:19 -070073 private:
Darin Petkove4c0ace2011-08-24 10:32:46 -070074 struct Info {
75 const char *name; // Error type name.
76 const char *message; // Default Error type message.
77 };
78
79 static const Info kInfos[kNumErrors];
80 static const char kInterfaceName[];
81
Chris Masone8fe2c7e2011-06-09 15:51:19 -070082 Type type_;
83 std::string message_;
84
85 DISALLOW_COPY_AND_ASSIGN(Error);
86};
87
88} // namespace shill
89
90#endif // SHILL_ERROR_