blob: 4e90c2b5a29405aa2d13f37b26a9b7a7b3db2f02 [file] [log] [blame]
Alex Vakulenkob3aac252014-05-07 17:35:24 -07001// Copyright 2014 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 BUFFET_ERROR_H_
6#define BUFFET_ERROR_H_
7
8#include <memory>
9#include <string>
10
11#include <base/basictypes.h>
12
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070013namespace buffet {
Alex Vakulenkob3aac252014-05-07 17:35:24 -070014
15class Error; // Forward declaration.
16
17typedef std::unique_ptr<Error> ErrorPtr;
18
19class Error {
20 public:
21 virtual ~Error() = default;
22
23 // Creates an instance of Error class.
24 static ErrorPtr Create(const std::string& domain, const std::string& code,
25 const std::string& message);
26 static ErrorPtr Create(const std::string& domain, const std::string& code,
27 const std::string& message, ErrorPtr inner_error);
28 // If |error| is not nullptr, creates another instance of Error class,
29 // initializes it with specified arguments and adds it to the head of
30 // the error chain pointed to by |error|.
31 static void AddTo(ErrorPtr* error, const std::string& domain,
32 const std::string& code, const std::string& message);
Alex Vakulenko96c84d32014-06-06 11:07:32 -070033 // Same as the Error::AddTo above, but allows to pass in a printf-like
34 // format string and optional parameters to format the error message.
35 static void AddToPrintf(ErrorPtr* error, const std::string& domain,
36 const std::string& code,
37 const char* format, ...) PRINTF_FORMAT(4, 5);
Alex Vakulenkob3aac252014-05-07 17:35:24 -070038
39 // Returns the error domain, code and message
40 const std::string& GetDomain() const { return domain_; }
41 const std::string& GetCode() const { return code_; }
42 const std::string& GetMessage() const { return message_; }
43
44 // Checks if this or any of the inner error in the chain has the specified
45 // error domain.
46 bool HasDomain(const std::string& domain) const;
47 // Checks if this or any of the inner error in the chain matches the specified
48 // error domain and code.
49 bool HasError(const std::string& domain, const std::string& code) const;
50
51 // Gets a pointer to the inner error, if present. Returns nullptr otherwise.
52 const Error* GetInnerError() const { return inner_error_.get(); }
53
Alex Vakulenko96c84d32014-06-06 11:07:32 -070054 // Gets a pointer to the first error occurred.
55 // Returns itself if no inner error are available.
56 const Error* GetFirstError() const;
57
Alex Vakulenkob3aac252014-05-07 17:35:24 -070058 protected:
59 // Constructor is protected since this object is supposed to be
60 // created via the Create factory methods.
61 Error(const std::string& domain, const std::string& code,
62 const std::string& message, ErrorPtr inner_error);
63
64 // Error domain. The domain defines the scopes for error codes.
65 // Two errors with the same code but different domains are different errors.
66 std::string domain_;
67 // Error code. A unique error code identifier within the given domain.
68 std::string code_;
69 // Human-readable error message.
70 std::string message_;
71 // Pointer to inner error, if any. This forms a chain of errors.
72 ErrorPtr inner_error_;
73
74 private:
75 DISALLOW_COPY_AND_ASSIGN(Error);
76};
77
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070078} // namespace buffet
Alex Vakulenkob3aac252014-05-07 17:35:24 -070079
80#endif // BUFFET_ERROR_H_