blob: f1eaa811d94c694e2e1bfcacd7413bc4f16a8d94 [file] [log] [blame]
Jae Hoon Kimc98e3a32020-03-13 09:40:48 -07001// 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
Andrew0a534ed2020-05-06 09:59:17 -07008#include <map>
Jae Hoon Kimc98e3a32020-03-13 09:40:48 -07009#include <string>
Andrew0a534ed2020-05-06 09:59:17 -070010#include <utility>
11#include <vector>
Jae Hoon Kimc98e3a32020-03-13 09:40:48 -070012
13#include <brillo/errors/error.h>
14
15namespace dlcservice {
16
Andrew0a534ed2020-05-06 09:59:17 -070017extern const char kDlcErrorDomain[];
18
19namespace error {
20extern const char kFailedToCreateDirectory[];
21extern const char kFailedInstallInUpdateEngine[];
22extern const char kFailedInternal[];
23extern const char kFailedToVerifyImage[];
24extern const char kFailedToMountImage[];
25}; // namespace error
26
Jae Hoon Kimc98e3a32020-03-13 09:40:48 -070027class Error {
28 public:
29 // Returns the D-Bus error object with error code and error message set.
Andrew9aa06442020-05-06 10:33:46 -070030 static brillo::ErrorPtr Create(const base::Location& location,
31 const std::string& code,
Jae Hoon Kimc98e3a32020-03-13 09:40:48 -070032 const std::string& msg);
33
Andrew0a534ed2020-05-06 09:59:17 -070034 // 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 Kimc98e3a32020-03-13 09:40:48 -070051 // Returns a string representation of D-Bus error object used to help logging.
52 static std::string ToString(const brillo::ErrorPtr& err);
Andrew0a534ed2020-05-06 09:59:17 -070053
54 // Returns the first error code of a chain of errors.
55 static std::string GetRootErrorCode(const brillo::ErrorPtr& error);
56
Jae Hoon Kime9c30f72021-03-12 16:26:00 -080057 // 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);
Andrew0a534ed2020-05-06 09:59:17 -070060
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 Kim71bc9bc2020-07-14 11:12:43 -070065
66 Error(const Error&) = delete;
67 Error& operator=(const Error&) = delete;
Jae Hoon Kimc98e3a32020-03-13 09:40:48 -070068};
69
70} // namespace dlcservice
71
72#endif // DLCSERVICE_ERROR_H_