Exception: Caprese::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/caprese/error.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil, controller: nil, action: nil, field: nil, code: :invalid, t: {}) ⇒ Error

Initializes a new error

Parameters:

  • model (Symbol) (defaults to: nil)

    a symbol representing the model that the error occurred on

  • controller (String) (defaults to: nil)

    the name of the controller the error occurred in

  • action (String) (defaults to: nil)

    the name of the controller action the error occurred in

  • field (Symbol, String) (defaults to: nil)

    a symbol or string representing the field (model attribute or controller param) that the error occurred on if Symbol, a shallow field name. EX: :password if String, a nested field name. EX: ‘order_items.amount’

  • code (Symbol) (defaults to: :invalid)

    the error code

  • t (Hash) (defaults to: {})

    the interpolation variables to supply to I18n.t when creating the full error message



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/caprese/error.rb', line 16

def initialize(model: nil, controller: nil, action: nil, field: nil, code: :invalid, t: {})
  @model = model

  @controller = controller
  @action = action

  @field = field
  @code = code

  # field is nil if :base
  field_name = field || model
  @t = { field: field_name.to_s, field_title: field_name.to_s.titleize }.merge t

  @header = { status: :bad_request }
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



4
5
6
# File 'lib/caprese/error.rb', line 4

def code
  @code
end

#fieldObject (readonly)

Returns the value of attribute field.



4
5
6
# File 'lib/caprese/error.rb', line 4

def field
  @field
end

#headerObject (readonly)

Returns the value of attribute header.



4
5
6
# File 'lib/caprese/error.rb', line 4

def header
  @header
end

#tObject (readonly)

Returns the value of attribute t.



4
5
6
# File 'lib/caprese/error.rb', line 4

def t
  @t
end

Instance Method Details

#as_jsonHash

Creates a serializable hash for the error so we can serialize and return it

Returns:

  • (Hash)

    the serializable hash of the error



97
98
99
100
101
102
103
# File 'lib/caprese/error.rb', line 97

def as_json
  {
    code: code,
    field: field,
    message: full_message
  }
end

#full_messageString Also known as: message

The full error message based on the different attributes we initialized the error with

Returns:

  • (String)

    the full error message



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
70
71
72
73
74
75
76
77
78
# File 'lib/caprese/error.rb', line 40

def full_message
  if @model
    if field
      if i18n_set? "#{i18n_scope}.models.#{@model}.#{field}.#{code}", t
        I18n.t("#{i18n_scope}.models.#{@model}.#{field}.#{code}", t)
      elsif i18n_set?("#{i18n_scope}.field.#{code}", t)
        I18n.t("#{i18n_scope}.field.#{code}", t)
      elsif i18n_set? "#{i18n_scope}.#{code}", t
        I18n.t("#{i18n_scope}.#{code}", t)
      else
        code.to_s
      end
    else
      if i18n_set? "#{i18n_scope}.models.#{@model}.#{code}", t
        I18n.t("#{i18n_scope}.models.#{@model}.#{code}", t)
      elsif i18n_set? "#{i18n_scope}.#{code}", t
        I18n.t("#{i18n_scope}.#{code}", t)
      else
        code.to_s
      end
    end
  elsif @controller && @action
    if field && i18n_set?("#{i18n_scope}.controllers.#{@controller}.#{@action}.#{field}.#{code}", t)
      I18n.t("#{i18n_scope}.controllers.#{@controller}.#{@action}.#{field}.#{code}", t)
    elsif i18n_set?("#{i18n_scope}.controllers.#{@controller}.#{@action}.#{code}", t)
      I18n.t("#{i18n_scope}.controllers.#{@controller}.#{@action}.#{code}", t)
    elsif i18n_set? "#{i18n_scope}.#{code}", t
      I18n.t("#{i18n_scope}.#{code}", t)
    else
      code.to_s
    end
  elsif field && i18n_set?("#{i18n_scope}.field.#{code}", t)
    I18n.t("#{i18n_scope}.field.#{code}", t)
  elsif i18n_set? "#{i18n_scope}.#{code}", t
    I18n.t("#{i18n_scope}.#{code}", t)
  else
    code.to_s
  end
end

#i18n_scopeString

Returns The scope to look for I18n translations in.

Returns:

  • (String)

    The scope to look for I18n translations in



33
34
35
# File 'lib/caprese/error.rb', line 33

def i18n_scope
  Caprese.config.i18n_scope
end

#with_header(header = {}) ⇒ Object

Note:

Should be used as such: fail Error.new(…).with_headers(…)

Allows us to add to the response header when we are failing

Parameters:

  • headers (Hash)

    the headers to supply in the error response

  • [Symbol] (Hash)

    a customizable set of options

  • [String, (Hash)

    a customizable set of options



88
89
90
91
92
# File 'lib/caprese/error.rb', line 88

def with_header(header = {})
  @header = header
  @header[:status] ||= :bad_request
  self
end