1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/error.h"
18
19 #include <base/files/file_path.h>
20 #if defined(__ANDROID__)
21 #include <dbus/service_constants.h>
22 #else
23 #include <chromeos/dbus/service_constants.h>
24 #endif // __ANDROID__
25 #include <brillo/errors/error.h>
26 #include <brillo/errors/error_codes.h>
27
28 #include "shill/logging.h"
29
30 using std::string;
31
32 namespace shill {
33
34 // static
35 const Error::Info Error::kInfos[kNumErrors] = {
36 { kErrorResultSuccess, "Success (no error)" },
37 { kErrorResultFailure, "Operation failed (no other information)" },
38 { kErrorResultAlreadyConnected, "Already connected" },
39 { kErrorResultAlreadyExists, "Already exists" },
40 { kErrorResultIncorrectPin, "Incorrect PIN" },
41 { kErrorResultInProgress, "In progress" },
42 { kErrorResultInternalError, "Internal error" },
43 { kErrorResultInvalidApn, "Invalid APN" },
44 { kErrorResultInvalidArguments, "Invalid arguments" },
45 { kErrorResultInvalidNetworkName, "Invalid network name" },
46 { kErrorResultInvalidPassphrase, "Invalid passphrase" },
47 { kErrorResultInvalidProperty, "Invalid property" },
48 { kErrorResultNoCarrier, "No carrier" },
49 { kErrorResultNotConnected, "Not connected" },
50 { kErrorResultNotFound, "Not found" },
51 { kErrorResultNotImplemented, "Not implemented" },
52 { kErrorResultNotOnHomeNetwork, "Not on home network" },
53 { kErrorResultNotRegistered, "Not registered" },
54 { kErrorResultNotSupported, "Not supported" },
55 { kErrorResultOperationAborted, "Operation aborted" },
56 { kErrorResultOperationInitiated, "Operation initiated" },
57 { kErrorResultOperationTimeout, "Operation timeout" },
58 { kErrorResultPassphraseRequired, "Passphrase required" },
59 { kErrorResultPermissionDenied, "Permission denied" },
60 { kErrorResultPinBlocked, "SIM PIN is blocked"},
61 { kErrorResultPinRequired, "SIM PIN is required"},
62 { kErrorResultWrongState, "Wrong state" }
63 };
64
Error()65 Error::Error() {
66 Reset();
67 }
68
Error(Type type)69 Error::Error(Type type) {
70 Populate(type);
71 }
72
Error(Type type,const string & message)73 Error::Error(Type type, const string& message) {
74 Populate(type, message);
75 }
76
~Error()77 Error::~Error() {}
78
Populate(Type type)79 void Error::Populate(Type type) {
80 Populate(type, GetDefaultMessage(type));
81 }
82
Populate(Type type,const string & message)83 void Error::Populate(Type type, const string& message) {
84 CHECK(type < kNumErrors) << "Error type out of range: " << type;
85 type_ = type;
86 message_ = message;
87 }
88
Populate(Type type,const string & message,const tracked_objects::Location & location)89 void Error::Populate(Type type,
90 const string& message,
91 const tracked_objects::Location& location) {
92 CHECK(type < kNumErrors) << "Error type out of range: " << type;
93 type_ = type;
94 message_ = message;
95 location_ = location;
96 }
97
Reset()98 void Error::Reset() {
99 Populate(kSuccess);
100 }
101
CopyFrom(const Error & error)102 void Error::CopyFrom(const Error& error) {
103 Populate(error.type_, error.message_);
104 }
105
ToChromeosError(brillo::ErrorPtr * error) const106 bool Error::ToChromeosError(brillo::ErrorPtr* error) const {
107 if (IsFailure()) {
108 brillo::Error::AddTo(error,
109 location_,
110 brillo::errors::dbus::kDomain,
111 kInfos[type_].dbus_result,
112 message_);
113 return true;
114 }
115 return false;
116 }
117
118 // static
GetDBusResult(Type type)119 string Error::GetDBusResult(Type type) {
120 CHECK(type < kNumErrors) << "Error type out of range: " << type;
121 return kInfos[type].dbus_result;
122 }
123
124 // static
GetDefaultMessage(Type type)125 string Error::GetDefaultMessage(Type type) {
126 CHECK(type < kNumErrors) << "Error type out of range: " << type;
127 return kInfos[type].message;
128 }
129
130 // static
PopulateAndLog(const tracked_objects::Location & from_here,Error * error,Type type,const string & message)131 void Error::PopulateAndLog(const tracked_objects::Location& from_here,
132 Error* error, Type type, const string& message) {
133 string file_name = base::FilePath(from_here.file_name()).BaseName().value();
134 LOG(ERROR) << "[" << file_name << "("
135 << from_here.line_number() << ")]: "<< message;
136 if (error) {
137 error->Populate(type, message, from_here);
138 }
139 }
140
141 } // namespace shill
142
operator <<(std::ostream & stream,const shill::Error & error)143 std::ostream& operator<<(std::ostream& stream, const shill::Error& error) {
144 stream << error.GetDBusResult(error.type()) << ": " << error.message();
145 return stream;
146 }
147