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