blob: dbcd4af693ed53e365abe63cef39666b3439c656 [file] [log] [blame]
James Kuszmaul847927d2024-05-23 15:33:18 -07001#include "aos/util/status.h"
2
3namespace aos {
James Kuszmaul053d42a2024-05-30 16:46:08 -07004namespace {
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).
10static 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
17Error::Error(StatusCode code, std::string_view message,
18 std::optional<std::source_location> source_location)
James Kuszmaul847927d2024-05-23 15:33:18 -070019 : 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 Kuszmaul053d42a2024-05-30 16:46:08 -070023Error::Error(StatusCode code, const char *message,
24 std::optional<std::source_location> source_location)
James Kuszmaul847927d2024-05-23 15:33:18 -070025 : code_(code),
26 message_(message),
27 source_location_(std::move(source_location)) {}
28
James Kuszmaul053d42a2024-05-30 16:46:08 -070029Error::Error(Error &&other)
James Kuszmaul847927d2024-05-23 15:33:18 -070030 : 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 Kuszmaul053d42a2024-05-30 16:46:08 -070038Error &Error::operator=(Error &&other) {
James Kuszmaul847927d2024-05-23 15:33:18 -070039 std::swap(*this, other);
40 return *this;
41}
James Kuszmaul053d42a2024-05-30 16:46:08 -070042Error::Error(const Error &other)
James Kuszmaul847927d2024-05-23 15:33:18 -070043 : code_(other.code_),
44 owned_message_(other.owned_message_),
45 message_(MakeStringViewFromBufferOrView(owned_message_, other.message_)),
46 source_location_(other.source_location_) {}
47
James Kuszmaul053d42a2024-05-30 16:46:08 -070048std::string Error::ToString() const {
James Kuszmaul847927d2024-05-23 15:33:18 -070049 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 Kuszmaul053d42a2024-05-30 16:46:08 -070056 return absl::StrFormat("%sErrored with code of %d and message: %s",
57 source_info, code(), message());
James Kuszmaul847927d2024-05-23 15:33:18 -070058}
59
60template <>
James Kuszmaul053d42a2024-05-30 16:46:08 -070061void CheckExpected<void>(const Result<void> &expected) {
James Kuszmaul847927d2024-05-23 15:33:18 -070062 if (expected.has_value()) {
63 return;
64 }
65 LOG(FATAL) << expected.error().ToString();
66}
James Kuszmaul053d42a2024-05-30 16:46:08 -070067
68int ResultExitCode(const Result<void> &expected) {
69 return expected.has_value() ? static_cast<int>(Error::StatusCode::kOk)
70 : expected.error().code();
71}
James Kuszmaul847927d2024-05-23 15:33:18 -070072} // namespace aos