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

Lars-Janssen's avatar

Laravel catch 403 in spark

Hi,

How can I catch custom http responses like a 403 within spark?

For example:

Spark.post('/stores-name', this.form)
    .then((response) => {
        this.storeCreated(response);
    })
    .catch(error => {
        console.log(error.response)
    });

However when I console.log(error.response); or console.log(error);

There is no status code?

0 likes
1 reply
ejdelmonico's avatar

You can do this in App\Exception\Handler.php

public function handleException($request, Exception $exception)
{
    if ($exception instanceof AuthorizationException) {
            return $this->errorResponse($exception->getMessage(), 403);
        }
}

You can customize most of the exceptions in this method. Then, in something like an api controller:

class ApiController extends Controller
{
    use ApiResponser;

    /**
     * ApiController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth:api');
    }

    /**
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    protected function allowedAdminAction()
    {
        if (Gate::denies('admin-action')) {
            throw new AuthorizationException('This action is unauthorized');
        }
    }
}

Please or to participate in this conversation.