LightlyEdge C++ SDK
Loading...
Searching...
No Matches
lightly_edge_error.h
1/* © 2024 Lightly AI. All rights reserved. */
2
3#ifndef LIGHTLY_EDGE_ERROR_H
4#define LIGHTLY_EDGE_ERROR_H
5
6#include "tl/expected.hpp"
7#include <cstdint>
8#include <string>
9
10// Note: We add a namespace Doxygen comment so that documentation is generated
11// for functions outside classes.
16
20struct Error {
26 uint16_t code;
27
33 std::string message;
34
35 Error(uint16_t code, std::string message) : code(code), message(std::move(message)) {}
36};
37
44class Exception : public std::exception {
45private:
46 std::string message_;
47
48public:
49 explicit Exception(std::string message) : message_(std::move(message)) {}
50 [[nodiscard]] auto what() const noexcept -> const char * override {
51 return message_.c_str();
52 }
53};
54
78template <typename T>
79inline auto value_or_throw(tl::expected<T, lightly_edge_sdk::Error> &&result) -> T {
80 if (!result.has_value()) {
81 throw Exception(std::move(result).error().message);
82 }
83 return std::move(*result);
84}
85
86// Specialization of `value_or_throw` for `void` return type.
87template <>
88inline void value_or_throw(tl::expected<void, lightly_edge_sdk::Error> &&result) {
89 if (!result.has_value()) {
90 throw Exception(std::move(result).error().message);
91 }
92}
93
94} // namespace lightly_edge_sdk
95
96#endif // LIGHTLY_EDGE_ERROR_H
Exception class for lightly_edge_sdk::value_or_throw function.
Definition lightly_edge_error.h:44
Namespace with core LightlyEdge SDK functionality.
Definition lightly_edge_error.h:15
auto value_or_throw(tl::expected< T, lightly_edge_sdk::Error > &&result) -> T
Convenience function to unwrap a result of lightly_edge_sdk::LightlyEdge functions.
Definition lightly_edge_error.h:79
Error struct to hold error code and message.
Definition lightly_edge_error.h:20
std::string message
Error message.
Definition lightly_edge_error.h:33
uint16_t code
Error code.
Definition lightly_edge_error.h:26