class SmartlingApi::Errors::RaiseError

Middleware for Faraday error handling

Constants

ERRORS

Mapping of Smartling Error response

Public Instance Methods

on_complete(response) click to toggle source

Hook for faraday middleware to respond to errors. Will raise mapped error, but will default to using Errors::Client.

@param response [Faraday::Response] Response object passed from middleware @return [nil] If no errors @raise [Errors::Client] Specific error raised depending on response status code.

# File lib/smartling_api/errors/raise_error.rb, line 25
def on_complete(response)
  return if response.status < 400

  raise ERRORS.fetch(response.status, Client), get_message(response)
end

Private Instance Methods

get_message(response) click to toggle source
# File lib/smartling_api/errors/raise_error.rb, line 33
def get_message(response)
  return "" if response.body.nil? || response.body.empty?

  errors = JSON.parse(response.body).fetch("response", {}).fetch("errors", {})

  errors.map { |error| error["message"] }.join(", ")
end