blob: 476c42bbeb742813e3cd4998ed683e1820840127 [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" },
Eric Shienbrood9a245532012-03-07 14:20:39 -050023 { "OperationInitiated", "Operation initiated" },
Darin Petkove4c0ace2011-08-24 10:32:46 -070024 { "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 Shienbrood5de44ab2011-12-05 10:46:27 -050040 { "IncorrectPin", "Incorrect PIN" },
41 { "PinRequired", "SIM PIN is required"},
42 { "PinBlocked", "SIM PIN is blocked"},
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040043 { "InvalidApn", "Invalid APN" },
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050044 { "PermissionDenied", "Permission denied" }
Chris Masone8fe2c7e2011-06-09 15:51:19 -070045};
46
Darin Petkove4c0ace2011-08-24 10:32:46 -070047Error::Error() {
Gaurav Shah1b7a6162011-11-09 11:41:01 -080048 Reset();
Darin Petkove4c0ace2011-08-24 10:32:46 -070049}
50
51Error::Error(Type type) {
52 Populate(type);
53}
54
Darin Petkovb100ae72011-08-24 16:19:45 -070055Error::Error(Type type, const string &message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070056 Populate(type, message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070057}
58
59Error::~Error() {}
60
Darin Petkove4c0ace2011-08-24 10:32:46 -070061void Error::Populate(Type type) {
62 Populate(type, GetDefaultMessage(type));
63}
64
Darin Petkovb100ae72011-08-24 16:19:45 -070065void Error::Populate(Type type, const string &message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070066 CHECK(type < kNumErrors) << "Error type out of range: " << type;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070067 type_ = type;
68 message_ = message;
69}
70
Gaurav Shah1b7a6162011-11-09 11:41:01 -080071void Error::Reset() {
72 Populate(kSuccess);
73}
74
Darin Petkove5bc2cb2011-12-07 14:47:32 +010075void Error::CopyFrom(const Error &error) {
76 Populate(error.type_, error.message_);
77}
78
mukesh agrawal7a4e4002011-09-06 11:26:05 -070079bool Error::ToDBusError(::DBus::Error *error) const {
Darin Petkove4c0ace2011-08-24 10:32:46 -070080 if (IsFailure()) {
81 error->set(GetName(type_).c_str(), message_.c_str());
mukesh agrawal7a4e4002011-09-06 11:26:05 -070082 return true;
83 } else {
84 return false;
Darin Petkove4c0ace2011-08-24 10:32:46 -070085 }
86}
87
88// static
89string Error::GetName(Type type) {
90 CHECK(type < kNumErrors) << "Error type out of range: " << type;
mukesh agrawald1511fe2012-03-14 17:12:27 -070091 return base::StringPrintf("%s.Error.%s", SHILL_INTERFACE, kInfos[type].name);
Darin Petkove4c0ace2011-08-24 10:32:46 -070092}
93
94// static
95string Error::GetDefaultMessage(Type type) {
96 CHECK(type < kNumErrors) << "Error type out of range: " << type;
97 return kInfos[type].message;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070098}
99
Paul Stewartbe005172011-11-02 18:10:29 -0700100// static
101void 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 Masone8fe2c7e2011-06-09 15:51:19 -0700108} // namespace shill
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500109
110std::ostream &operator<<(std::ostream &stream, const shill::Error &error) {
111 stream << error.GetName(error.type()) << ":" << error.message();
112 return stream;
113}