MapErrorToHTTPStatus maps APIError codes to appropriate HTTP status codes. It returns the corresponding HTTP status code for the given APIError.
(err error)
| 64 | // MapErrorToHTTPStatus maps APIError codes to appropriate HTTP status codes. |
| 65 | // It returns the corresponding HTTP status code for the given APIError. |
| 66 | func MapErrorToHTTPStatus(err error) int { |
| 67 | if apiErr, ok := err.(APIError); ok { |
| 68 | switch apiErr.Code { |
| 69 | case ErrNotFound: |
| 70 | return http.StatusNotFound // HTTP 404 Not Found for missing resources. |
| 71 | case ErrConflict: |
| 72 | return http.StatusConflict // HTTP 409 Conflict for conflicting requests. |
| 73 | case ErrInvalidInput: |
| 74 | return http.StatusBadRequest // HTTP 400 Bad Request for invalid inputs. |
| 75 | case ErrInternalServer: |
| 76 | return http.StatusInternalServerError // HTTP 500 Internal Server Error for server issues. |
| 77 | case ErrRateLimited: |
| 78 | return http.StatusTooManyRequests // HTTP 429 Too Many Requests for rate limiting. |
| 79 | default: |
| 80 | return http.StatusInternalServerError |
| 81 | } |
| 82 | } |
| 83 | return http.StatusInternalServerError // Default to 500 Internal Server Error if no specific mapping is found. |
| 84 | } |
no outgoing calls