Migrate to error constants in glacier
This commit is contained in:
parent
3ab9b4d818
commit
0b86a94f14
30 changed files with 171 additions and 114 deletions
39
lib/glacier/status/error.h
Normal file
39
lib/glacier/status/error.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace glcr {
|
||||
|
||||
typedef uint64_t ErrorCode;
|
||||
|
||||
#define RET_ERR(expr) \
|
||||
{ \
|
||||
z_err_t _tmp_err = expr; \
|
||||
if (_tmp_err != glcr::OK) { \
|
||||
return _tmp_err; \
|
||||
} \
|
||||
}
|
||||
|
||||
static const uint64_t OK = 0x0;
|
||||
|
||||
// First set of error codes generally indicate user errors.
|
||||
static const uint64_t INVALID_ARGUMENT = 0x1;
|
||||
static const uint64_t NOT_FOUND = 0x2;
|
||||
static const uint64_t PERMISSION_DENIED = 0x3;
|
||||
static const uint64_t NULL_PTR = 0x4;
|
||||
static const uint64_t EMPTY = 0x5;
|
||||
static const uint64_t ALREADY_EXISTS = 0x6;
|
||||
static const uint64_t BUFFER_SIZE = 0x7;
|
||||
static const uint64_t FAILED_PRECONDITION = 0x8;
|
||||
|
||||
// Second set of error codes generally indicate service errors.
|
||||
static const uint64_t INTERNAL = 0x100;
|
||||
static const uint64_t UNIMPLEMENTED = 0x101;
|
||||
static const uint64_t EXHAUSTED = 0x102;
|
||||
|
||||
// Kernel specific error codes (relating to capabilities).
|
||||
static const uint64_t CAP_NOT_FOUND = 0x1000;
|
||||
static const uint64_t CAP_WRONG_TYPE = 0x1001;
|
||||
static const uint64_t CAP_PERMISSION_DENIED = 0x1002;
|
||||
|
||||
} // namespace glcr
|
||||
41
lib/glacier/status/error_or.h
Normal file
41
lib/glacier/status/error_or.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error.h>
|
||||
|
||||
namespace glcr {
|
||||
|
||||
template <typename T>
|
||||
class ErrorOr {
|
||||
public:
|
||||
ErrorOr() = delete;
|
||||
ErrorOr(const ErrorOr&) = delete;
|
||||
ErrorOr(ErrorOr&&) = delete;
|
||||
|
||||
ErrorOr(ErrorCode code) : error_(code), ok_(false) {}
|
||||
ErrorOr(T obj) : obj_(obj), ok_(true) {}
|
||||
ErrorOr(T&& obj) : obj_(obj), ok_(true) {}
|
||||
|
||||
bool ok() { return ok_; }
|
||||
operator bool() { return ok_; }
|
||||
|
||||
T& value() { return obj_; }
|
||||
|
||||
ErrorCode error() { return error_; }
|
||||
|
||||
private:
|
||||
union {
|
||||
ErrorCode error_;
|
||||
T obj_;
|
||||
};
|
||||
bool ok_;
|
||||
};
|
||||
|
||||
#define ASSIGN_OR_RETURN(lhs, rhs) \
|
||||
\
|
||||
auto e##__LINE__ = rhs; \
|
||||
if (!e##__LINE__.ok()) { \
|
||||
return e##__LINE__.error(); \
|
||||
} \
|
||||
lhs = rhs.value();
|
||||
|
||||
} // namespace glcr
|
||||
Loading…
Add table
Add a link
Reference in a new issue