Browse Source

feat(errors): add 429 TooManyRequests

Fszontagh 1 month ago
parent
commit
741a8dcacd
3 changed files with 8 additions and 3 deletions
  1. 3 2
      src/errors.cpp
  2. 1 1
      src/errors.hpp
  3. 4 0
      tests/test_errors.cpp

+ 3 - 2
src/errors.cpp

@@ -8,8 +8,9 @@ int httpStatus(ErrCode code) {
         case ErrCode::Unauthorized:  return 401;
         case ErrCode::Forbidden:     return 403;
         case ErrCode::NotFound:      return 404;
-        case ErrCode::Unprocessable: return 422;
-        case ErrCode::Unavailable:   return 503;
+        case ErrCode::Unprocessable:    return 422;
+        case ErrCode::TooManyRequests:  return 429;
+        case ErrCode::Unavailable:      return 503;
         case ErrCode::Internal:      return 500;
     }
     return 500;

+ 1 - 1
src/errors.hpp

@@ -5,7 +5,7 @@
 
 namespace svapi {
 
-enum class ErrCode { BadRequest, Unauthorized, Forbidden, NotFound, Unprocessable, Unavailable, Internal };
+enum class ErrCode { BadRequest, Unauthorized, Forbidden, NotFound, Unprocessable, TooManyRequests, Unavailable, Internal };
 
 int httpStatus(ErrCode code);
 nlohmann::json errorBody(const std::string& code, const std::string& message);

+ 4 - 0
tests/test_errors.cpp

@@ -26,3 +26,7 @@ TEST(Errors, ApiErrorCarriesFields) {
 }
 
 TEST(Errors, ForbiddenIs403) { EXPECT_EQ(httpStatus(svapi::ErrCode::Forbidden), 403); }
+
+TEST(Errors, TooManyRequestsMapsTo429) {
+    EXPECT_EQ(svapi::httpStatus(svapi::ErrCode::TooManyRequests), 429);
+}