| 12345678910111213141516171819202122232425262728 |
- #pragma once
- #include <stdexcept>
- #include <string>
- #include <nlohmann/json.hpp>
- namespace svapi {
- enum class ErrCode { BadRequest, Unauthorized, Forbidden, NotFound, Conflict,
- Unprocessable, TooManyRequests, Unavailable, Internal };
- int httpStatus(ErrCode code);
- /// `details` is merged into the error object as `error.details` when non-empty.
- /// Used for machine-readable payloads (colliding ids, relation impacts) that
- /// would otherwise be flattened into the message string.
- nlohmann::json errorBody(const std::string& code, const std::string& message,
- const nlohmann::json& details = nlohmann::json::object());
- struct ApiError : std::runtime_error {
- ErrCode code;
- std::string slug;
- nlohmann::json details; // optional structured payload; empty object = omitted
- ApiError(ErrCode c, std::string slug_, const std::string& msg,
- nlohmann::json det = nlohmann::json::object())
- : std::runtime_error(msg), code(c), slug(std::move(slug_)), details(std::move(det)) {}
- };
- } // namespace svapi
|