Sha256: 1ff91470e4ea9f71d192974c4db9324de8f2347eba654e5888fc9ee788c91d24

Contents?: true

Size: 1.26 KB

Versions: 24

Compression:

Stored size: 1.26 KB

Contents

module Startback
  module Web
    #
    # 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
    #
    class Shield < Rack::Robustness
      include Errors

      self.no_catch_all
      self.content_type 'application/json'

      # Decoding errors from json and csv are considered user's fault
      self.on(Finitio::TypeError){ 400 }
      self.on(::NotImplementedError){ 501 }

      # Various other codes for the framework specific error classes
      self.on(Startback::Errors::Error) {|ex|
        ex.class.status
      }

      # A bit of logic to choose the best error message for the user
      # according to the error class
      self.body{|ex|
        ex = ex.root_cause if ex.is_a?(Finitio::TypeError)
        { code: ex.class.name, description: ex.message }.to_json
      }

    end # class Shield
  end # module Web
end # module Startback

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
startback-0.10.0 lib/startback/web/shield.rb
startback-0.9.1 lib/startback/web/shield.rb
startback-0.9.0 lib/startback/web/shield.rb
startback-0.8.3 lib/startback/web/shield.rb
startback-0.8.2 lib/startback/web/shield.rb
startback-0.8.1 lib/startback/web/shield.rb
startback-0.8.0 lib/startback/web/shield.rb
startback-0.7.6 lib/startback/web/shield.rb
startback-0.7.5 lib/startback/web/shield.rb
startback-0.7.4 lib/startback/web/shield.rb
startback-0.7.3 lib/startback/web/shield.rb
startback-0.7.2 lib/startback/web/shield.rb
startback-0.7.1 lib/startback/web/shield.rb
startback-0.7.0 lib/startback/web/shield.rb
startback-0.6.0 lib/startback/web/shield.rb
startback-0.5.5 lib/startback/web/shield.rb
startback-0.5.4 lib/startback/web/shield.rb
startback-0.5.3 lib/startback/web/shield.rb
startback-0.5.2 lib/startback/web/shield.rb
startback-0.5.1 lib/startback/web/shield.rb