2

I am using a JWS library,to un-sign some tokens, so when the token was invalid I got the Exception InvalidArgumentException

({ "name": "Exception", "message": "The token \"123\" is an invalid JWS", "code": 0, "type": "InvalidArgumentException", "file": "/var/www/html/checkout/vendor/namshi/jose/src/Namshi/JOSE/J‌​WS.php", "line": 143,)

$jws= SimpleJWS::load($data);

The static function load throws an exception if the token is not valid, and I don't want to display the exception message, instead I want to display a friendly error message.

any help?

3
  • 3
    just use try{} catch(invalidArgumentException $e ){} Commented Feb 16, 2017 at 8:55
  • I tried this try {$jws= SimpleJWS::load($data);}catch(invalidArgumentException $e){echo $e->getMessage();} but not working! Commented Feb 16, 2017 at 8:59
  • php.net/manual/en/language.exceptions.php Commented Feb 16, 2017 at 9:02

2 Answers 2

3

What about this:

try {
    $jws= SimpleJWS::load($data);
// if it's the php exception http://php.net/manual/en/class.invalidargumentexception.php
} catch (\InvalidArgumentException $e) {
// if it's the library's exception you should specify the complete namespace
//} catch (InvalidArgumentException $e) {
    //  token is not valid
} 
Sign up to request clarification or add additional context in comments.

3 Comments

Still not working ({ "name": "Exception", "message": "The token \"123\" is an invalid JWS", "code": 0, "type": "InvalidArgumentException", "file": "/var/www/html/checkout/vendor/namshi/jose/src/Namshi/JOSE/JWS.php", "line": 143,)
thanks man, but why "\Exception" this works not "InvalidArgumentException" since the thrown exception is InvalidArgumentException?
your file is in a namespace so you need to use \InvalidArgumentException to catch the exception of the base namespace. If you forget the '\' before the name, php will look for an InvalidArgumentException defined in the current namespace
2

Try this, it worked for me

try {
    $jws= SimpleJWS::load($data);
} catch (\Exception $e) {
    # Do something
} 

1 Comment

I used only Exception, and wasn't working. With \Exception works.