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 | #include "shill/error.h" |
| 6 | |
Paul Stewart | 71b9ed5 | 2014-01-29 08:53:06 -0800 | [diff] [blame] | 7 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 8 | #include <dbus-c++/error.h> |
| 9 | |
| 10 | #include "shill/dbus_adaptor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 11 | #include "shill/logging.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 12 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 13 | using std::string; |
| 14 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 15 | namespace shill { |
| 16 | |
| 17 | // static |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 18 | const Error::Info Error::kInfos[kNumErrors] = { |
Paul Stewart | 71b9ed5 | 2014-01-29 08:53:06 -0800 | [diff] [blame] | 19 | { kErrorResultSuccess, "Success (no error)" }, |
| 20 | { kErrorResultFailure, "Operation failed (no other information)" }, |
| 21 | { kErrorResultAlreadyConnected, "Already connected" }, |
| 22 | { kErrorResultAlreadyExists, "Already exists" }, |
| 23 | { kErrorResultIncorrectPin, "Incorrect PIN" }, |
| 24 | { kErrorResultInProgress, "In progress" }, |
| 25 | { kErrorResultInternalError, "Internal error" }, |
| 26 | { kErrorResultInvalidApn, "Invalid APN" }, |
| 27 | { kErrorResultInvalidArguments, "Invalid arguments" }, |
| 28 | { kErrorResultInvalidNetworkName, "Invalid network name" }, |
| 29 | { kErrorResultInvalidPassphrase, "Invalid passphrase" }, |
| 30 | { kErrorResultInvalidProperty, "Invalid property" }, |
| 31 | { kErrorResultNoCarrier, "No carrier" }, |
| 32 | { kErrorResultNotConnected, "Not connected" }, |
| 33 | { kErrorResultNotFound, "Not found" }, |
| 34 | { kErrorResultNotImplemented, "Not implemented" }, |
| 35 | { kErrorResultNotOnHomeNetwork, "Not on home network" }, |
| 36 | { kErrorResultNotRegistered, "Not registered" }, |
| 37 | { kErrorResultNotSupported, "Not supported" }, |
| 38 | { kErrorResultOperationAborted, "Operation aborted" }, |
| 39 | { kErrorResultOperationInitiated, "Operation initiated" }, |
| 40 | { kErrorResultOperationTimeout, "Operation timeout" }, |
| 41 | { kErrorResultPassphraseRequired, "Passphrase required" }, |
| 42 | { kErrorResultPermissionDenied, "Permission denied" }, |
| 43 | { kErrorResultPinBlocked, "SIM PIN is blocked"}, |
| 44 | { kErrorResultPinRequired, "SIM PIN is required"}, |
| 45 | { kErrorResultWrongState, "Wrong state" } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 48 | Error::Error() { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 49 | Reset(); |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | Error::Error(Type type) { |
| 53 | Populate(type); |
| 54 | } |
| 55 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 56 | Error::Error(Type type, const string &message) { |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 57 | Populate(type, message); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | Error::~Error() {} |
| 61 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 62 | void Error::Populate(Type type) { |
| 63 | Populate(type, GetDefaultMessage(type)); |
| 64 | } |
| 65 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 66 | void Error::Populate(Type type, const string &message) { |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 67 | CHECK(type < kNumErrors) << "Error type out of range: " << type; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 68 | type_ = type; |
| 69 | message_ = message; |
| 70 | } |
| 71 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 72 | void Error::Reset() { |
| 73 | Populate(kSuccess); |
| 74 | } |
| 75 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 76 | void Error::CopyFrom(const Error &error) { |
| 77 | Populate(error.type_, error.message_); |
| 78 | } |
| 79 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 80 | bool Error::ToDBusError(::DBus::Error *error) const { |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 81 | if (IsFailure()) { |
Paul Stewart | 71b9ed5 | 2014-01-29 08:53:06 -0800 | [diff] [blame] | 82 | error->set(GetDBusResult(type_).c_str(), message_.c_str()); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 83 | return true; |
| 84 | } else { |
| 85 | return false; |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | // static |
Paul Stewart | 71b9ed5 | 2014-01-29 08:53:06 -0800 | [diff] [blame] | 90 | string Error::GetDBusResult(Type type) { |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 91 | CHECK(type < kNumErrors) << "Error type out of range: " << type; |
Paul Stewart | 71b9ed5 | 2014-01-29 08:53:06 -0800 | [diff] [blame] | 92 | return kInfos[type].dbus_result; |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // static |
| 96 | string Error::GetDefaultMessage(Type type) { |
| 97 | CHECK(type < kNumErrors) << "Error type out of range: " << type; |
| 98 | return kInfos[type].message; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 101 | // static |
| 102 | void Error::PopulateAndLog(Error *error, Type type, const string &message) { |
| 103 | LOG(ERROR) << message; |
| 104 | if (error) { |
| 105 | error->Populate(type, message); |
| 106 | } |
| 107 | } |
| 108 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 109 | } // namespace shill |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 110 | |
| 111 | std::ostream &operator<<(std::ostream &stream, const shill::Error &error) { |
Paul Stewart | 71b9ed5 | 2014-01-29 08:53:06 -0800 | [diff] [blame] | 112 | stream << error.GetDBusResult(error.type()) << ": " << error.message(); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 113 | return stream; |
| 114 | } |