JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Internal server error when validation fails on a join model
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Ruby
- Sterne
- 2.3k
- Forks
- 546
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
Hello!
I have a model with a has_many :through relationship and a couple of validations on the join model. Whenever the validations fail i get a 500 response, a 422 with a properly formatted jsonapi error response would be preferable. For now i've resorted to using custom exceptions and fail CustomException in the validations so it's catched properly, but that's not ideal.
Problem is, the shovel (<<) method in _create_to_many_links (https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/resource.rb#L280) is triggering the model's save! immediately, so when it raises ActiveRecord::ValidationFailed it's not catched and interpreted as an internal server error.
Can this be solved, or is there a preferred approach to processing such exceptions?
I've added a failing spec that demonstrates this issue here: https://github.com/needto/jsonapi-resources/commit/928485a4ea071edf164e954496c06b0de314d555
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne in lib/jsonapi/resource.rb bei _create_to_many_links um Zeile 280, wo das Issue meldet, dass das Join-Modell sofort gespeichert wird. Führe die fehlschlagende Spec aus dem Commit 928485a4ea071edf164e954496c06b0de314d555 aus oder untersuche sie; abgeschlossen ist die Arbeit, wenn Validierungsfehler eine 422-Antwort mit einer korrekt formatierten JSON:API-Fehlerantwort statt einer 500 erzeugen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- ruby
- Bereich
- api, backend
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100