blob: 8557ef389d756585dfbbb83fd81f3c65d5a1bd1f [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 Stewart34f424e2015-01-16 15:30:20 -08007#include <base/files/file_path.h>
Paul Stewart71b9ed52014-01-29 08:53:06 -08008#include <chromeos/dbus/service_constants.h>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07009#include <dbus-c++/error.h>
10
11#include "shill/dbus_adaptor.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070012#include "shill/logging.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070013
Darin Petkove4c0ace2011-08-24 10:32:46 -070014using std::string;
15
Chris Masone8fe2c7e2011-06-09 15:51:19 -070016namespace shill {
17
18// static
Darin Petkove4c0ace2011-08-24 10:32:46 -070019const Error::Info Error::kInfos[kNumErrors] = {
Paul Stewart71b9ed52014-01-29 08:53:06 -080020 { kErrorResultSuccess, "Success (no error)" },
21 { kErrorResultFailure, "Operation failed (no other information)" },
22 { kErrorResultAlreadyConnected, "Already connected" },
23 { kErrorResultAlreadyExists, "Already exists" },
24 { kErrorResultIncorrectPin, "Incorrect PIN" },
25 { kErrorResultInProgress, "In progress" },
26 { kErrorResultInternalError, "Internal error" },
27 { kErrorResultInvalidApn, "Invalid APN" },
28 { kErrorResultInvalidArguments, "Invalid arguments" },
29 { kErrorResultInvalidNetworkName, "Invalid network name" },
30 { kErrorResultInvalidPassphrase, "Invalid passphrase" },
31 { kErrorResultInvalidProperty, "Invalid property" },
32 { kErrorResultNoCarrier, "No carrier" },
33 { kErrorResultNotConnected, "Not connected" },
34 { kErrorResultNotFound, "Not found" },
35 { kErrorResultNotImplemented, "Not implemented" },
36 { kErrorResultNotOnHomeNetwork, "Not on home network" },
37 { kErrorResultNotRegistered, "Not registered" },
38 { kErrorResultNotSupported, "Not supported" },
39 { kErrorResultOperationAborted, "Operation aborted" },
40 { kErrorResultOperationInitiated, "Operation initiated" },
41 { kErrorResultOperationTimeout, "Operation timeout" },
42 { kErrorResultPassphraseRequired, "Passphrase required" },
43 { kErrorResultPermissionDenied, "Permission denied" },
44 { kErrorResultPinBlocked, "SIM PIN is blocked"},
45 { kErrorResultPinRequired, "SIM PIN is required"},
46 { kErrorResultWrongState, "Wrong state" }
Chris Masone8fe2c7e2011-06-09 15:51:19 -070047};
48
Darin Petkove4c0ace2011-08-24 10:32:46 -070049Error::Error() {
Gaurav Shah1b7a6162011-11-09 11:41:01 -080050 Reset();
Darin Petkove4c0ace2011-08-24 10:32:46 -070051}
52
53Error::Error(Type type) {
54 Populate(type);
55}
56
Paul Stewarta794cd62015-06-16 13:13:10 -070057Error::Error(Type type, const string& message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070058 Populate(type, message);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070059}
60
61Error::~Error() {}
62
Darin Petkove4c0ace2011-08-24 10:32:46 -070063void Error::Populate(Type type) {
64 Populate(type, GetDefaultMessage(type));
65}
66
Paul Stewarta794cd62015-06-16 13:13:10 -070067void Error::Populate(Type type, const string& message) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070068 CHECK(type < kNumErrors) << "Error type out of range: " << type;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070069 type_ = type;
70 message_ = message;
71}
72
Gaurav Shah1b7a6162011-11-09 11:41:01 -080073void Error::Reset() {
74 Populate(kSuccess);
75}
76
Paul Stewarta794cd62015-06-16 13:13:10 -070077void Error::CopyFrom(const Error& error) {
Darin Petkove5bc2cb2011-12-07 14:47:32 +010078 Populate(error.type_, error.message_);
79}
80
Paul Stewarta794cd62015-06-16 13:13:10 -070081bool Error::ToDBusError(::DBus::Error* error) const {
Darin Petkove4c0ace2011-08-24 10:32:46 -070082 if (IsFailure()) {
Paul Stewart71b9ed52014-01-29 08:53:06 -080083 error->set(GetDBusResult(type_).c_str(), message_.c_str());
mukesh agrawal7a4e4002011-09-06 11:26:05 -070084 return true;
85 } else {
86 return false;
Darin Petkove4c0ace2011-08-24 10:32:46 -070087 }
88}
89
90// static
Paul Stewart71b9ed52014-01-29 08:53:06 -080091string Error::GetDBusResult(Type type) {
Darin Petkove4c0ace2011-08-24 10:32:46 -070092 CHECK(type < kNumErrors) << "Error type out of range: " << type;
Paul Stewart71b9ed52014-01-29 08:53:06 -080093 return kInfos[type].dbus_result;
Darin Petkove4c0ace2011-08-24 10:32:46 -070094}
95
96// static
97string Error::GetDefaultMessage(Type type) {
98 CHECK(type < kNumErrors) << "Error type out of range: " << type;
99 return kInfos[type].message;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700100}
101
Paul Stewartbe005172011-11-02 18:10:29 -0700102// static
Paul Stewarta794cd62015-06-16 13:13:10 -0700103void Error::PopulateAndLog(const tracked_objects::Location& from_here,
104 Error* error, Type type, const string& message) {
Paul Stewart34f424e2015-01-16 15:30:20 -0800105 string file_name = base::FilePath(from_here.file_name()).BaseName().value();
106 LOG(ERROR) << "[" << file_name << "("
107 << from_here.line_number() << ")]: "<< message;
Paul Stewartbe005172011-11-02 18:10:29 -0700108 if (error) {
109 error->Populate(type, message);
110 }
111}
112
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700113} // namespace shill
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500114
Paul Stewarta794cd62015-06-16 13:13:10 -0700115std::ostream& operator<<(std::ostream& stream, const shill::Error& error) {
Paul Stewart71b9ed52014-01-29 08:53:06 -0800116 stream << error.GetDBusResult(error.type()) << ": " << error.message();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500117 return stream;
118}