Class: Chip::ErrorMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/chip/error_middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/chip/error_middleware.rb', line 7

def body
  @body
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/chip/error_middleware.rb', line 7

def status
  @status
end

Instance Method Details

#on_complete(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/chip/error_middleware.rb', line 9

def on_complete(env)
  return if env.success?

  @status = env[:status].to_i
  @body = env.body
  case status
  when 400..600
    raise Chip::ServiceException.new("CHIP_#{status}", response_values(body), status, body)
  end
end

#response_values(body = {}) ⇒ Object (private)



22
23
24
25
26
27
28
# File 'lib/chip/error_middleware.rb', line 22

def response_values(body = {})
  {
    status:,
    detail: [JSON.parse(body)],
    code: "CHIP_#{status}"
  }
end