Module: ErratumResponsum

Extended by:
ActiveSupport::Concern
Defined in:
lib/erratum_responsum.rb,
lib/erratum_responsum/version.rb,
lib/erratum_responsum/cancancan.rb

Defined Under Namespace

Classes: NothingAccessibleBy

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Instance Method Details

#bad_request(*exception) ⇒ Object

400



21
22
23
24
25
# File 'lib/erratum_responsum.rb', line 21

def bad_request(*exception)
  render json: { errors: serialize_errors(exception) },
         status: :bad_request,
         content_type: ErratumResponsum.error_media_type
end

#conflict(exception) ⇒ Object

409



69
70
71
72
73
# File 'lib/erratum_responsum.rb', line 69

def conflict(exception)
  render json: { errors: serialize_errors(exception) },
         status: :conflict,
         content_type: ErratumResponsum.error_media_type
end

#error_code(error) ⇒ Object



130
131
132
133
# File 'lib/erratum_responsum.rb', line 130

def error_code(error)
  return 'Ex' + String(error.send(:error_code)) if error.respond_to?(:error_code)
  'Gx' + Digest::MD5.hexdigest(error.class.name)
end

#forbidden(exception) ⇒ Object

403



35
36
37
38
39
# File 'lib/erratum_responsum.rb', line 35

def forbidden(exception)
  render json: { errors: serialize_errors(exception) },
         status: :forbidden,
         content_type: ErratumResponsum.error_media_type
end

#gone(exception) ⇒ Object

410



76
77
78
79
80
# File 'lib/erratum_responsum.rb', line 76

def gone(exception)
  render json: { errors: serialize_errors(exception) },
         status: :gone,
         content_type: ErratumResponsum.error_media_type
end

#internal_server_error(exception) ⇒ Object

500



111
112
113
114
115
# File 'lib/erratum_responsum.rb', line 111

def internal_server_error(exception)
  render json: { errors: serialize_errors(exception) },
         status: :internal_server_error,
         content_type: ErratumResponsum.error_media_type
end

#method_not_allowed(exception) ⇒ Object

405



55
56
57
58
59
# File 'lib/erratum_responsum.rb', line 55

def method_not_allowed(exception)
  render json: { errors: serialize_errors(exception) },
         status: :method_not_allowed,
         content_type: ErratumResponsum.error_media_type
end

#not_acceptable(exception) ⇒ Object

406



62
63
64
65
66
# File 'lib/erratum_responsum.rb', line 62

def not_acceptable(exception)
  render json: { errors: serialize_errors(exception) },
         status: :not_acceptable,
         content_type: ErratumResponsum.error_media_type
end

#not_found(exception) ⇒ Object

404



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/erratum_responsum.rb', line 42

def not_found(exception)
  if defined?(NothingAccessibleBy) && exception.message.include?('TRUE=FALSE')
    # When CanCanCan can not find a clause for an associated model, when using accessible_by, it will use TRUE=FALSE

    # instead of throwing a CanCan::AccessDenied.

    return forbidden(NothingAccessibleBy.new(exception.model))
  end

  render json: { errors: serialize_errors(exception) },
         status: :not_found,
         content_type: ErratumResponsum.error_media_type
end

#not_implemented(exception) ⇒ Object

501



118
119
120
121
122
# File 'lib/erratum_responsum.rb', line 118

def not_implemented(exception)
  render json: { errors: serialize_errors(exception) },
         status: :not_implemented,
         content_type: ErratumResponsum.error_media_type
end

#precondition_required(exception) ⇒ Object

428



97
98
99
100
101
# File 'lib/erratum_responsum.rb', line 97

def precondition_required(exception)
  render json: { errors: serialize_errors(exception) },
         status: :precondition_required,
         content_type: ErratumResponsum.error_media_type
end

#serialize_errors(errors) ⇒ Object



124
125
126
127
128
# File 'lib/erratum_responsum.rb', line 124

def serialize_errors(errors)
  Array(errors).map do |error|
    { message: error.respond_to?(:message) ? error.message : error, code: error_code(error) }
  end
end

#too_many_requests(exception) ⇒ Object

429



104
105
106
107
108
# File 'lib/erratum_responsum.rb', line 104

def too_many_requests(exception)
  render json: { errors: serialize_errors(exception) },
         status: :too_many_requests,
         content_type: ErratumResponsum.error_media_type
end

#unauthorized(*exception) ⇒ Object

401



28
29
30
31
32
# File 'lib/erratum_responsum.rb', line 28

def unauthorized(*exception)
  render json: { errors: serialize_errors(exception) },
         status: :unauthorized,
         content_type: ErratumResponsum.error_media_type
end

#unprocessable_entity(exception) ⇒ Object

422



90
91
92
93
94
# File 'lib/erratum_responsum.rb', line 90

def unprocessable_entity(exception)
  render json: { errors: serialize_errors(exception) },
         status: :unprocessable_entity,
         content_type: ErratumResponsum.error_media_type
end

#unsupported_media_type(exception) ⇒ Object

415



83
84
85
86
87
# File 'lib/erratum_responsum.rb', line 83

def unsupported_media_type(exception)
  render json: { errors: serialize_errors(exception) },
         status: :unsupported_media_type,
         content_type: ErratumResponsum.error_media_type
end