From 83d606a2c79e3a8d83904a905bed944ce0e6eacd Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Tue, 28 Jun 2022 16:01:39 +0200 Subject: [PATCH] Error: Add error codes to ErrorOr<> and add constructors This allows us to handle functions that can fail in multiple different ways --- src/lib/error.h++ | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/lib/error.h++ b/src/lib/error.h++ index e60af52..a675363 100644 --- a/src/lib/error.h++ +++ b/src/lib/error.h++ @@ -15,22 +15,59 @@ #pragma once +#include + template struct ErrorOr { bool isError; + uint8_t errorCode; T value; ErrorOr(); ErrorOr(T); + ErrorOr(bool); + ErrorOr(bool, uint8_t); + ErrorOr(bool, uint8_t, T); }; template ErrorOr::ErrorOr() { this->isError = false; + this->errorCode = 0; } template ErrorOr::ErrorOr(T value) { this->isError = false; + this->errorCode = 0; this->value = value; } + +template +ErrorOr::ErrorOr(bool isError) { + this->isError = isError; + this->errorCode = 0; +} + +template +ErrorOr::ErrorOr(bool isError, uint8_t errorCode) { + this->isError = isError; + this->errorCode = errorCode; +} + +template +ErrorOr::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; +}