JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Internal server error when validation fails on a join model

オープン
#1,016 コメント 0 件 リアクション 6 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Ruby
スター
2.3k
フォーク
546
PR マージ指標
30日以内にマージされた PR はありません

説明

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

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

lib/jsonapi/resource.rb の 280 行目付近にある _create_to_many_links から始めます。ここで issue は join model が直ちに保存されると報告しています。commit 928485a4ea071edf164e954496c06b0de314d555 の失敗する spec を実行または調査します。完了条件は、バリデーションエラーによって 500 ではなく、適切にフォーマットされた JSON:API エラーレスポンスを含む 422 レスポンスが生成されることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
ruby
領域
api, backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。