Module: Deepgram::ResponseHandler

Included in:
Base
Defined in:
lib/deepgram/response_handler.rb

Overview

The ResponseHandler module provides a method for handling responses from the Deepgram API and raising appropriate exceptions based on the HTTP status code.

Instance Method Summary collapse

Instance Method Details

#handle_response(res) ⇒ Faraday::Response

Handles the response from the Deepgram API.

Parameters:

  • res (Faraday::Response)

    The response object from the API request.

Returns:

  • (Faraday::Response)

    The response object if the status code is between 200 and 226.

Raises:

  • (Deepgram::BadRequestError)

    If the response has a 400 status code.

  • (Deepgram::UnauthorizedError)

    If the response has a 401 status code.

  • (Deepgram::ForbiddenError)

    If the response has a 403 status code.

  • (Deepgram::NotFoundError)

    If the response has a 404 status code.

  • (Deepgram::RateLimitExceededError)

    If the response has a 429 status code.

  • (Deepgram::InternalServerErrorError)

    If the response has a 500 status code.

  • (Deepgram::BadGatewayError)

    If the response has a 502 status code.

  • (Deepgram::ServiceUnavailableError)

    If the response has a 503 status code.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/deepgram/response_handler.rb', line 24

def handle_response(res) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
  case res.status
  when 200..226 then res
  when 400 then raise Deepgram::BadRequest.new(res.body, code: res.status, headers: res.headers)
  when 401 then raise Deepgram::Unauthorized.new(res.body, code: res.status, headers: res.headers)
  when 403 then raise Deepgram::Forbidden.new(res.body, code: res.status, headers: res.headers)
  when 404 then raise Deepgram::NotFound.new(res.body, code: res.status, headers: res.headers)
  when 429 then raise Deepgram::RateLimitExceeded.new(res.body, code: res.status, headers: res.headers)
  when 500 then raise Deepgram::InternalServerError.new(res.body, code: res.status, headers: res.headers)
  when 502 then raise Deepgram::BadGateway.new(res.body, code: res.status, headers: res.headers)
  when 503 then raise Deepgram::ServiceUnavailable.new(res.body, code: res.status, headers: res.headers)
  end
end