Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 1 | // 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 | |
| 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. |
| 22 | kAlreadyConnected, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 23 | kAlreadyExists, |
| 24 | kInProgress, |
| 25 | kInternalError, |
| 26 | kInvalidArguments, |
| 27 | kInvalidNetworkName, |
| 28 | kInvalidPassphrase, |
| 29 | kInvalidProperty, |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 30 | kNoCarrier, |
| 31 | kNotConnected, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 32 | kNotFound, |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 33 | kNotImplemented, |
| 34 | kNotOnHomeNetwork, |
| 35 | kNotRegistered, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 36 | kNotSupported, |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 37 | kOperationAborted, |
| 38 | kOperationTimeout, |
| 39 | kPassphraseRequired, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 40 | kPermissionDenied, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 41 | kNumErrors |
| 42 | }; |
| 43 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 44 | 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 Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 48 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 49 | void Populate(Type type); // Uses the default message for |type|. |
| 50 | void Populate(Type type, const std::string &message); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 51 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 52 | void Reset(); |
| 53 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 54 | // Sets the DBus |error| and returns true if Error represents failure. |
| 55 | // Leaves |error| unchanged, and returns false, otherwise. |
| 56 | bool ToDBusError(::DBus::Error *error) const; |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 57 | |
| 58 | Type type() const { return type_; } |
| 59 | const std::string &message() const { return message_; } |
| 60 | |
| 61 | bool IsSuccess() const { return type_ == kSuccess; } |
| 62 | bool IsFailure() const { return !IsSuccess(); } |
| 63 | |
| 64 | static std::string GetName(Type type); |
| 65 | static std::string GetDefaultMessage(Type type); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 66 | |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 67 | // Log an error message. If |error| is non-NULL, also populate it. |
| 68 | static void PopulateAndLog(Error *error, Type type, |
| 69 | const std::string &message); |
| 70 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 71 | private: |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 72 | struct Info { |
| 73 | const char *name; // Error type name. |
| 74 | const char *message; // Default Error type message. |
| 75 | }; |
| 76 | |
| 77 | static const Info kInfos[kNumErrors]; |
| 78 | static const char kInterfaceName[]; |
| 79 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 80 | Type type_; |
| 81 | std::string message_; |
| 82 | |
| 83 | DISALLOW_COPY_AND_ASSIGN(Error); |
| 84 | }; |
| 85 | |
| 86 | } // namespace shill |
| 87 | |
| 88 | #endif // SHILL_ERROR_ |