Class: PuavoRest::SuppressJSONError

Inherits:
Object
  • Object
show all
Defined in:
middleware/suppress_json_errors.rb

Overview

Middleware to suppress Execeptions inherited from JSONError

Instance Method Summary (collapse)

Constructor Details

- (SuppressJSONError) initialize(app)

Returns a new instance of SuppressJSONError



6
7
8
# File 'middleware/suppress_json_errors.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

- (Object) call(env)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'middleware/suppress_json_errors.rb', line 10

def call(env)
  begin
    @app.call(env)
  rescue JSONError => err
    if ENV["RACK_ENV"] != "test"

      message = "#{ err }\n#{ err.backtrace.join("\n") }"
      if STDIN.tty?
        STDERR.puts message.colorize(:red)
      else
        STDERR.puts message
      end
    end

    [err.http_code, err.headers, [err.to_json]]
  end
end