Can anyone explain what all errors can App::error of Laravel handles..?
For eg :
[404] unable to access the url
[500] internal server error
If I have a db connection error or any missing parameter error it doesn't come under this class. How can I handle those major errors..?
Please help in listing all the possible cases..
The error handling isn't specifically tied to HTTP status codes.
App::error handles any uncaught exceptions. A not found error is just a NotFoundHttpException.
http://laravel.com/docs/4.2/errors#handling-errors
A 404 exception can be easily caught with this shortcut method:
App::missing(function($exception)
{
// Example response
return Response::view('errors.missing', array(), 404);
});
http://laravel.com/docs/4.2/errors#handling-404-errors
If you don't use the App::missing syntax, a not-found type of exception should bubble up to the App::error handler.
Related
I am using latest version laravel(5.6)
Now in my code whenever an exception occurs laravel treating it as a fatal error, stop executing instantly and displaying the error message in some template.
But I don't want that, I want to handle exceptions and display some custom error messages
I found some ways like
changing the APP_DEBUG value in the .env file to false. But this also displays another
page with the message "whoops!some this want wrong";
In Handler.php which is in app/Exceptions, I had put some exceptions in not report zone. But the app is still reporting them
Custom HTTP Error Pages
Laravel makes it easy to display custom error pages for various HTTP
status codes. For example, if you wish to customize the error page for
404 HTTP status codes, create a resources/views/errors/404.blade.php.
This file will be served on all 404 errors generated by your
application. The views within this directory should be named to match
the HTTP status code they correspond to. The HttpException instance
raised by the abort function will be passed to the view as an
$exception variable.
https://laravel.com/docs/5.6/errors#custom-http-error-pages
Really you want to be handling your exceptions. Wrap the code in a try catch and you can do all manner of things (e.g. email / slack / log). Once you have handled the exception you can still use custom http error pages inside the catch so the end user get's a friendly message on a nicely designed page. There is even a report helper built in to allow you to externally log and continue on processing the code.
#Devon's above answer re: Custom HTTP Error Pages gets you exactly what you want also.
Please note few important points :
The App\Exceptions\Handler class is where all exceptions triggered by your application are logged and then rendered back to the user. This class has two method report() and render(), both has their own responsibility.
The report method is used to log exceptions. By default, the report method passes the exception to the base class where the exception is logged. However, you are free to log exceptions however you wish. For example, if you need to report different types of exceptions in different ways, you may use the PHP instanceof comparison operator
The render method is responsible for converting a given exception into an HTTP response that should be sent back to the browser. By default, the exception is passed to the base class which generates a response for you. However, you are free to check the exception type or return your own custom response.
As in your case you want to return custom message for exception, inside render() you may use the PHP instanceof comparison operator and return you own logic.
Example :
if($exception instanceof PostTooLargeException || $exception instanceof FileException){
return response()->json([
'error' => true,
'error_message' => "The file you are trying to upload exceeds the maximum limit. Please try to upload a smaller file."
],200);
}
Go through https://laravel.com/docs/5.6/errors for more datails
I'm trying to setup custom error handlers in Handler.php but when I try to get the error message when a Request validation isn't met I get an empty response.
public function render($request, Exception $e)
{
dd($e->getMessage());
return parent::render($request, $e);
}
My rules are set to:
'min:10'
So when I don't provide parameters of length 10 I want it to output that error within $e->getMessage() so I can do whatever I want to do with it, but the error is always empty. What am i doing wrong?
Exception will not give you errors that occurs for Validator.
Validator error are sent on its object and by errors() method. So, after calling the errors method on a Validator instance, you will receive an Illuminate\Support\MessageBag instance, which has a variety of convenient methods for working with error messages.
For more details: https://laravel.com/docs/5.2/validation#custom-error-messages
When im attempting to deploy things using laravel 4.1's SSH/Remote classes occasionally i get this exception
[2014-01-03 18:26:21] production.ERROR: exception 'ErrorException' with message 'Connection closed by server' in /home/{user}/{location}/deploy/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php:918
I was wondering if there is a way to detect if it has failed to connect in order to attempt the connection again and or multiple times?
Any ideas?
Laravel uses phpseclib for its SSH library. On connection failure, phpseclib executes user_error('Connection closed by server'); (see Net/SSH2.php, line ~911). Laravel has a global error handler that picks this up, and logs it (as you saw in your question).
Unfortunately, phpseclib triggers errors, instead of throwing exceptions. If they were exceptions, you could add a new condition to Laravel's error handling:
App::error(function(Exception $exception){
Log::error($exception);
if($exception instanceof PHPSecLibException){
// Let's handle this
}
});
This definitely would be the "right way" to do it, but these aren't true exceptions (they're generic Laravel exceptions that are generated on your behalf when errors are triggered).
Luckily, Laravel translates errors into exceptions on your behalf. See src/Illuminate/Exception/Handler.php (lines ~129-135). So we could just add a conditional based on the info you do have:
App::error(function(Exception $exception){
Log::error($exception);
if(($exception->getMessage() == "Connection closed by server") &&
($exception->getFile() == "/home/{user}/{location}/deploy/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php")){
// Let's handle this
}
});
Take a look at the Exception methods available.
I am using the Stripe API and Laravel together. If Stripe detects an error charging the card (such as using a test credit card number that throws an invalid security code error), the API bindings are supposed to throw an exception, which they do. The problem is, I am having issues catching the exception before Laravel throws up the error 500 page (I am trying to perform a redirect with an error message instead).
The code I've written is available on Pastebin: http://pastebin.com/ZaW2xbbt
The behavior I'm expecting is for the catch to fire and the redirect to be performed, but instead, I get the stack trace with the message and "Unhandled Exception". That's confusing me because I am handling the exception.
Variables such as $customer are valid and have been defined previously. Any ideas what's going on?
For any future viewers, here's an article on error handling in laravel 4.
Laravel 4 lets you catch Exceptions by exception type. For instance, you can handle Symfony's HttpException and of its sub-classes by adding this to your code:
// Catch HttpException, NotFoundHttpException, etc etc
App::error(function(HttpException $exception, $code, $fromConsole)
{
...
});
Symfony HttpExceptions (used in Laravel) can be found here.
You can also throw this in a ServiceProvider:
<?php namespace My\Namespace;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\HttpKernel\Exception\HttpException;
class MyServiceProvider extends ServiceProvider {
public function register()
{
$this->app->error(function(HttpException $exception, $code, $fromConsole)
{
...
});
}
}
Hope that helps!
Generally, all errors logged by Laravel are logged under storage/logs folder
Anyway, the 500 error could be a syntax/parse error, in such case the Laravel framework could be not yet loaded when the error occurs and if so, the exception is not lhandled by Laravel.
In this case you should access the apache/vargrant/whatif php error log in some way (dependently on your server capabilities and configuration), in my personal cases I have configured the server to put that logs in a /storage/logs/error_log.txt file such that I can access them as other Laravel server logs
Note that in Laravel 5, you have app/Exceptions/Handler.php as entry point for customize exception handling/reporting
https://laravel.com/docs/5.7/errors#the-exception-handler
I have an API that uses Zend_Json_Server and defines a class that handles the requests.
However, how can I return an error, so that I can set the message AND the error code?
When I throw a new exception, I can only set the message. The exception code is not used.
I found the solution:
The error code has to be somewhere between -32099 and -32000.
So you can throw an Exception and pass one of those valid error codes.
Why don't you call the fault method on Zend_Json_Server?