Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 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 | |
| 12 | namespace DBus { |
| 13 | class Error; |
| 14 | } // namespace DBus |
| 15 | |
| 16 | namespace shill { |
| 17 | |
| 18 | class Error { |
| 19 | public: |
| 20 | enum Type { |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 21 | kSuccess = 0, // No error. |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 22 | kOperationFailed, // failure, otherwise unspecified |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 23 | kAlreadyConnected, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 24 | kAlreadyExists, |
| 25 | kInProgress, |
| 26 | kInternalError, |
| 27 | kInvalidArguments, |
| 28 | kInvalidNetworkName, |
| 29 | kInvalidPassphrase, |
| 30 | kInvalidProperty, |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 31 | kNoCarrier, |
| 32 | kNotConnected, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 33 | kNotFound, |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 34 | kNotImplemented, |
| 35 | kNotOnHomeNetwork, |
| 36 | kNotRegistered, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 37 | kNotSupported, |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 38 | kOperationAborted, |
| 39 | kOperationTimeout, |
| 40 | kPassphraseRequired, |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 41 | kIncorrectPin, |
| 42 | kPinRequired, |
| 43 | kPinBlocked, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 44 | kPermissionDenied, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 45 | kNumErrors |
| 46 | }; |
| 47 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 48 | Error(); // Success by default. |
| 49 | explicit Error(Type type); // Uses the default message for |type|. |
| 50 | Error(Type type, const std::string &message); |
| 51 | ~Error(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 52 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 53 | void Populate(Type type); // Uses the default message for |type|. |
| 54 | void Populate(Type type, const std::string &message); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 55 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 56 | void Reset(); |
| 57 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 58 | void CopyFrom(const Error &error); |
| 59 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 60 | // Sets the DBus |error| and returns true if Error represents failure. |
| 61 | // Leaves |error| unchanged, and returns false, otherwise. |
| 62 | bool ToDBusError(::DBus::Error *error) const; |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 63 | |
| 64 | Type type() const { return type_; } |
| 65 | const std::string &message() const { return message_; } |
| 66 | |
| 67 | bool IsSuccess() const { return type_ == kSuccess; } |
| 68 | bool IsFailure() const { return !IsSuccess(); } |
| 69 | |
| 70 | static std::string GetName(Type type); |
| 71 | static std::string GetDefaultMessage(Type type); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 72 | |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 73 | // Log an error message. If |error| is non-NULL, also populate it. |
| 74 | static void PopulateAndLog(Error *error, Type type, |
| 75 | const std::string &message); |
| 76 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 77 | private: |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 78 | struct Info { |
| 79 | const char *name; // Error type name. |
| 80 | const char *message; // Default Error type message. |
| 81 | }; |
| 82 | |
| 83 | static const Info kInfos[kNumErrors]; |
| 84 | static const char kInterfaceName[]; |
| 85 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 86 | Type type_; |
| 87 | std::string message_; |
| 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(Error); |
| 90 | }; |
| 91 | |
| 92 | } // namespace shill |
| 93 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 94 | // stream operator provided to facilitate logging |
| 95 | std::ostream &operator<<(std::ostream &stream, const shill::Error &error); |
| 96 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 97 | #endif // SHILL_ERROR_ |