Reputation: 2365
I've been throwing Exceptions when I wanted to halt an action during backend processing. I had forgotten that I could use the Laravel abort() method. Is there any reason to use abort() over throwing an Exception? Does it pretty much do the same thing?
I'm also asking because I noticed that while I'm tailing my logs, abort() doesn't show the stack trace, but throwing the Exception does. I don't need the stack trace in these cases, because I know why it's failing. I also don't want the logs to grow huge from these known failures.
Upvotes: 4
Views: 3162
Reputation: 41
In Laravel 5.1 a HttpException is not reported (logged) because it's included in the Exception Handler's $dontReport
array. Remove it from the array, and it will be logged.
In Laravel 8 a HttpException is also not reported, but it's because the exception type is added to $internalDontReport
in the framework's Handler. You could add an $internalDontReport
array to your applications Handler
without HttpException::class
to override it.
However, a HttpException is not reported for a reason. You probably don't want 400, 401, 403, 404s and so on in your log. At least not by default.
Server errors (status code 500 and above) are likely more important to log, as they indicate something went wrong that perhaps could have been avoided. I.e. by validating the request better you could have given the user a validation error instead.
This means if you use the abort(500)
helper the user will get a 500 error in the browser, but it won't be logged because abort throws a HttpException.
To summarize, you probably want to use abort()
or throw new HttpException()
for 400-level errors as long as you don't want logging. For 500-level errors, throw new MyCustomException()
will be logged as long as it does not extend HttpException
.
If you don't want to rely on Laravel automatically logging some exceptions (but not necessarily all exceptions), you could always call Log::error("Some descriptive message");
before you throw an exception or use abort()
.
Upvotes: 2
Reputation: 6432
Let's have a look at the code:
/**
* Throw an HttpException with the given data.
*
* @param int $code
* @param string $message
* @param array $headers
* @return void
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function abort($code, $message = '', array $headers = array())
{
if ($code == 404)
{
throw new NotFoundHttpException($message);
}
throw new HttpException($code, $message, null, $headers);
}
So it appears that the abort
method is simply a helper which throws a NotFoundHttpException
if the code 404
is passed, otherwise it throws an HttpException
.
I'm not sure why throwing an Exception
would result in logging while using abort()
does not. You might want to check your exception handlers and see if different types of exceptions are being caught and handled differently.
You shouldn't be worrying about the size of your log files. Storage is extremely cheap these days and text takes very little space. The knowledge you gain from your logs will far outweigh their physical cost.
Upvotes: 10