Class: Startback::Web::Shield

Inherits:
Rack::Robustness
  • Object
show all
Includes:
Errors
Defined in:
lib/startback/web/shield.rb

Overview

This Rack middleware catches all known exceptions raised by sublayers in the Rack chain. Those exceptions are converted to proper HTTP error codes and friendly error messages encoded in json.

Please check the Errors module about status codes used for each Startback error.

This class aims at being used as top level of a Rack chain.

Examples:

Rack::Builder.new do
  use Startback::Web::Shield
end

Instance Method Summary collapse

Methods included from Errors

bad_request_error!, conflict_error!, expectation_failed_error!, forbidden_error!, gone_error!, internal_server_error!, locked_error!, method_not_allowed_error!, not_acceptable_error!, not_found_error!, not_implemented_error!, precondition_failed_error!, precondition_required_error!, server_error!, unauthorized_error!, unsupported_media_type_error!, user_error!

Instance Method Details

#body_for(ex) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/startback/web/shield.rb', line 40

def body_for(ex)
  ex = ex.root_cause if ex.is_a?(Finitio::TypeError)
  body = { code: ex.class.name, description: ex.message }
  body[:location] = ex.location if ex.is_a?(Finitio::TypeError)
  return body unless ex.is_a?(Startback::Errors::Error)
  return body unless ex.has_causes?

  body[:causes] = ex.causes
    .filter{|cause|
      cause.is_a?(Startback::Errors::Error)
    }
    .map{|cause|
      body_for(cause)
    }
  body
end