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