Module: HelixWebServices::ErrorHandling

Defined in:
lib/helix_web_services.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) registered(app)



19
20
21
22
23
24
25
26
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
# File 'lib/helix_web_services.rb', line 19

def self.registered(app)
  # Always invoke our error handlers.
  app.set :raise_errors => false
  app.set :show_exceptions => false
  app.set :dump_errors => true

  app.error do |err|
    err = env['sinatra.error']

    if err.is_a?(P4Exception)
      # Can happen when we're not passing a block to
      # open_p4. Convert to a P4WebAPI error. This is not ideal
      # as it always uses the same code, but then we have no idea
      # what actually happened here.

      err = P4Error.default_error(err.to_s)

      # Fall through...
    end

    body = nil

    if err.is_a?(P4Error)
      if err.message_code == 7480 || err.message_code == 7189
        halt 401, {
                    MessageCode: 0,
                    MessageSeverity: 4,
                    MessageText: 'Resource not found'
                }.to_json
      else
        code = 500
        code = 400 if err.user_error?
        halt code, {
                     MessageCode: err.message_code,
                     MessageSeverity: err.message_severity,
                     MessageText: err.message_text
                 }.to_json
      end
    end

    if body.nil?
      body = {
          MessageCode: 15_361,
          MessageSeverity: P4::E_FATAL,
          MessageText: 'Unknown server issue'
      }.to_json
    end

    halt 500, body
  end
end