blob: a61f736403988738177f21d21f9431451136185c [file] [log] [blame]
Eric Shienbrood5de44ab2011-12-05 10:46:27 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone8fe2c7e2011-06-09 15:51:19 -07002// 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 Masone8fe2c7e2011-06-09 15:51:19 -07007#include <base/logging.h>
Darin Petkove4c0ace2011-08-24 10:32:46 -07008#include <base/stringprintf.h>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07009#include <dbus-c++/error.h>
10
11#include "shill/dbus_adaptor.h"
12
Darin Petkove4c0ace2011-08-24 10:32:46 -070013using std::string;
14
Chris Masone8fe2c7e2011-06-09 15:51:19 -070015namespace shill {
16
17// static
Darin Petkove4c0ace2011-08-24 10:32:46 -070018const Error::Info Error::kInfos[kNumErrors] = {
19 { "Success", "Success (no error)" },
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050020 { "Failure", "Operation failed (no other information)" },
Darin Petkove4c0ace2011-08-24 10:32:46 -070021 { "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 Shienbrood5de44ab2011-12-05 10:46:27 -050039 { "IncorrectPin", "Incorrect PIN" },
40 { "PinRequired", "SIM PIN is required"},
41 { "PinBlocked", "SIM PIN is blocked"},
42 { "PermissionDenied", "Permission denied" }
Chris Masone8fe2c7e2011-06-09 15:51:19 -070043};
44
Darin Petkove4c0ace2011-08-24 10:32:46 -070045Error::Error() {
Gaurav Shah1b7a6162011-11-09 11:41:01 -080046 Reset();
Darin Petkove4c0ace2011-08-24 10:32:46 -070047}
48
49Error::Error(Type type) {
50 Populate(type);
51}
52
Darin Petkovb100ae72011-08-24 16:19:45 -070053Error::Error(Type type, const string &message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070054 Populate(type, message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070055}
56
57Error::~Error() {}
58
Darin Petkove4c0ace2011-08-24 10:32:46 -070059void Error::Populate(Type type) {
60 Populate(type, GetDefaultMessage(type));
61}
62
Darin Petkovb100ae72011-08-24 16:19:45 -070063void Error::Populate(Type type, const string &message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070064 CHECK(type < kNumErrors) << "Error type out of range: " << type;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070065 type_ = type;
66 message_ = message;
67}
68
Gaurav Shah1b7a6162011-11-09 11:41:01 -080069void Error::Reset() {
70 Populate(kSuccess);
71}
72
Darin Petkove5bc2cb2011-12-07 14:47:32 +010073void Error::CopyFrom(const Error &error) {
74 Populate(error.type_, error.message_);
75}
76
mukesh agrawal7a4e4002011-09-06 11:26:05 -070077bool Error::ToDBusError(::DBus::Error *error) const {
Darin Petkove4c0ace2011-08-24 10:32:46 -070078 if (IsFailure()) {
79 error->set(GetName(type_).c_str(), message_.c_str());
mukesh agrawal7a4e4002011-09-06 11:26:05 -070080 return true;
81 } else {
82 return false;
Darin Petkove4c0ace2011-08-24 10:32:46 -070083 }
84}
85
86// static
87string Error::GetName(Type type) {
88 CHECK(type < kNumErrors) << "Error type out of range: " << type;
mukesh agrawald1511fe2012-03-14 17:12:27 -070089 return base::StringPrintf("%s.Error.%s", SHILL_INTERFACE, kInfos[type].name);
Darin Petkove4c0ace2011-08-24 10:32:46 -070090}
91
92// static
93string Error::GetDefaultMessage(Type type) {
94 CHECK(type < kNumErrors) << "Error type out of range: " << type;
95 return kInfos[type].message;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070096}
97
Paul Stewartbe005172011-11-02 18:10:29 -070098// static
99void Error::PopulateAndLog(Error *error, Type type, const string &message) {
100 LOG(ERROR) << message;
101 if (error) {
102 error->Populate(type, message);
103 }
104}
105
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700106} // namespace shill
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500107
108std::ostream &operator<<(std::ostream &stream, const shill::Error &error) {
109 stream << error.GetName(error.type()) << ":" << error.message();
110 return stream;
111}