| // Copyright 2018 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef COBALT_SRC_PUBLIC_LIB_STATUS_H_ |
| #define COBALT_SRC_PUBLIC_LIB_STATUS_H_ |
| |
| #include <ostream> |
| #include <sstream> |
| #include <string> |
| |
| #include "src/public/lib/status_codes.h" |
| |
| namespace cobalt { |
| |
| class Status { |
| public: |
| Status() = default; |
| |
| Status(StatusCode code, std::string error_message) |
| : code_(code), error_message_(std::move(error_message)) {} |
| |
| Status(StatusCode code, std::string error_message, std::string error_details) |
| : code_(code), |
| error_message_(std::move(error_message)), |
| error_details_(std::move(error_details)) {} |
| |
| // ToString converts the Status to a human-readable string. |
| [[nodiscard]] inline std::string ToString() const; |
| |
| [[nodiscard]] StatusCode error_code() const { return code_; } |
| [[nodiscard]] std::string error_message() const { return error_message_; } |
| [[nodiscard]] std::string error_details() const { return error_details_; } |
| |
| [[nodiscard]] bool ok() const { return code_ == StatusCode::OK; } |
| |
| // Ignores any errors. This method does nothing except potentially suppress |
| // complaints from any tools that are checking that errors are not dropped on |
| // the floor. |
| void IgnoreError() {} |
| |
| inline static Status OkStatus() { return Status(); } |
| // DEPRECATED: Use OkStatus() instead |
| // TODO(fxbug.dev/87583): Delete this once it is no longer referenced. |
| static const Status OK; |
| |
| private: |
| StatusCode code_ = StatusCode::OK; |
| std::string error_message_; |
| std::string error_details_; |
| }; |
| |
| // Implementation of the steam operator for printing StatusCodes. |
| std::ostream &operator<<(std::ostream &os, const Status &status); |
| |
| } // namespace cobalt |
| |
| #endif // COBALT_SRC_PUBLIC_LIB_STATUS_H_ |