Improve unexpected error handling and add test cases for it

This commit is contained in:
Luis Ramos
2020-04-14 13:44:58 +01:00
parent 7414047b92
commit cdf5bcb7eb
2 changed files with 31 additions and 2 deletions

View File

@@ -22,11 +22,14 @@ Darkswarm.factory 'Checkout', ($injector, CurrentOrder, ShippingMethods, StripeE
if response.data.path
Navigation.go response.data.path
else
throw response unless response.data.errors || response.data.flash
Loading.clear()
@errors = response.data.errors
RailsFlashLoader.loadFlash(response.data.flash)
catch error
RailsFlashLoader.loadFlash("Unkown error occurred")
RailsFlashLoader.loadFlash(error: t("checkout.failed")) # inform the user about the unexpected error
throw error # generate a BugsnagJS alert
# Rails wants our Spree::Address data to be provided with _attributes
preprocess: ->

View File

@@ -6,7 +6,9 @@ describe 'Checkout service', ->
flash = null
scope = null
FlashLoaderMock =
loadFlash: (arg)->
loadFlash: (arg) ->
Loading =
clear: (arg)->
paymentMethods = [{
id: 99
test: "foo"
@@ -48,6 +50,7 @@ describe 'Checkout service', ->
module 'Darkswarm'
module ($provide)->
$provide.value "RailsFlashLoader", FlashLoaderMock
$provide.value "Loading", Loading
$provide.value "currentOrder", orderData
$provide.value "shippingMethods", shippingMethods
$provide.value "paymentMethods", paymentMethods
@@ -123,6 +126,29 @@ describe 'Checkout service', ->
$httpBackend.flush()
expect(Checkout.errors).toEqual {error: "frogs"}
it "throws an exception and sends a flash message to the flash service when reponse doesnt contain errors nor a flash message", ->
spyOn(FlashLoaderMock, "loadFlash") # Stubbing out writes to window.location
$httpBackend.expectPUT("/checkout.json").respond 400, "broken response"
try
Checkout.submit()
$httpBackend.flush()
catch e
expect(e.data).toBe("broken response")
expect(FlashLoaderMock.loadFlash).toHaveBeenCalledWith({ error: t("checkout.failed") })
it "throws an exception and sends a flash message to the flash service when an exception is thrown while handling the error", ->
spyOn(FlashLoaderMock, "loadFlash") # Stubbing out writes to window.location
spyOn(Loading, "clear").and.callFake(-> throw "unexpected error")
$httpBackend.expectPUT("/checkout.json").respond 400, {flash: {error: "frogs"}}
try
Checkout.submit()
$httpBackend.flush()
catch e
expect(e).toBe("unexpected error")
expect(FlashLoaderMock.loadFlash).toHaveBeenCalledWith({ error: t("checkout.failed") })
describe "when using the Stripe Connect gateway", ->
beforeEach inject ($injector, StripeElements) ->
Checkout.order.payment_method_id = 666