blob: 5fd4ec4eee32d85dd4cd820d0611c22ca6ed914f [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
mukesh agrawal7a4e4002011-09-06 11:26:05 -070072bool Error::ToDBusError(::DBus::Error *error) const {
Darin Petkove4c0ace2011-08-24 10:32:46 -070073 if (IsFailure()) {
74 error->set(GetName(type_).c_str(), message_.c_str());
mukesh agrawal7a4e4002011-09-06 11:26:05 -070075 return true;
76 } else {
77 return false;
Darin Petkove4c0ace2011-08-24 10:32:46 -070078 }
79}
80
81// static
82string Error::GetName(Type type) {
83 CHECK(type < kNumErrors) << "Error type out of range: " << type;
84 return base::StringPrintf("%s.Error.%s", kInterfaceName, kInfos[type].name);
85}
86
87// static
88string Error::GetDefaultMessage(Type type) {
89 CHECK(type < kNumErrors) << "Error type out of range: " << type;
90 return kInfos[type].message;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070091}
92
Paul Stewartbe005172011-11-02 18:10:29 -070093// static
94void Error::PopulateAndLog(Error *error, Type type, const string &message) {
95 LOG(ERROR) << message;
96 if (error) {
97 error->Populate(type, message);
98 }
99}
100
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700101} // namespace shill