errors.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. #include "errors.hpp"
  2. namespace svapi {
  3. int httpStatus(ErrCode code) {
  4. switch (code) {
  5. case ErrCode::BadRequest: return 400;
  6. case ErrCode::Unauthorized: return 401;
  7. case ErrCode::Forbidden: return 403;
  8. case ErrCode::NotFound: return 404;
  9. case ErrCode::Conflict: return 409;
  10. case ErrCode::Unprocessable: return 422;
  11. case ErrCode::TooManyRequests: return 429;
  12. case ErrCode::Unavailable: return 503;
  13. // A function that ran past its wall-time budget. 504 rather than 408:
  14. // nothing was slow about the client, the upstream we were waiting on
  15. // did not answer. Nothing but function dispatch produces this.
  16. case ErrCode::GatewayTimeout: return 504;
  17. case ErrCode::Internal: return 500;
  18. }
  19. return 500;
  20. }
  21. nlohmann::json errorBody(const std::string& code, const std::string& message,
  22. const nlohmann::json& details) {
  23. nlohmann::json err{{"code", code}, {"message", message}};
  24. if (!details.is_null() && !details.empty()) err["details"] = details;
  25. return nlohmann::json{{"error", err}};
  26. }
  27. } // namespace svapi