如何在Symfony2 / Silex中捕获错误和异常?

时间:2015-01-22 13:25:15

标签: php symfony silex

我想在我的Silex应用程序中捕获错误和异常,将它们包装在一个自定义的JSON响应中,该响应将始终返回给客户端。我找到了三种基本方法:

$app->error()
Symfony\Component\Debug\ErrorHandler::register();
Symfony\Component\Debug\ExceptionHandler::register();

虽然我能够使用error()捕获控制器异常,但我没有使用php错误 - 它们总是在xdebug中结束。 我也未能理解error()ExceptionHandler::register()如何相互作用 - 我是否需要两者?如何确保我的error()响应是JSON?

我现在有以下示例代码:

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Router extends Silex\Application
{
    function __construct() {
        parent::__construct();

        // routes
        $this->match('/{context}', array($this, 'handler'));

        // error handler
        $this->error(function(\Exception $e, $code) {
            return $this->json(array("error" => $e->getMessage()), $code);
        });
    }

    function handler(Request $request, $context) {
        // throw new \Exception('test'); // exception- this is caught
        $t = new Test(); // error- this is not caught

        return 'DONE';
    }
}

Symfony\Component\Debug\ErrorHandler::register();

$app = new Router();
$app->run();

2 个答案:

答案 0 :(得分:5)

使用ErrorHandler::register();,您可以捕获错误,例如异常

实施例

use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\Debug\ErrorHandler;

///bla bla bla some code

//catch all errors and convert them to exceptions
ErrorHandler::register();

try {
    //for example error happens here
    trigger_error( 'OH MY GOD, I AM ON FIRE' );
} catch ( \Exception $e ) {

    //for debugging you can do like this
    $handler = new ExceptionHandler();
    $handler->handle( $e );

    /*
    * ExceptionHendler class comments
    * It is mostly useful in debug mode to replace the default PHP/XDebug
    * output with something prettier and more useful.
    * so i suggest to create json response
    * and replace this code $handler = new ExceptionHandler();
    * $handler->handle( $e );
    */
    return new JsonResponse(
        array(
            'status' => 'error', 
            'message' => $e->getMessage()
        )
    );
}

使用silex你可以做下一件事

ErrorHandler::register();
//register an error handler
$app->error(function ( \Exception $e, $code ) use ($app) {

    //return your json response here
    $error = array( 'message' => $e->getMessage() );

    return $app->json( $error, 200 );
});

答案 1 :(得分:1)

我今天遇到这种情况,并找到了一种与Silex绑定致命错误的方法。

首先,您需要使用Silex绑定异常处理程序:

$app->error(function (\Exception $exception, $code) {
    // Something that build a nice \Symfony\Component\HttpFoundation\Response. This part is up to you.
    $response = MyExceptionFormatter::format($exception, $code);

    // A Silex exception handler must return a Response.
    return $response;
});

现在,我们使用demoSymfony Debug Component

// Convert simple errors into nice Exception, automaticaly handled by Silex.
Symfony\Component\Debug\ErrorHandler::register();

// Now, the hard part, handle fatal error.
$handler = Symfony\Component\Debug\ExceptionHandler::register($app['debug']);
$handler->setHandler(function ($exception) use ($app) {

    // Create an ExceptionEvent with all the informations needed.
    $event = new Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent(
        $app,
        $app['request'],
        Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST,
        $exception
    );

    // Hey Silex ! We have something for you, can you handle it with your exception handler ?
    $app['dispatcher']->dispatch(Symfony\Component\HttpKernel\KernelEvents::EXCEPTION, $event);

    // And now, just display the response ;)
    $response = $event->getResponse();
    $response->sendHeaders();
    $response->sendContent();
    //$response->send(); We can't do that, something happened with the buffer, and Symfony still return its HTML.
});