Exception: Caprese::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Caprese::Error
- Defined in:
- lib/caprese/error.rb
Direct Known Subclasses
ActionForbiddenError, AssociationNotFoundError, DeleteRestrictedError, RecordInvalidError, RecordNotFoundError
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Creates a serializable hash for the error so we can serialize and return it.
-
#full_message ⇒ String
(also: #message)
The full error message based on the different attributes we initialized the error with.
-
#i18n_scope ⇒ String
The scope to look for I18n translations in.
-
#initialize(model: nil, controller: nil, action: nil, field: nil, code: :invalid, t: {}) ⇒ Error
constructor
Initializes a new error.
-
#t ⇒ Hash
Adds field and capitalized Field title to the translation params for every error and returns them.
-
#with_header(header = {}) ⇒ Object
Allows us to add to the response header when we are failing.
Constructor Details
#initialize(model: nil, controller: nil, action: nil, field: nil, code: :invalid, t: {}) ⇒ Error
Initializes a new error
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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 @t = t @header = { status: :bad_request } end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
4 5 6 |
# File 'lib/caprese/error.rb', line 4 def code @code end |
#field ⇒ Object (readonly)
Returns the value of attribute field.
4 5 6 |
# File 'lib/caprese/error.rb', line 4 def field @field end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
4 5 6 |
# File 'lib/caprese/error.rb', line 4 def header @header end |
Instance Method Details
#as_json ⇒ Hash
Creates a serializable hash for the error so we can serialize and return it
95 96 97 98 99 100 101 |
# File 'lib/caprese/error.rb', line 95 def as_json { code: code, field: field, message: } end |
#full_message ⇒ String Also known as: message
The full error message based on the different attributes we initialized the error with
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 70 71 72 73 74 75 76 |
# File 'lib/caprese/error.rb', line 38 def 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_scope ⇒ String
Returns The scope to look for I18n translations in.
31 32 33 |
# File 'lib/caprese/error.rb', line 31 def i18n_scope Caprese.config.i18n_scope end |
#t ⇒ Hash
Adds field and capitalized Field title to the translation params for every error and returns them
107 108 109 |
# File 'lib/caprese/error.rb', line 107 def t @t.merge(field: @field, field_title: @field.to_s.titleize) end |
#with_header(header = {}) ⇒ Object
Should be used as such: fail Error.new(…).with_headers(…)
Allows us to add to the response header when we are failing
86 87 88 89 90 |
# File 'lib/caprese/error.rb', line 86 def with_header(header = {}) @header = header @header[:status] ||= :bad_request self end |