code: public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
      {
          if (($request->get('_format') == 'json'
              || 0 < strpos($request->headers->get('accept'), 'application/json')
              || 0 < strpos($request->headers->get('accept'), 'text/javascript'))
          ) {
              return new JsonErrorResponse('Authentication failed. Please try again.');
          }
  
          return parent::onAuthenticationFailure($request, $exception);
      }
docs: {@inheritDoc}