Exception: Caprese::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Caprese::Error
- Defined in:
- lib/caprese/error.rb
Direct Known Subclasses
ActionForbiddenError, AssociationNotFoundError, DeleteRestrictedError, RecordInvalidError, RecordNotFoundError, RequestDocumentInvalidError
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.
-
#t ⇒ Object
readonly
Returns the value of attribute t.
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.
-
#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 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
#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 |
#t ⇒ Object (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_json ⇒ Hash
Creates a serializable hash for the error so we can serialize and return it
97 98 99 100 101 102 103 |
# File 'lib/caprese/error.rb', line 97 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
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 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
33 34 35 |
# File 'lib/caprese/error.rb', line 33 def i18n_scope Caprese.config.i18n_scope 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
88 89 90 91 92 |
# File 'lib/caprese/error.rb', line 88 def with_header(header = {}) @header = header @header[:status] ||= :bad_request self end |