How to catch authentication errors in Silex?
<p>I am developping a Rest API with Silex framework and I use  <code>SecurityServiceProvider</code>. However, if a user sends bad credentials throw HTTP, I don't know how I can catch the error to display something like this :</p>

<pre><code>{"status_code":401, "error_message":"Bad credentials"}
</code></pre>

<p>Below is a part of my code:</p>

<pre><code>$app-&gt;register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' =&gt; array(
        'default' =&gt; array(
            'pattern' =&gt; '^.*$',
            'stateless' =&gt; true,
            'http' =&gt; true,
            'users' =&gt; $app-&gt;share(function() use ($app) {
                return new App\Providers\UserProvider($app['db']);
            }),
        ),
    ),
));

// ...

$app-&gt;error(function (\Exception $e, $code) use ($app) {
    // This code is never executed in case of Bad credentials

    // ...

    return new JsonResponse($contentResponse, $statusCode);
});
</code></pre>

<p>Thank you in advance</p>

<p><strong>EDIT</strong></p>

<p>I've seen I can use <code>$app['security.authentication.failure_handler.'.$name]</code> but it is never handled in my case. </p>

<pre><code>$app['security.authentication.failure_handler.default'] = $app-&gt;share(function ($app) {
     // This code is never executed
     return new MySuccessHandler();
});
</code></pre>

<p>Is it because of <code>'http' =&gt; true</code>?</p>