JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

aborted destroy turned into JSONAPI::Exceptions::ValidationErrors without error message

Abierto
#1,253 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
Ruby
Estrellas
2.3k
Forks
546
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

This issue is a (choose one):

  • Problem/bug report.
  • Feature request.
  • Request for support. Note: Please try to avoid submitting issues for support requests. Use Gitter instead.

Checklist before submitting:

  • I've searched for an existing issue.
  • I've asked my question on Gitter and have not received a satisfactory answer.
  • I've included a complete bug report template. This step helps us and allows us to see the bug without trying to reproduce the problem from your description. It helps you because you will frequently detect if it's a problem specific to your project.
  • The feature I'm asking for is compliant with the JSON:API spec.

Description

Choose one section below and delete the other:

Bug reports:

JR version 0.9.3

  1. Given a record with before_destroy :something_that_may_abort
  2. Define def something_that_may_abort; throw :abort if condition?; end
  3. POST a DELETE by id to the corresponding resource
  4. Get
    NoMethodError: undefined method `code' for nil:NilClass

The problem stems from the JR processor

    def process
      run_callbacks :operation do
        run_callbacks operation_type do
          @result = send(operation_type)
        end
      end

    rescue JSONAPI::Exceptions::Error => e
      @result = JSONAPI::ErrorsOperationResult.new(e.errors[0].code, e.errors)
    end

Where the rescued JSONAPI::Exceptions::Error is a JSONAPI::Exceptions::ValidationErrors and the error_messages attribute is an empty hash. This can be observed in Rails code as

record.destroy
=> false
 record.errors.messages
=> {}

I suspect this isn't an altogether unusual case. Until this is addressed in JR, I'm looking at handling this by adding validation errors to the model before throwing the abort

Proposal

I wonder if there may be a place in JR to add a default validation error message to JSONAPI::Exceptions::ValidationErrors when none are given (i.e. when resource.model_error_messages is empty

https://github.com/cerebris/jsonapi-resources/blob/v0.9.7/lib/jsonapi/resource.rb#L224-L231

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el método process del procesador JR y con lib/jsonapi/resource.rb alrededor de las líneas 224-231, que el issue identifica como relacionados con los mensajes de error del modelo. Reproduce un callback de destroy de Rails que lance :abort y, a continuación, rastrea cómo los errores vacíos llegan a ValidationErrors. La tarea estará terminada cuando el destroy abortado produzca un error de validación utilizable en lugar del NoMethodError causado por la ausencia de un código.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
rails, ruby
Área
api, backend
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.