PHP: Slim Framework-Exception Handling

Ich gerade fertig mit der Erstellung einer API-Anwendung mit slim-Rahmen, zunächst, in meinem code verwende ich einen dependency-container, um alle Ausnahmen geworfen, der code ist unten.

//Add container to handle all exceptions/errors, fail safe and return json
$container['errorHandler'] = function ($container) {
    return function ($request, $response, $exception) use ($container) {
        //Format of exception to return
        $data = [
            'message' => $exception->getMessage()
        ];
        return $container->get('response')->withStatus(500)
            ->withHeader('Content-Type', 'application/json')
            ->write(json_encode($data));
    };
};

Sondern werfen einen 500 Server Error alle Zeit, die ich hinzufügen möchte, andere HTTPS-response-code. Ich Frage mich, wenn ich Hilfe bekommen könnte, wie das geht.

public static function decodeToken($token)
{
    $token = trim($token);
    //Check to ensure token is not empty or invalid
    if ($token === '' || $token === null || empty($token)) {
        throw new JWTException('Invalid Token');
    }
    //Remove Bearer if present
    $token = trim(str_replace('Bearer ', '', $token));

    //Decode token
    $token = JWT::decode($token, getenv('SECRET_KEY'), array('HS256'));

    //Ensure JIT is present
    if ($token->jit == null || $token->jit == "") {
        throw new JWTException('Invalid Token');
    }

    //Ensure User Id is present
    if ($token->data->uid == null || $token->data->uid == "") {
        throw new JWTException("Invalid Token");
    }
    return $token;
}

Das problem ist noch viel mehr Funktionen, wie das oben, da-slim framework entscheidet, behandeln alle Ausnahmen implizit, habe ich keinen Zugriff auf try catch um alle Fehler abzufangen,

Schreibe einen Kommentar