JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Internal server error when validation fails on a join model
まだ誰も着手していません。
- 主要言語
- 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
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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