-
-
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
Making simple coordinator insufficient stock to include a message #3577
Making simple coordinator insufficient stock to include a message #3577
Conversation
raise Spree::Order::InsufficientStock | ||
item_names = leftover.variants.map(&:name).to_sentence | ||
message = I18n.t('spree.inventory_error_flash_for_insufficient_quantity', names: item_names) | ||
raise Spree::Order::InsufficientStock.new(message) |
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.
What about enhancing this spec
expect{ shipments }.to raise_error(Spree::Order::InsufficientStock) |
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 think there's another path to explore here, let me know your thoughts.
raise Spree::Order::InsufficientStock | ||
item_names = leftover.variants.map(&:name).to_sentence | ||
message = I18n.t('spree.inventory_error_flash_for_insufficient_quantity', names: item_names) | ||
raise Spree::Order::InsufficientStock.new(message) |
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 like the idea but I think we can take advantage of creating a custom exception that contains the unavailable items and let the consumer decide what to do with those.
What I have in mind is changing the exception definition to something like:
class InsufficientStock < StandardError
attr_reader :items
def initialize(message=nil, items: [])
super(message)
@items = items
end
end
and push names in it with:
raise Spree::Order::InsufficientStock.new(items: leftovers)
At that point, we can consume the exception data in different ways depending on the context, even using rescue_from
since its handler methods accept an argument that will be the exception containing the items.
rescue_from Spree::Order::InsufficientStock, with: :something
def something(exception)
# exception.items is available here!
end
What do you think?
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 like it! will refactor my code to take this path
3678847
to
cecb3c1
Compare
What do you think about this new approach @kennyadsl |
core/app/models/spree/order.rb
Outdated
class InsufficientStock < StandardError | ||
attr_reader :items | ||
|
||
def initialize(message = nil, items: []) |
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.
Should the default value of items:
be an array or a hash?
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.
True! it returns a hash of items
cecb3c1
to
ace5ace
Compare
Rerunning specs, it's a flaky one! |
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.
Thanks, @softr8!
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.
Looks good to me! (Though there's a "simlpe" typo in the commit message that could be fixed.)
It is hard to know what items were the ones that were unavailable, this change makes the exception to include all the items that were unable to be fullfilled so they can be rescued and then can be manipulated to build a better error message
ace5ace
to
75f5e98
Compare
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.
Very cool, thanks @softr8!
It is hard to know what items were the ones that were
unavailable, this change includes item names that
were unable to be fullfilled, then this message
can be used in the frontend easily.
What do you think about this?
I saw that the frontend does some complex code to achieve kind of the same here
Checklist: