-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Spree::Api:LineItemsController#create
handling of validation errors
#4177
Fix Spree::Api:LineItemsController#create
handling of validation errors
#4177
Conversation
@RyanofWoods thanks for this PR! I think it makes sense, although I would like to discuss some doubts that come to my mind. The first one is about the fact that the focus changes from the line item to the order. Also, I'm wondering if we should add another action (i.e. Then, I wonder if changing the implementation of |
Hi @spaghetticode, thanks for the review! 🙂 Having another action for adding multiple items would probably be best 👍 If a method gets added to the OrderContents class then the Having the Do you think there's more of a risk of developers not handling the errors if it doesn't raise? Or it is something we shouldn't worry about. Note, this change would mean updating the frontend code, however, I currently have a PR open to move this logic out 🤔 #4102 |
@RyanofWoods thanks for the detailed explanation ❤️ Well, given that the frontend is leveraging the current behavior I think that changing the way On the other hand, I think it's still possible to avoid answering with the order in favor of the line item (thus keeping the existing interface also in case of error) with something like this: def create
variant = Spree::Variant.find(params[:line_item][:variant_id])
@line_item = begin
@order.contents.add(
variant,
params[:line_item][:quantity] || 1,
options: line_item_params[:options].to_h
)
rescue ActiveRecord::RecordInvalid => error
error.record
end
if @line_item.persisted?
respond_with(@line_item, status: 201, default_template: :show)
else
invalid_resource!(@line_item)
end
end Disclaimer: I didn't try this code thoroughly, so there may be something I'm overlooking |
Hi @spaghetticode. Sorry for the very late response. I have been thinking about this more deeply. I am thinking that in the long-term it would be more preferable and be more in line with Solidus to have If so, perhaps there's a way to achieve this change by using the |
@RyanofWoods yes, in general I think using |
The idea is to deprecate I agree that it'd be best to keep the interface for the current end-point and create a new one for the batch action. |
BTW, we should make sure that the raising on |
Hey, @RyanofWoods! Would you still like to work on it? As a minimal change, we could fix rendering the validation errors to the end-user instead of raising them. |
Hey, @RyanofWoods, pinging you again, in case you'd like to complete it 🙂 |
I will look into this again soon (friday or after my two weeks vacation) @waiting-for-dev |
1367a6f
to
ccfe2a7
Compare
Codecov Report
@@ Coverage Diff @@
## main #4177 +/- ##
=======================================
Coverage 88.67% 88.67%
=======================================
Files 564 564
Lines 13894 13894
=======================================
+ Hits 12320 12321 +1
+ Misses 1574 1573 -1
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a suggestion about the test, but the application code is perfect 👍
Before, #create was expecting that the LineItem instance would have errors if failed to be created due to validation errors. However, #add actually raises ActiveRecord::RecordInvalid: Validation failed, so this endpoint could have never handled validation errors correctly.
ccfe2a7
to
5946a34
Compare
Rebased |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! 🙌
rescue ActiveRecord::RecordInvalid => error | ||
invalid_resource!(error.record) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering ig this could/should be turned into a rescue_from
in the API::BaseController
Spree::Api:LineItemsController#create
handling of validation errors
Spree::Api:LineItemsController#create calls #add on an OrderContents
instance which eventually call #save!. This raises errors if validation
fails. #create was not handling these validation errors. In fact, it had
an if statement checking for errors on a LineItem, but this could never
be returned if errors were raised.
I can include changes to the documentation if the code looks good 👍
Checklist: