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

commandantp's avatar

Try Catch not being handled in Artisan Command

Hi,

I have a try catch block in one of the command. Problem is when error occurs Laravel takes over and my catch block never happens. I read the error docs on L5.1 but not sure how it works with the ErrorHandler. Here is my block:

try {
                            $re = \Stripe\Refund::create([
                                'charge'    => $booking->stripe_charge_id,
                            ]);
                        } catch(\Stripe\Error\InvalidRequest $e) {
                            // Since it's a decline, \Stripe\Error\Card will be caught
                            $body = $e->getJsonBody();
                            $err  = $body['error'];

                            $err_status     = $e->getHttpStatus();
                            $err_type       = array_key_exists('type'   , $err)       ? $err['type']      : '';
                            $err_code       = array_key_exists('code'   , $err)       ? $err['code']      : '';
                            $err_param      = array_key_exists('param'  , $err)       ? $err['param']     : '';
                            $err_message    = array_key_exists('message', $err)       ? $err['message']   : '';
}

Is it something like in Exceptions/handler.php:

// Stripe exceptions
        if ($e instanceof \Stripe\Error\InvalidRequest) {
            // Do something ?
        }

If yes how do I get the variables I had in my ongoing command? I need specific error responses depending on the case.

Thanks!

0 likes
2 replies
JeroenVanOort's avatar
Level 6

That should just work, provided the Exception is a \Stripe\Error\Card. Are you sure about that? Can you post a stacktrace?

1 like
commandantp's avatar

@JeroenVanOort thanks, you pointed out the right solution. The error was an Invalid Request error which I catched in the handler but not the Command. It does work now within the Command. It did follow the rest of the command after catching my error. Do you know if it possible in the error exception handler to pass arguments to it from a controller or command?

Please or to participate in this conversation.