Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef DLCSERVICE_ERROR_H_ |
| 6 | #define DLCSERVICE_ERROR_H_ |
| 7 | |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 8 | #include <map> |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 9 | #include <string> |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 10 | #include <utility> |
| 11 | #include <vector> |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 12 | |
| 13 | #include <brillo/errors/error.h> |
| 14 | |
| 15 | namespace dlcservice { |
| 16 | |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 17 | extern const char kDlcErrorDomain[]; |
| 18 | |
| 19 | namespace error { |
| 20 | extern const char kFailedToCreateDirectory[]; |
| 21 | extern const char kFailedInstallInUpdateEngine[]; |
| 22 | extern const char kFailedInternal[]; |
| 23 | extern const char kFailedToVerifyImage[]; |
| 24 | extern const char kFailedToMountImage[]; |
| 25 | }; // namespace error |
| 26 | |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 27 | class Error { |
| 28 | public: |
| 29 | // Returns the D-Bus error object with error code and error message set. |
Andrew | 9aa0644 | 2020-05-06 10:33:46 -0700 | [diff] [blame] | 30 | static brillo::ErrorPtr Create(const base::Location& location, |
| 31 | const std::string& code, |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 32 | const std::string& msg); |
| 33 | |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 34 | // Returns the D-Bus error object with error code and error message set. |
| 35 | static brillo::ErrorPtr CreateInternal(const base::Location& location, |
| 36 | const std::string& code, |
| 37 | const std::string& message); |
| 38 | |
| 39 | // Add a dbus error to the error chain. |
| 40 | static void AddTo(brillo::ErrorPtr* error, |
| 41 | const base::Location& location, |
| 42 | const std::string& code, |
| 43 | const std::string& message); |
| 44 | |
| 45 | // Add an internal error to the error chain. |
| 46 | static void AddInternalTo(brillo::ErrorPtr* error, |
| 47 | const base::Location& location, |
| 48 | const std::string& code, |
| 49 | const std::string& message); |
| 50 | |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 51 | // Returns a string representation of D-Bus error object used to help logging. |
| 52 | static std::string ToString(const brillo::ErrorPtr& err); |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 53 | |
| 54 | // Returns the first error code of a chain of errors. |
| 55 | static std::string GetRootErrorCode(const brillo::ErrorPtr& error); |
| 56 | |
Jae Hoon Kim | e9c30f7 | 2021-03-12 16:26:00 -0800 | [diff] [blame] | 57 | // Returns the first error code of a chain of errors with a dlcservice error |
| 58 | // code. If no dlcservice error code is found, it returns |kErrorInternal|. |
| 59 | static std::string GetErrorCode(const brillo::ErrorPtr& error); |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 60 | |
| 61 | // Convert a chain of errors into a single error in the DBus domain. The first |
| 62 | // error in the chain which is in the DBus domain, will be returned. If no |
| 63 | // error is in the DBus domain, return |kErrorInternal|. |
| 64 | static void ConvertToDbusError(brillo::ErrorPtr* error); |
Jae Hoon Kim | 71bc9bc | 2020-07-14 11:12:43 -0700 | [diff] [blame] | 65 | |
| 66 | Error(const Error&) = delete; |
| 67 | Error& operator=(const Error&) = delete; |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | } // namespace dlcservice |
| 71 | |
| 72 | #endif // DLCSERVICE_ERROR_H_ |