verbb / postie

A Craft Commerce shipping calculator plugin.
Other
12 stars 18 forks source link

Ability to act on provider errors #78

Open JJimmyFlynn opened 2 years ago

JJimmyFlynn commented 2 years ago

When an error is returned from a provider, it would be great if Postie provided a way to listen for an error response so that a different course of action could be taken.

For example: we have a site where we're using Postie to calculate UPS rates. The site sadly doesn't do any real address validation and a user could end up entering a wrong address. We would then need to show an error to the user and modify a few other things if the call to fetch rates fails.

Even having access to the raw response from the provider in the instance of an error would be a great start, as the modifyRates event doesn't fire on an error response. Though any additional helpers would also be great. :)

engram-design commented 2 years ago

My immediate thoughts are to expose a custom flash() Twig function, for you to check if there are any Postie-related errors. The idea being:

{% if craft.app.session.getFlash('postie-error') %}
...

I might also propose another event for you to hook into in your modules in the event of an error. The tricky part is that a provider can return an error for all manner of reasons, so it'd be a good idea to filter out anything you show customer-facing.

In terms of adding this to the modifyRates, I might create a new event for errors, purely because an error can happen before rates are even fetched, and the responses from providers can vary.

JJimmyFlynn commented 2 years ago

I think both would have pretty good utility. Particularly being able to respond to an error event and take appropriate actions.

It would be nice to have access to what the error code is, but know that's pretty unlikely due to the varying APIs for the providers. But just being notified there was an error and getting access to the raw error response would be tops!