Peter Qiu | 69d2521 | 2015-11-19 13:13:25 -0800 | [diff] [blame] | 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 "apmanager/error.h" |
| 18 | |
| 19 | #include <base/files/file_path.h> |
| 20 | #include <base/logging.h> |
| 21 | #include <brillo/errors/error.h> |
| 22 | #include <brillo/errors/error_codes.h> |
| 23 | |
| 24 | #if defined(__ANDROID__) |
| 25 | #include <dbus/service_constants.h> |
| 26 | #else |
| 27 | #include <chromeos/dbus/service_constants.h> |
| 28 | #endif // __ANDROID__ |
| 29 | |
| 30 | using std::string; |
| 31 | |
| 32 | namespace apmanager { |
| 33 | |
| 34 | Error::Error() : type_(kSuccess) {} |
| 35 | |
| 36 | Error::~Error() {} |
| 37 | |
| 38 | void Error::Populate(Type type, |
| 39 | const string& message, |
| 40 | const tracked_objects::Location& location) { |
| 41 | CHECK(type < kNumErrors) << "Error type out of range: " << type; |
| 42 | type_ = type; |
| 43 | message_ = message; |
| 44 | location_ = location; |
| 45 | } |
| 46 | |
| 47 | void Error::Reset() { |
| 48 | type_ = kSuccess; |
| 49 | message_ = ""; |
| 50 | location_ = tracked_objects::Location(); |
| 51 | } |
| 52 | |
| 53 | bool Error::ToDBusError(brillo::ErrorPtr* error) const { |
| 54 | if (IsSuccess()) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | string error_code = kErrorInternalError; |
| 59 | if (type_ == kInvalidArguments) { |
| 60 | error_code = kErrorInvalidArguments; |
| 61 | } else if (type_ == kInvalidConfiguration) { |
| 62 | error_code = kErrorInvalidConfiguration; |
| 63 | } |
| 64 | |
| 65 | brillo::Error::AddTo(error, |
| 66 | location_, |
| 67 | brillo::errors::dbus::kDomain, |
| 68 | error_code, |
| 69 | message_); |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | // static |
| 74 | void Error::PopulateAndLog(Error* error, |
| 75 | Type type, |
| 76 | const string& message, |
| 77 | const tracked_objects::Location& from_here) { |
| 78 | string file_name = base::FilePath(from_here.file_name()).BaseName().value(); |
| 79 | LOG(ERROR) << "[" << file_name << "(" |
| 80 | << from_here.line_number() << ")]: "<< message; |
| 81 | if (error) { |
| 82 | error->Populate(type, message, from_here); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } // namespace apmanager |