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 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 7 | #include <base/stringprintf.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] = { |
| 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" }, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 23 | { "OperationInitiated", "Operation initiated" }, |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 24 | { "InProgress", "In progress" }, |
| 25 | { "InternalError", "Internal error" }, |
| 26 | { "InvalidArguments", "Invalid arguments" }, |
| 27 | { "InvalidNetworkName", "Invalid network name" }, |
| 28 | { "InvalidPassphrase", "Invalid passphrase" }, |
| 29 | { "InvalidProperty", "Invalid property" }, |
| 30 | { "NoCarrier", "No carrier" }, |
| 31 | { "NotConnected", "Not connected" }, |
| 32 | { "NotFound", "Not found" }, |
| 33 | { "NotImplemented", "Not implemented" }, |
| 34 | { "NotOnHomeNetwork", "Not on home network" }, |
| 35 | { "NotRegistered", "Not registered" }, |
| 36 | { "NotSupported", "Not supported" }, |
| 37 | { "OperationAborted", "Operation aborted" }, |
| 38 | { "OperationTimeout", "Operation timeout" }, |
| 39 | { "PassphraseRequired", "Passphrase required" }, |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 40 | { "IncorrectPin", "Incorrect PIN" }, |
| 41 | { "PinRequired", "SIM PIN is required"}, |
| 42 | { "PinBlocked", "SIM PIN is blocked"}, |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 43 | { "InvalidApn", "Invalid APN" }, |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 44 | { "PermissionDenied", "Permission denied" } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 47 | Error::Error() { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 48 | Reset(); |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | Error::Error(Type type) { |
| 52 | Populate(type); |
| 53 | } |
| 54 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 55 | Error::Error(Type type, const string &message) { |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 56 | Populate(type, message); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | Error::~Error() {} |
| 60 | |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 61 | void Error::Populate(Type type) { |
| 62 | Populate(type, GetDefaultMessage(type)); |
| 63 | } |
| 64 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 65 | void Error::Populate(Type type, const string &message) { |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 66 | CHECK(type < kNumErrors) << "Error type out of range: " << type; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 67 | type_ = type; |
| 68 | message_ = message; |
| 69 | } |
| 70 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 71 | void Error::Reset() { |
| 72 | Populate(kSuccess); |
| 73 | } |
| 74 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 75 | void Error::CopyFrom(const Error &error) { |
| 76 | Populate(error.type_, error.message_); |
| 77 | } |
| 78 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 79 | bool Error::ToDBusError(::DBus::Error *error) const { |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 80 | if (IsFailure()) { |
| 81 | error->set(GetName(type_).c_str(), message_.c_str()); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 82 | return true; |
| 83 | } else { |
| 84 | return false; |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | // static |
| 89 | string Error::GetName(Type type) { |
| 90 | CHECK(type < kNumErrors) << "Error type out of range: " << type; |
mukesh agrawal | d1511fe | 2012-03-14 17:12:27 -0700 | [diff] [blame] | 91 | return base::StringPrintf("%s.Error.%s", SHILL_INTERFACE, kInfos[type].name); |
Darin Petkov | e4c0ace | 2011-08-24 10:32:46 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // static |
| 95 | string Error::GetDefaultMessage(Type type) { |
| 96 | CHECK(type < kNumErrors) << "Error type out of range: " << type; |
| 97 | return kInfos[type].message; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 100 | // static |
| 101 | void Error::PopulateAndLog(Error *error, Type type, const string &message) { |
| 102 | LOG(ERROR) << message; |
| 103 | if (error) { |
| 104 | error->Populate(type, message); |
| 105 | } |
| 106 | } |
| 107 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 108 | } // namespace shill |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 109 | |
| 110 | std::ostream &operator<<(std::ostream &stream, const shill::Error &error) { |
| 111 | stream << error.GetName(error.type()) << ":" << error.message(); |
| 112 | return stream; |
| 113 | } |