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