Exception: Api::Exception

Inherits:
StandardError
  • Object
show all
Defined in:
lib/api/exception.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_constant, messages = nil) ⇒ Exception

Returns a new instance of Exception.



10
11
12
13
14
15
# File 'lib/api/exception.rb', line 10

def initialize(error_constant, messages = nil)
  @error_constant = error_constant
  @messages = {
    @error_constant[:key].to_s => [messages || @error_constant[:message]]
  }
end

Instance Attribute Details

#messagesObject

Returns the value of attribute messages.



8
9
10
# File 'lib/api/exception.rb', line 8

def messages
  @messages
end

Class Method Details

.error_constantsObject



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
# File 'lib/api/exception.rb', line 26

def error_constants
  {
    INTERNAL_SERVER_ERROR: {
      message: t(:internal_server_error),
      key: :internal_server_error,
      status_code: :internal_server_error
    },
    RECORD_NOT_FOUND: {
      message: t(:record_not_found),
      key: :record_not_found,
      status_code: :not_found
    },
    RECORD_INVALID: {
      message: t(:record_invalid),
      key: :record_invalid,
      status_code: :unprocessable_entity
    },
    RECORD_NOT_DESTROYED: {
      message: t(:record_not_destroyed),
      key: :record_not_destroyed,
      status_code: :forbidden
    },
    FORBIDDEN_RESOURCE: {
      message: t(:forbidden_resource),
      key: :forbidden_resource,
      status_code: :forbidden
    },
    UNAUTHORIZED_ACCESS: {
      message: t(:unauthorized_access),
      key: :unauthorized_access,
      status_code: :unauthorized
    }
  }
end

Instance Method Details

#full_messagesObject



17
18
19
# File 'lib/api/exception.rb', line 17

def full_messages
  @messages.values.flatten
end

#status_codeObject



21
22
23
# File 'lib/api/exception.rb', line 21

def status_code
  @error_constant[:status_code]
end