62 lines
1.5 KiB
C++
62 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <ckitty/memory/primitives.hpp>
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
|
|
namespace ckitty {
|
|
|
|
struct error {
|
|
|
|
i32 code;
|
|
std::string message;
|
|
|
|
error(i32 code = 0, const std::string& msg = "")
|
|
: code(code), message(msg) {
|
|
}
|
|
|
|
operator bool() const {
|
|
return code;
|
|
}
|
|
|
|
operator std::string() const {
|
|
std::ostringstream oss;
|
|
oss << "[" << std::hex << std::showbase << std::uppercase << std::setw(2) << std::setfill('0') << code << "] ";
|
|
oss << (message.empty() ? "(no info)" : message);
|
|
return oss.str();
|
|
}
|
|
|
|
error& operator+=(const error& err) {
|
|
auto err1 = (*this) + err;
|
|
code = err1.code;
|
|
message = err1.message;
|
|
return *this;
|
|
}
|
|
|
|
error operator+(const error& err) const {
|
|
// If this error has no code (no error), return the other error
|
|
if (code == 0) {
|
|
return err;
|
|
}
|
|
|
|
// If the other error has no code, return this error
|
|
if (err.code == 0) {
|
|
return *this;
|
|
}
|
|
|
|
// Both errors have codes, combine them
|
|
error result = *this;
|
|
|
|
// Combine messages
|
|
if (!result.message.empty()) {
|
|
result.message += "\n ";
|
|
}
|
|
|
|
// add next error to the message
|
|
result.message += err.operator std::string();
|
|
return result;
|
|
}
|
|
|
|
};
|
|
|
|
} |