Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kreierson's avatar

Catch Laravel custom exception in axios

I am storing a credit card on Stripe for a customer using axios.

axios.post('/outfitter/api/customers/update-card', self.formData).then(function(response) {
    self.alert.success('Card updated!');
}).catch(function(error) {
    console.log(error.response.data.errors);
});
                                

This submits the request with the stripe token to my controller. In my controller I create the customer on stripes end and associate the card.

I'm trying to catch a card declined error and throw a custom exception

try {
    $stripeCustomer = \Stripe\Customer::create([
        "description" => $customer->first_name . ' ' . $customer->last_name,
        "email" => $customer->email,
        "source" => $stripe_token, // obtained with Stripe.js
        "metadata" => [
            'outfitter_id' => Auth::user()->outfitter_id,
            'outfitter_name' => Auth::user()->outfitter->name
        ]
    ]);
} catch (\Stripe\Error\Card $e) {
    throw new StripeCardDeclinedException;
}

Custom Exception

public function render($request)
{
    return response()->json([
            'errors' => [
                'card_declined' => ['Card Declined']
            ]
    ], 422);
}

I want to return a json response similar to what the Laravel validator returns. In the network tab in Chrome, I am seeing the response: {"errors":{"card_declined":["Card Declined"]}} so this tells me it is working. But what isn't happening is the catch from my axios request isn't getting called so the errors are getting logged to the console.

If I open up the console below is what I get:

Uncaught (in promise) Error: Request failed with status code 422 at createError (app.js?id=09a13c6d9ff0ded25ace:601) at settle (app.js?id=09a13c6d9ff0ded25ace:745) at XMLHttpRequest.handleLoad (app.js?id=09a13c6d9ff0ded25ace:155)

Anyone run into this before?

0 likes
6 replies
kreierson's avatar

I suppose it's worth mentioning that my axios request is inside the stripe request. I wonder if it has something to do with the way promises work that I don't understand.

this.stripe.createToken(this.card, {
    name: this.formData.customer.full_name,
}).then(function(result) {
     self.formData.stripeToken = result.token.id;
     axios.post('/outfitter/api/customers/update-card', self.formData).then(function(response) {
         self.alert.success('Card updated!');
     }).catch(function(error) {
         console.log(error.response.data.errors);
     });
});
kreierson's avatar
kreierson
OP
Best Answer
Level 13

Damn it. I was working on the wrong Vue file. Idiot

5 likes
iCick e Ciak's avatar

@kreierson Well, I have to admit this answer was helpful because I noticed I was working on the wrong method😂

bdjunayed's avatar

Damn it. I was working on the wrong Vue file. Idiot

lolz. enjoyed your post.

1 like

Please or to participate in this conversation.