jsonapi-rb / jsonapi-rails

Rails gem for fast jsonapi-compliant APIs.
http://jsonapi-rb.org
MIT License
319 stars 63 forks source link

Render error code #64

Closed arefaslani closed 6 years ago

arefaslani commented 6 years ago

I want to render error code property, as JSON API specification describes, with jsonapi_errors method. How could I set the code property in error object?

beauby commented 6 years ago

Hi @arefaslani. It really depends how you're serializing your errors. If you're using custom error serializers, you can use the code DSL method.

arefaslani commented 6 years ago

@beauby How should I use the code DSL? I tested it as:

render jsonapi_errors: outcome.errors, code: 100, status: :unprocessable_entity

but it doesn't work. I use ActiveInteraction as the I in DCI. This is my configuration for jsonapi:

JSONAPI::Rails.configure do |config|
  # Set a default serializable class mapping for errors.
  config.jsonapi_errors_class = Hash.new { |h, k|
  }.tap { |h|
    h[:'ActiveModel::Errors'] = JSONAPI::Rails::SerializableActiveModelErrors
    h[:'ActiveInteraction::Errors'] = JSONAPI::Rails::SerializableActiveModelErrors
    h[:Hash] = JSONAPI::Rails::SerializableErrorHash
  }
end

Here is how JSON API describes error code.

arefaslani commented 6 years ago

@beauby I can't find code DSL and how to use it anywhere...