I just finished creating an API application with slim framework, initially in my code I use a dependency container to handle all exceptions, the code is below.
$container['errorHandler'] = function ($container) {
return function ($request, $response, $exception) use ($container) {
$data = [
'message' => $exception->getMessage()
];
return $container->get('response')->withStatus(500)
->withHeader('Content-Type', 'application/json')
->write(json_encode($data));
};
};
But instead of throwing 500 Server Errorall the time, I would like to add another HTTPS response code. I wonder if I can get help on how to do this.
public static function decodeToken($token)
{
$token = trim($token);
if ($token === '' || $token === null || empty($token)) {
throw new JWTException('Invalid Token');
}
$token = trim(str_replace('Bearer ', '', $token));
$token = JWT::decode($token, getenv('SECRET_KEY'), array('HS256'));
if ($token->jit == null || $token->jit == "") {
throw new JWTException('Invalid Token');
}
if ($token->data->uid == null || $token->data->uid == "") {
throw new JWTException("Invalid Token");
}
return $token;
}
The problem is even more from such functions as above, since the slim framework decides to handle all exceptions implicitly, I do not have access to use try catchto catch any errors
source
share