Skip to content

Commit

Permalink
Add error handling for different status codes in translation API
Browse files Browse the repository at this point in the history
  • Loading branch information
kojix2 committed Mar 19, 2024
1 parent 7e48951 commit b9b7951
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/deepl/translator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ module Deepl
form.add("glossary_id", glossary_id) if glossary_id
end
response = execute_post_request(API_URL_TRANSLATE, params, http_headers_for_text)
case response.status_code
when 456
raise RequestError.new("Quota exceeded")
when HTTP::Status::FORBIDDEN
raise RequestError.new("Authorization failed")
when HTTP::Status::NOT_FOUND
raise RequestError.new("Not found")
when HTTP::Status::BAD_REQUEST
raise RequestError.new("Bad request")
when HTTP::Status::TOO_MANY_REQUESTS
raise RequestError.new("Too many requests")
when HTTP::Status::SERVICE_UNAVAILABLE
raise RequestError.new("Service unavailable")
end
parsed_response = JSON.parse(response.body)
begin
parsed_response.dig("translations", 0, "text")
Expand Down

0 comments on commit b9b7951

Please sign in to comment.