Class: Shift::Api::Core::Middleware::ErrorHandler

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/shift/api/core/middleware/error_handler.rb

Overview

Middleware to translate json api client errors to Shift::Api::Core errors

This is so as to not expose json api client stuff to the outside world

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



11
12
13
# File 'lib/shift/api/core/middleware/error_handler.rb', line 11

def initialize(app)
  self.app = app
end

Instance Method Details

#call(env) ⇒ Object

Executes as normal but catches jsonapi client errors and translates them

Parameters:

  • env (Faraday::Env)

    The environment from faraday



17
18
19
20
21
# File 'lib/shift/api/core/middleware/error_handler.rb', line 17

def call(env)
  app.call(env)
rescue JsonApiClient::Errors::ApiError => ex
  raise ex.class.name.gsub(/^JsonApiClient::/, "Shift::Api::Core::").constantize.from_jsonapi_client(ex)
end