Class: UU::OS::Application::ErrorHandlingMiddleware

Inherits:
Server::AbstractMiddleware show all
Includes:
Util::ResponseBuilder
Defined in:
uu_os_application-server-2.7.3/lib/uu/os/application/error_handling_middleware.rb

Instance Method Summary (collapse)

Methods included from Util::ResponseBuilder

#handle_response, #response, #streamed_response

Constructor Details

- (ErrorHandlingMiddleware) initialize(app)

Returns a new instance of ErrorHandlingMiddleware



23
24
25
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/error_handling_middleware.rb', line 23

def initialize(app)
  super(app)
end

Instance Method Details

- (Object) call(env)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'uu_os_application-server-2.7.3/lib/uu/os/application/error_handling_middleware.rb', line 27

def call(env)
  begin
    return @app.call(env)


  rescue Exception => e
    request = UU::OS::Server::Request.new(env)
    log_error(request.logger, e)
    if raise_errors
      raise e
    end
    if e.kind_of? UU::OS::Server::ImmediateResponse
      return e.response
    end
    error_declared = false
    if e.kind_of?(UU::OS::Application::TimeoutError)
      error_declared = true
    else
      (declared_errors || []).each do |error_class|
        if e.kind_of?(error_class)
          error_declared = true
          break;
        end
      end
    end
    if error_declared
      # Declared error should be passed directly to client.
      status = (e.respond_to?(:kind) && e.kind == UU::OS::IPC::ErrorKind::CLIENT) ? 400 : 500
      return response(status, {content_type: 'application/json'}, e.to_json)
    else
      # Wrap undeclared error to common error.
      if UU::OS::Server::ServerContext.test_or_development_mode?
        common_error = UU::OS::Application::ApplicationError.new(UU::OS::IPC::ErrorKind::SERVER, UU::OS::Application::ErrorCodes::UNEXPECTED_PROBLEM,
                                                                 'Unexpected server error', e)
      else
        # Do not publish internal problem when not in development mode.
        common_error = UU::OS::Application::ApplicationError.new(UU::OS::IPC::ErrorKind::SERVER, UU::OS::Application::ErrorCodes::UNEXPECTED_PROBLEM,
                                                                 'Unexpected server error')
      end
      # Use same exception ID to be able to find exception in logs.
      common_error.instance_variable_set(:@id, e.id)
      return response(500, {content_type: 'application/json'}, common_error.to_json)
    end
  end
end