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