Module: RazorRisk::Cassini::Mixin::RazorResponseValidator

Extended by:
RazorResponseValidator
Includes:
Pantheios, RazorRisk::Core::Diagnostics::Logger, Xqsr3::Quality::ParameterChecking
Included in:
RazorResponseValidator
Defined in:
lib/razor_risk/cassini/mixin/razor_response_validator.rb

Overview

Methods for handling qualified responses from Razor. Should be included by a Sintra Microservice. When included into a microservice that inherits from Sinatra::Base it will define a custom error hanlder so exceptions generated during validation do not need to be caught.

Defined Under Namespace

Classes: RazorErrorException

Instance Method Summary collapse

Instance Method Details

#included(base) ⇒ Object

Adds a custom error handler to a Sintra Application so the exceptions raised by validate_qualified_response will automatically get handled.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/razor_risk/cassini/mixin/razor_response_validator.rb', line 95

def included(base)

    if base.respond_to? :error
        base.error RazorErrorException do
            x = env['sinatra.error']
            halt(
                x.http_status,
                { 'Content-Type' => 'text/plain' },
                x.message
            )
        end
    end
end

#validate_qualified_razor_response(qualified_result) ⇒ Object

Halts procressing the request with an appropriate status code if the response is a failure and provided a Razor Error code.

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/razor_risk/cassini/mixin/razor_response_validator.rb', line 114

def validate_qualified_razor_response qualified_result

    return unless qualified_result.respond_to? :succeeded?

    unless qualified_result.succeeded?

        failure_qualifier = qualified_result.failure_qualifier
        return unless failure_qualifier.respond_to? :reasons

        reasons = failure_qualifier.reasons

        if r = reasons.lookup?('RZ0007')
            # Unauthorized

            log :debug1, 'Invalid Razor session'
            raise RazorErrorException.new 401, 'Invalid credentials'
        elsif r = reasons.lookup?('RZ0009')
            # Invalid session

            log :debug1, 'Invalid Razor session'
            raise RazorErrorException.new 401, 'Invalid credentials'
        elsif r = reasons.lookup?('RZ0002')
            # Generic error, should be checked before others as will

            # acompany errors such as ODBC errors that should not be

            # reported to the user.

            reasons_str = reasons.map { |r| "#{r.message}: #{r.details}" }.to_s
            log :warning, 'Request Failed:', reasons_str
            raise RazorErrorException.new 500, "#{r.message}: #{r.details}"
        elsif r = reasons.lookup?('RZ0011')
            # Record does not exist

            log :debug1, 'Record does not exist'
            raise RazorErrorException.new 404, "#{r.message}: #{r.details}"
        elsif r = reasons.lookup?('RZ0010')
            # Duplicate record

            log :debug1, 'Duplicate record'
            raise RazorErrorException.new 400, "#{r.message}: #{r.details}"
        elsif r = reasons.lookup?('RZ0008')
            # ODBC Error. Should not be reported to the user.

            reasons_str = reasons.map { |r| "#{r.message}: #{r.details}" }.to_s
            log :warning, 'Request Failed:', reasons_str
            raise RazorErrorException.new 500, 'Oops! Something went wrong!'
        elsif !reasons.empty?
            # Unhandled Razor error code.

            reasons_str = reasons.map { |r| "#{r.message}: #{r.details}" }.to_s
            log :warning, 'Request Failed:', reasons_str
            raise RazorErrorException.new 500, 'Oops! Something went wrong!'
        end
    end

    nil
end