Skip to content

[Proposal] Descriptive Exception Classes #20

@alexw23

Description

@alexw23

Is it possible to have specific exceptions so they can be caught accordingly?

At the moment I am having todo the following:

    catch (UnexpectedValueException $e)
    {
        if($e->getMessage() == "Expired Token") {
           // Logic Here
        } else {
            throw $e;
        }
    }

Instead I propose the following:

Declare an exception class (i.e. Exceptions.php)

        class ExpiredTokenException extends \UnexpectedValueException {}

Handle it in the code

        if (isset($payload->exp) && time() >= $payload->exp){
            throw new ExpiredTokenException('Expired Token');
        }

Catch It

    catch (JWT\Exceptions\ExpiredTokenException $e)
    {
        // Logic Here
    }

This can be caught much easier and would have backwards compatibility with devs using UnexpectedValueException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions