James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 1 | #include "aos/util/status.h" |
| 2 | |
| 3 | namespace aos { |
James Kuszmaul | 053d42a | 2024-05-30 16:46:08 -0700 | [diff] [blame^] | 4 | namespace { |
| 5 | // Constructs a string view from the provided buffer if it has data and |
| 6 | // otherwise uses the provided string view. Used in copy/move constructors to |
| 7 | // figure out whether we should use the buffer or keep the pointer to the |
| 8 | // existing std::string_view (as is the case for when we store a pointer to a |
| 9 | // string literal). |
| 10 | static std::string_view MakeStringViewFromBufferOrView( |
| 11 | const aos::InlinedVector<char, Error::kStaticMessageLength> &buffer, |
| 12 | const std::string_view &view) { |
| 13 | return (buffer.size() > 0) ? std::string_view(buffer.begin(), buffer.end()) |
| 14 | : view; |
| 15 | } |
| 16 | } // namespace |
| 17 | Error::Error(StatusCode code, std::string_view message, |
| 18 | std::optional<std::source_location> source_location) |
James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 19 | : code_(code), |
| 20 | owned_message_(message.begin(), message.end()), |
| 21 | message_(owned_message_.data(), owned_message_.size()), |
| 22 | source_location_(std::move(source_location)) {} |
James Kuszmaul | 053d42a | 2024-05-30 16:46:08 -0700 | [diff] [blame^] | 23 | Error::Error(StatusCode code, const char *message, |
| 24 | std::optional<std::source_location> source_location) |
James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 25 | : code_(code), |
| 26 | message_(message), |
| 27 | source_location_(std::move(source_location)) {} |
| 28 | |
James Kuszmaul | 053d42a | 2024-05-30 16:46:08 -0700 | [diff] [blame^] | 29 | Error::Error(Error &&other) |
James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 30 | : code_(other.code_), |
| 31 | owned_message_(std::move(other.owned_message_)), |
| 32 | message_(MakeStringViewFromBufferOrView(owned_message_, other.message_)), |
| 33 | source_location_(std::move(other.source_location_)) { |
| 34 | // Because the internal string view contains a pointer to the owned_message_ |
| 35 | // buffer, we need to have a manually written move constructor to manage it. |
| 36 | other.message_ = {}; |
| 37 | } |
James Kuszmaul | 053d42a | 2024-05-30 16:46:08 -0700 | [diff] [blame^] | 38 | Error &Error::operator=(Error &&other) { |
James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 39 | std::swap(*this, other); |
| 40 | return *this; |
| 41 | } |
James Kuszmaul | 053d42a | 2024-05-30 16:46:08 -0700 | [diff] [blame^] | 42 | Error::Error(const Error &other) |
James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 43 | : code_(other.code_), |
| 44 | owned_message_(other.owned_message_), |
| 45 | message_(MakeStringViewFromBufferOrView(owned_message_, other.message_)), |
| 46 | source_location_(other.source_location_) {} |
| 47 | |
James Kuszmaul | 053d42a | 2024-05-30 16:46:08 -0700 | [diff] [blame^] | 48 | std::string Error::ToString() const { |
James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 49 | std::string source_info = ""; |
| 50 | if (source_location_.has_value()) { |
| 51 | source_info = absl::StrFormat( |
| 52 | "%s:%d in %s: ", source_location_->file_name(), |
| 53 | source_location_->line(), source_location_->function_name()); |
| 54 | } |
| 55 | |
James Kuszmaul | 053d42a | 2024-05-30 16:46:08 -0700 | [diff] [blame^] | 56 | return absl::StrFormat("%sErrored with code of %d and message: %s", |
| 57 | source_info, code(), message()); |
James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | template <> |
James Kuszmaul | 053d42a | 2024-05-30 16:46:08 -0700 | [diff] [blame^] | 61 | void CheckExpected<void>(const Result<void> &expected) { |
James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 62 | if (expected.has_value()) { |
| 63 | return; |
| 64 | } |
| 65 | LOG(FATAL) << expected.error().ToString(); |
| 66 | } |
James Kuszmaul | 053d42a | 2024-05-30 16:46:08 -0700 | [diff] [blame^] | 67 | |
| 68 | int ResultExitCode(const Result<void> &expected) { |
| 69 | return expected.has_value() ? static_cast<int>(Error::StatusCode::kOk) |
| 70 | : expected.error().code(); |
| 71 | } |
James Kuszmaul | 847927d | 2024-05-23 15:33:18 -0700 | [diff] [blame] | 72 | } // namespace aos |