#include "errors.hpp" namespace svapi { int httpStatus(ErrCode code) { switch (code) { case ErrCode::BadRequest: return 400; case ErrCode::Unauthorized: return 401; case ErrCode::Forbidden: return 403; case ErrCode::NotFound: return 404; case ErrCode::Conflict: return 409; case ErrCode::Unprocessable: return 422; case ErrCode::TooManyRequests: return 429; case ErrCode::Unavailable: return 503; // A function that ran past its wall-time budget. 504 rather than 408: // nothing was slow about the client, the upstream we were waiting on // did not answer. Nothing but function dispatch produces this. case ErrCode::GatewayTimeout: return 504; case ErrCode::Internal: return 500; } return 500; } nlohmann::json errorBody(const std::string& code, const std::string& message, const nlohmann::json& details) { nlohmann::json err{{"code", code}, {"message", message}}; if (!details.is_null() && !details.empty()) err["details"] = details; return nlohmann::json{{"error", err}}; } } // namespace svapi