blob: b1d5deaae549dec2f8d588e630e79307e9a41db1 [file] [log] [blame]
Chris Masone8fe2c7e2011-06-09 15:51:19 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
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 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)" },
20 { "AlreadyConnected", "Already connected" },
21 { "AlreadyExists", "Already exists" },
22 { "InProgress", "In progress" },
23 { "InternalError", "Internal error" },
24 { "InvalidArguments", "Invalid arguments" },
25 { "InvalidNetworkName", "Invalid network name" },
26 { "InvalidPassphrase", "Invalid passphrase" },
27 { "InvalidProperty", "Invalid property" },
28 { "NoCarrier", "No carrier" },
29 { "NotConnected", "Not connected" },
30 { "NotFound", "Not found" },
31 { "NotImplemented", "Not implemented" },
32 { "NotOnHomeNetwork", "Not on home network" },
33 { "NotRegistered", "Not registered" },
34 { "NotSupported", "Not supported" },
35 { "OperationAborted", "Operation aborted" },
36 { "OperationTimeout", "Operation timeout" },
37 { "PassphraseRequired", "Passphrase required" },
38 { "PermissionDenied", "Permission denied" },
Chris Masone8fe2c7e2011-06-09 15:51:19 -070039};
40
Darin Petkove4c0ace2011-08-24 10:32:46 -070041// static
42const char Error::kInterfaceName[] = SHILL_INTERFACE;
43
44Error::Error() {
Gaurav Shah1b7a6162011-11-09 11:41:01 -080045 Reset();
Darin Petkove4c0ace2011-08-24 10:32:46 -070046}
47
48Error::Error(Type type) {
49 Populate(type);
50}
51
Darin Petkovb100ae72011-08-24 16:19:45 -070052Error::Error(Type type, const string &message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070053 Populate(type, message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070054}
55
56Error::~Error() {}
57
Darin Petkove4c0ace2011-08-24 10:32:46 -070058void Error::Populate(Type type) {
59 Populate(type, GetDefaultMessage(type));
60}
61
Darin Petkovb100ae72011-08-24 16:19:45 -070062void Error::Populate(Type type, const string &message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070063 CHECK(type < kNumErrors) << "Error type out of range: " << type;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070064 type_ = type;
65 message_ = message;
66}
67
Gaurav Shah1b7a6162011-11-09 11:41:01 -080068void Error::Reset() {
69 Populate(kSuccess);
70}
71
Darin Petkove5bc2cb2011-12-07 14:47:32 +010072void Error::CopyFrom(const Error &error) {
73 Populate(error.type_, error.message_);
74}
75
mukesh agrawal7a4e4002011-09-06 11:26:05 -070076bool Error::ToDBusError(::DBus::Error *error) const {
Darin Petkove4c0ace2011-08-24 10:32:46 -070077 if (IsFailure()) {
78 error->set(GetName(type_).c_str(), message_.c_str());
mukesh agrawal7a4e4002011-09-06 11:26:05 -070079 return true;
80 } else {
81 return false;
Darin Petkove4c0ace2011-08-24 10:32:46 -070082 }
83}
84
85// static
86string Error::GetName(Type type) {
87 CHECK(type < kNumErrors) << "Error type out of range: " << type;
88 return base::StringPrintf("%s.Error.%s", kInterfaceName, kInfos[type].name);
89}
90
91// static
92string Error::GetDefaultMessage(Type type) {
93 CHECK(type < kNumErrors) << "Error type out of range: " << type;
94 return kInfos[type].message;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070095}
96
Paul Stewartbe005172011-11-02 18:10:29 -070097// static
98void Error::PopulateAndLog(Error *error, Type type, const string &message) {
99 LOG(ERROR) << message;
100 if (error) {
101 error->Populate(type, message);
102 }
103}
104
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700105} // namespace shill