1

I only want to handle certain query exception codes. The rest I want to let go; so that I get the regular debug or Oops, something went wrong screen, depending on my environment.

I currently have the following code in my routes.php file, it seems to work. But is it the correct way of doing it?

// Query Exceptions
App::error(function(QueryException $exception)
{
    $allowedCodes = array(
        '23000',            // Integrity constraint violation
    );

    if (in_array($exception->getCode(), $allowedCodes))
    {
        return Response::view('errors.show', array('code' => 'query_error_' . $exception->getCode()));
    }
    else
    {
        App::error(function(QueryException $exception){});
    }
});

Update, here was the solution I ended up based on Jarek Tkaczyk's answer:

App::error(function(QueryException $exception)
{
    $allowedCodes = array(
        '23000',            // Integrity constraint violation
    );

    if (in_array($exception->getCode(), $allowedCodes) && !App::environment('local'))
    {
        Log::warning('QueryException', array('context' => $exception->getMessage()));
        return Response::view('errors.show', array('code' => 'query_error_' . $exception->getCode()));
    }
});
5
  • Your else does nothing. Commented Sep 19, 2014 at 9:58
  • But it does seem to work as I intend, exception codes other than 23000 gives an debug exception message. Is that because I am not returning anything? Commented Sep 19, 2014 at 10:02
  • 1
    Yes, it works as expected. Howeveer else does this: Register empty error handler, while you are already in the error handler. This piece doesn't re-throw the error, as you asked in the q title, that's all. Commented Sep 19, 2014 at 10:10
  • I see, so what I am really doing is letting the exception slide through if not the "allowed" exception code? Commented Sep 19, 2014 at 10:30
  • @JarekTkaczyk Post this as an answer and I'll accept it, and change the question title. Commented Sep 19, 2014 at 12:47

2 Answers 2

3

Your current code is the way to go with one exception: else code block does basically nothing - it registers another handler for the exception that is being handled right now.

Here's something to make it more clear:

App::error(function(QueryException $exception)
{
    $allowedCodes = array(
        '23000',            // Integrity constraint violation
    );

    if (in_array($exception->getCode(), $allowedCodes))
    {
        return Response::view('errors.show', array('code' => 'query_error_' . $exception->getCode()));
    }

    // no need for else, it will handle exception like usually - depending on the debug config
});

or you could rethrow the exception and do pretty much the same:

App::error(function(QueryException $exception)
{
    $allowedCodes = array(
        '23000',            // Integrity constraint violation
    );

    if (in_array($exception->getCode(), $allowedCodes))
    {
        return Response::view('errors.show', array('code' => 'query_error_' . $exception->getCode()));
    }
    else
    {
        throw $exception; // this will show plain exception

        // or display whoops pretty handler:
        App::getFacadeApplication()->{'exception.debug'}->display($exception);

    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for clearing that up for me, and also showing me how to properly throw exceptions :)
0

Yes, it's the right way to do it.

For the local environment you'd of course use environments as described in Laravel's documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.