1 // 2 // Copyright (C) 2015 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 #ifndef APMANAGER_ERROR_H_ 18 #define APMANAGER_ERROR_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include <base/location.h> 24 #include <base/macros.h> 25 26 namespace brillo { 27 class Error; 28 using ErrorPtr = std::unique_ptr<Error>; 29 } // namespace brillo 30 31 namespace apmanager { 32 33 class Error { 34 public: 35 enum Type { 36 kSuccess = 0, // No error. 37 kOperationInProgress, 38 kInternalError, 39 kInvalidArguments, 40 kInvalidConfiguration, 41 kNumErrors 42 }; 43 44 Error(); 45 ~Error(); 46 47 void Populate(Type type, 48 const std::string& message, 49 const tracked_objects::Location& location); 50 51 void Reset(); 52 type()53 Type type() const { return type_; } message()54 const std::string& message() const { return message_; } 55 IsSuccess()56 bool IsSuccess() const { return type_ == kSuccess; } IsFailure()57 bool IsFailure() const { return !IsSuccess() && !IsOngoing(); } IsOngoing()58 bool IsOngoing() const { return type_ == kOperationInProgress; } 59 60 // Log an error message from |from_here|. If |error| is non-NULL, also 61 // populate it. 62 static void PopulateAndLog(Error* error, 63 Type type, 64 const std::string& message, 65 const tracked_objects::Location& from_here); 66 67 // TODO(zqiu): put this under a compiler flag (e.g. __DBUS__). 68 // Sets the D-Bus error and returns true if Error represents failure. 69 // Leaves error unchanged, and returns false otherwise. 70 bool ToDBusError(brillo::ErrorPtr* error) const; 71 72 private: 73 friend class ErrorTest; 74 75 Type type_; 76 std::string message_; 77 tracked_objects::Location location_; 78 79 DISALLOW_COPY_AND_ASSIGN(Error); 80 }; 81 82 } // namespace apmanager 83 84 #endif // APMANAGER_ERROR_H_ 85