blob: 28f7d284be041a42d4c621e5c056e3ee9328d642 [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 -070045// static
46const char Error::kInterfaceName[] = SHILL_INTERFACE;
47
48Error::Error() {
Gaurav Shah1b7a6162011-11-09 11:41:01 -080049 Reset();
Darin Petkove4c0ace2011-08-24 10:32:46 -070050}
51
52Error::Error(Type type) {
53 Populate(type);
54}
55
Darin Petkovb100ae72011-08-24 16:19:45 -070056Error::Error(Type type, const string &message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070057 Populate(type, message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070058}
59
60Error::~Error() {}
61
Darin Petkove4c0ace2011-08-24 10:32:46 -070062void Error::Populate(Type type) {
63 Populate(type, GetDefaultMessage(type));
64}
65
Darin Petkovb100ae72011-08-24 16:19:45 -070066void Error::Populate(Type type, const string &message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070067 CHECK(type < kNumErrors) << "Error type out of range: " << type;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070068 type_ = type;
69 message_ = message;
70}
71
Gaurav Shah1b7a6162011-11-09 11:41:01 -080072void Error::Reset() {
73 Populate(kSuccess);
74}
75
Darin Petkove5bc2cb2011-12-07 14:47:32 +010076void Error::CopyFrom(const Error &error) {
77 Populate(error.type_, error.message_);
78}
79
mukesh agrawal7a4e4002011-09-06 11:26:05 -070080bool Error::ToDBusError(::DBus::Error *error) const {
Darin Petkove4c0ace2011-08-24 10:32:46 -070081 if (IsFailure()) {
82 error->set(GetName(type_).c_str(), message_.c_str());
mukesh agrawal7a4e4002011-09-06 11:26:05 -070083 return true;
84 } else {
85 return false;
Darin Petkove4c0ace2011-08-24 10:32:46 -070086 }
87}
88
89// static
90string 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
96string Error::GetDefaultMessage(Type type) {
97 CHECK(type < kNumErrors) << "Error type out of range: " << type;
98 return kInfos[type].message;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070099}
100
Paul Stewartbe005172011-11-02 18:10:29 -0700101// static
102void 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 Masone8fe2c7e2011-06-09 15:51:19 -0700109} // namespace shill
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500110
111std::ostream &operator<<(std::ostream &stream, const shill::Error &error) {
112 stream << error.GetName(error.type()) << ":" << error.message();
113 return stream;
114}