Switch from old success/error to modern then/catch structure

Catch() will get a few more errors then errors()

Also, add try/catch inside catch to detect any errors parsing the
response error payload
This commit is contained in:
Luis Ramos
2020-04-10 12:21:42 +01:00
parent b92e858448
commit 7414047b92
2 changed files with 14 additions and 9 deletions

View File

@@ -14,15 +14,19 @@ Darkswarm.factory 'Checkout', ($injector, CurrentOrder, ShippingMethods, StripeE
submit: =>
Loading.message = t 'submitting_order'
$http.put('/checkout.json', {order: @preprocess()}).success (data, status)=>
Navigation.go data.path
.error (response, status)=>
if response.path
Navigation.go response.path
else
Loading.clear()
@errors = response.errors
RailsFlashLoader.loadFlash(response.flash)
$http.put('/checkout.json', {order: @preprocess()})
.then (response) =>
Navigation.go response.data.path
.catch (response) =>
try
if response.data.path
Navigation.go response.data.path
else
Loading.clear()
@errors = response.data.errors
RailsFlashLoader.loadFlash(response.data.flash)
catch error
RailsFlashLoader.loadFlash("Unkown error occurred")
# Rails wants our Spree::Address data to be provided with _attributes
preprocess: ->

View File

@@ -119,6 +119,7 @@ describe 'Checkout service', ->
it "puts errors into the scope", ->
$httpBackend.expectPUT("/checkout.json").respond 400, {errors: {error: "frogs"}}
Checkout.submit()
$httpBackend.flush()
expect(Checkout.errors).toEqual {error: "frogs"}