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 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 7 | #include <base/logging.h> |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 8 | #include <base/stringprintf.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" |
| 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] = { |
| 19 | { "Success", "Success (no error)" }, |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 20 | { "Failure", "Operation failed (no other information)" }, |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 21 | { "AlreadyConnected", "Already connected" }, |
| 22 | { "AlreadyExists", "Already exists" }, |
| 23 | { "InProgress", "In progress" }, |
| 24 | { "InternalError", "Internal error" }, |
| 25 | { "InvalidArguments", "Invalid arguments" }, |
| 26 | { "InvalidNetworkName", "Invalid network name" }, |
| 27 | { "InvalidPassphrase", "Invalid passphrase" }, |
| 28 | { "InvalidProperty", "Invalid property" }, |
| 29 | { "NoCarrier", "No carrier" }, |
| 30 | { "NotConnected", "Not connected" }, |
| 31 | { "NotFound", "Not found" }, |
| 32 | { "NotImplemented", "Not implemented" }, |
| 33 | { "NotOnHomeNetwork", "Not on home network" }, |
| 34 | { "NotRegistered", "Not registered" }, |
| 35 | { "NotSupported", "Not supported" }, |
| 36 | { "OperationAborted", "Operation aborted" }, |
| 37 | { "OperationTimeout", "Operation timeout" }, |
| 38 | { "PassphraseRequired", "Passphrase required" }, |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 39 | { "IncorrectPin", "Incorrect PIN" }, |
| 40 | { "PinRequired", "SIM PIN is required"}, |
| 41 | { "PinBlocked", "SIM PIN is blocked"}, |
| 42 | { "PermissionDenied", "Permission denied" } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 45 | // static |
| 46 | const char Error::kInterfaceName[] = SHILL_INTERFACE; |
| 47 | |
| 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()) { |
| 82 | error->set(GetName(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 |
| 90 | string Error::GetName(Type type) { |
| 91 | CHECK(type < kNumErrors) << "Error type out of range: " << type; |
| 92 | return base::StringPrintf("%s.Error.%s", kInterfaceName, kInfos[type].name); |
| 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) { |
| 112 | stream << error.GetName(error.type()) << ":" << error.message(); |
| 113 | return stream; |
| 114 | } |