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