blob: 87c8a5317a757a9a2aa230032039b90562f542a4 [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
Paul Stewart71b9ed52014-01-29 08:53:06 -08007#include <chromeos/dbus/service_constants.h>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07008#include <dbus-c++/error.h>
9
10#include "shill/dbus_adaptor.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070011#include "shill/logging.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070012
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] = {
Paul Stewart71b9ed52014-01-29 08:53:06 -080019 { kErrorResultSuccess, "Success (no error)" },
20 { kErrorResultFailure, "Operation failed (no other information)" },
21 { kErrorResultAlreadyConnected, "Already connected" },
22 { kErrorResultAlreadyExists, "Already exists" },
23 { kErrorResultIncorrectPin, "Incorrect PIN" },
24 { kErrorResultInProgress, "In progress" },
25 { kErrorResultInternalError, "Internal error" },
26 { kErrorResultInvalidApn, "Invalid APN" },
27 { kErrorResultInvalidArguments, "Invalid arguments" },
28 { kErrorResultInvalidNetworkName, "Invalid network name" },
29 { kErrorResultInvalidPassphrase, "Invalid passphrase" },
30 { kErrorResultInvalidProperty, "Invalid property" },
31 { kErrorResultNoCarrier, "No carrier" },
32 { kErrorResultNotConnected, "Not connected" },
33 { kErrorResultNotFound, "Not found" },
34 { kErrorResultNotImplemented, "Not implemented" },
35 { kErrorResultNotOnHomeNetwork, "Not on home network" },
36 { kErrorResultNotRegistered, "Not registered" },
37 { kErrorResultNotSupported, "Not supported" },
38 { kErrorResultOperationAborted, "Operation aborted" },
39 { kErrorResultOperationInitiated, "Operation initiated" },
40 { kErrorResultOperationTimeout, "Operation timeout" },
41 { kErrorResultPassphraseRequired, "Passphrase required" },
42 { kErrorResultPermissionDenied, "Permission denied" },
43 { kErrorResultPinBlocked, "SIM PIN is blocked"},
44 { kErrorResultPinRequired, "SIM PIN is required"},
45 { kErrorResultWrongState, "Wrong state" }
Chris Masone8fe2c7e2011-06-09 15:51:19 -070046};
47
Darin Petkove4c0ace2011-08-24 10:32:46 -070048Error::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()) {
Paul Stewart71b9ed52014-01-29 08:53:06 -080082 error->set(GetDBusResult(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
Paul Stewart71b9ed52014-01-29 08:53:06 -080090string Error::GetDBusResult(Type type) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070091 CHECK(type < kNumErrors) << "Error type out of range: " << type;
Paul Stewart71b9ed52014-01-29 08:53:06 -080092 return kInfos[type].dbus_result;
Darin Petkove4c0ace2011-08-24 10:32:46 -070093}
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) {
Paul Stewart71b9ed52014-01-29 08:53:06 -0800112 stream << error.GetDBusResult(error.type()) << ": " << error.message();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500113 return stream;
114}