1#pragma once2#include <stdexcept>3#include <string>4#include <string_view>56namespace raid {78enum class ErrorCode {9 invalid_argument, data_quality, numerical, not_converged, unsupported, io, api, insufficient_samples, memory, cancelled10};11constexpr std::string_view to_string(ErrorCode c) {12 switch (c) {13 case ErrorCode::invalid_argument: return "invalid_argument"; case ErrorCode::data_quality: return "data_quality";14 case ErrorCode::numerical: return "numerical"; case ErrorCode::not_converged: return "not_converged";15 case ErrorCode::unsupported: return "unsupported"; case ErrorCode::io: return "io"; case ErrorCode::api: return "api";16 case ErrorCode::insufficient_samples: return "insufficient_samples"; case ErrorCode::memory: return "memory";17 case ErrorCode::cancelled: return "cancelled";18 }19 return "?";20}2122class RaidError : public std::runtime_error {23 public:24 RaidError(ErrorCode code, const std::string& msg) : std::runtime_error(std::string(to_string(code)) + ": " + msg), code_(code) {}25 ErrorCode code() const noexcept { return code_; }26 private:27 ErrorCode code_;28};2930#define RAID_REQUIRE(cond, code, msg) \31 do { if (!(cond)) throw ::raid::RaidError(::raid::ErrorCode::code, std::string(msg) + " [" #cond "]"); } while (0)3233} // namespace raid34