blob: e486bb58931db0c39d6abcac72ebafef8664323a [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() {
45 Populate(kSuccess);
46}
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
mukesh agrawal7a4e4002011-09-06 11:26:05 -070068bool Error::ToDBusError(::DBus::Error *error) const {
Darin Petkove4c0ace2011-08-24 10:32:46 -070069 if (IsFailure()) {
70 error->set(GetName(type_).c_str(), message_.c_str());
mukesh agrawal7a4e4002011-09-06 11:26:05 -070071 return true;
72 } else {
73 return false;
Darin Petkove4c0ace2011-08-24 10:32:46 -070074 }
75}
76
77// static
78string Error::GetName(Type type) {
79 CHECK(type < kNumErrors) << "Error type out of range: " << type;
80 return base::StringPrintf("%s.Error.%s", kInterfaceName, kInfos[type].name);
81}
82
83// static
84string Error::GetDefaultMessage(Type type) {
85 CHECK(type < kNumErrors) << "Error type out of range: " << type;
86 return kInfos[type].message;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070087}
88
89} // namespace shill