Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 1 | // 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 Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 13 | namespace buffet { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 14 | |
| 15 | class Error; // Forward declaration. |
| 16 | |
| 17 | typedef std::unique_ptr<Error> ErrorPtr; |
| 18 | |
| 19 | class 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 Vakulenko | 96c84d3 | 2014-06-06 11:07:32 -0700 | [diff] [blame] | 33 | // 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 Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 38 | |
| 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 Vakulenko | 96c84d3 | 2014-06-06 11:07:32 -0700 | [diff] [blame] | 54 | // Gets a pointer to the first error occurred. |
| 55 | // Returns itself if no inner error are available. |
| 56 | const Error* GetFirstError() const; |
| 57 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 58 | 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 Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 78 | } // namespace buffet |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 79 | |
| 80 | #endif // BUFFET_ERROR_H_ |