Class: Blacksheep::Decorators::DefaultErrorHandler

Inherits:
ActionDecorator
  • Object
show all
Includes:
ErrorHandler
Defined in:
lib/blacksheep/decorators/default_error_handler.rb

Instance Method Summary collapse

Methods included from ErrorHandler

#call, #perform

Methods inherited from ActionDecorator

#__class__, #class, #decorators_chain

Instance Method Details

#handle(exception) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
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
# File 'lib/blacksheep/decorators/default_error_handler.rb', line 7

def handle(exception)
  json = status = nil

  case exception
    when Blacksheep::ActionError
      json = {
        errors: [
          pointer: {
            source:  exception.backtrace.first,
            identifier: exception.identifier,
          },
          title: exception.title,
          detail: exception.message,
        ]
      }

      status = exception.status

      #   when Pundit::NotAuthorizedError
      #     json = {
      #       errors: [
      #         pointer: {
      #           source: not_authorized_pointer(exception)
      #         },
      #         title: "#{exception.class}",
      #         detail: "#{exception.message}",
      #       ]
      #     }
      #     status = :unauthorized # 401
      #   when Exceptions::AuthenticationInvalid
      #     json = {
      #       errors: [
      #         pointer: {
      #           source: 'Secured Module'
      #         },
      #         title: "#{exception.class}",
      #         detail: "#{exception.message}",
      #       ]
      #     }
      #     status = :unauthorized # 401

    else
      json = {
        errors: [
          pointer: {
            source: 'Internal'
          },
          title: "#{exception.class}",
          detail: "#{exception.message}",
        ]
      }
  end

  status ||= :internal_server_error

  ActionResult.new(json, status)
end