I have a custom error class like this:
class EntityCrudError < StandardError
attr_reader :action
attr_reader :modelName
attr_reader :entity
attr_reader :errors
def initialize(action, model, entity = nil)
@action = action
@model = model
@entity = entity
@errors = entity.respond_to?(:errors) ? entity.errors : nil
end
end
I use this in a Sinatra web application. When the error occurs I send an instance of this object in JSON format:
myError.to_json
this to_json
method is the ActiveSupport implementation.
However, the message is not in the JSON object since it's not an instance variable. I need that message in my JSON object. I had two ideas to do this but both of them don't work...
- Make the message an instance variable in
initialize
, but there it doesn't seem to exist yet. - Merge the message into the JSON object, but I don't know how to merge it.
Which of both is the best and how do I implement it?
.message
is a function that returns it. That's the way Errors work in Ruby.