How can I catch the errors of Management API in Laravel SDK?

I’m using Auth0 Laravel SDK, and trying to create a new user with Management API.

try {
    Auth0::management()
        ->users()
        ->create(
            connection: 'Username-Password-Authentication',
            body: [ params ]
        );
} catch (\Exception $e) {
    // I want here to be executed if errors occur.
}

If the user has already exited, I expect the catch block to be executed.
However, it is not.

I can check the errors in Monitoring > Logs section.

How can I use the error messages (e.g., “The user already exists.”) in the catch block?
Is it possible?

1 Like

I’m not sure if this approach is correct, but I’ve found how I can retrieve the error messages.

$user_create_api = Auth0::management()
    ->users()
    ->create(
        connection: 'Username-Password-Authentication',
        body: [ params ]
    );

$message = json_decode($user_create_api->getBody()->getContents())->message

This seems to be working well so far.

2 Likes