nebulab / simple_command

A simple, standardized way to build and use Service Objects (aka Commands) in Ruby
http://nebulab.it
MIT License
625 stars 55 forks source link

How to export command errors in ams ErrorSerializer? #4

Closed smartepsh closed 7 years ago

smartepsh commented 8 years ago

JSON API Errors Document in AMS I want to render the error message by using AMS jsonapi serializer. But there is something wrong if I render a simple_command result. I can render the errors to json but can not to jsonapi.

#simple_command
Class AuthenticateUser
*
*
  errors.add :user_authentication, 'invalid credentials'
*
*
end
#controller
command = AuthenticateUser.call(params[:email], params[:password])

render json: command, status: 401, adapter: :json_api, serializer: ActiveModel::Serializer::ErrorSerializer

log:

NoMethodError (undefined method `messages' for {:user_authentication=>["invalid credentials"]}:SimpleCommand::Errors):

app/controllers/authentication_controller.rb:10:in `authenticate'

What should I do?

kennyadsl commented 8 years ago

Hey there,

from the page you linked:

The resource MUST have a non-empty associated #errors object. The errors object must have a #messages method that returns a hash of error name to array of descriptions.

which SimpleCommand::Errors doesn't currently have.

If you want to add this method, feel free to submit a PR with this change.

smartepsh commented 8 years ago

thanks...I'll try..

kennyadsl commented 7 years ago

Going to close this one, feel free to open another issue or submit that PR. Thanks!

denniscastro commented 7 years ago

Hi I have the same problem

Just add:

    module SimpleCommand
        class Errors
            def messages
               self
            end
        end
    end