Error: Add error codes to ErrorOr<> and add constructors

This allows us to handle functions that can fail in multiple different ways
BodgeMaster-unfinished
BodgeMaster 2022-06-28 16:01:39 +02:00
parent 081035db32
commit 83d606a2c7
1 changed files with 37 additions and 0 deletions

View File

@ -15,22 +15,59 @@
#pragma once #pragma once
#include <cstdint>
template <typename T> template <typename T>
struct ErrorOr { struct ErrorOr {
bool isError; bool isError;
uint8_t errorCode;
T value; T value;
ErrorOr<T>(); ErrorOr<T>();
ErrorOr<T>(T); ErrorOr<T>(T);
ErrorOr<T>(bool);
ErrorOr<T>(bool, uint8_t);
ErrorOr<T>(bool, uint8_t, T);
}; };
template <typename T> template <typename T>
ErrorOr<T>::ErrorOr() { ErrorOr<T>::ErrorOr() {
this->isError = false; this->isError = false;
this->errorCode = 0;
} }
template <typename T> template <typename T>
ErrorOr<T>::ErrorOr(T value) { ErrorOr<T>::ErrorOr(T value) {
this->isError = false; this->isError = false;
this->errorCode = 0;
this->value = value; this->value = value;
} }
template <typename T>
ErrorOr<T>::ErrorOr(bool isError) {
this->isError = isError;
this->errorCode = 0;
}
template <typename T>
ErrorOr<T>::ErrorOr(bool isError, uint8_t errorCode) {
this->isError = isError;
this->errorCode = errorCode;
}
template <typename T>
ErrorOr<T>::ErrorOr(bool isError, uint8_t errorCode, T value) {
this->isError = isError;
this->errorCode = errorCode;
this->value = value;
}
namespace ErrorCodes {
// These are all arbitrary values used to assign error codes to different
// kinds of errors.
// Using them is optional as ErrorOr<> accepts any uint8_t value as
// error code, but they are useful for readability.
// IndexOutOfRangeException equivalent
const uint8_t RANGE_ERROR = 1;
}