I have project on Laravel 5, and I need to do async request via jQuery's $.ajax method.
Laravel can catch exception, and then it render special error template with it's own styles and markup.
But for async requests this html-code is redundant.
Is there a way to generate error response without laravel's markup on async requests?
I guess you wanted this to write the web service.
To handle this
Goto app/Exceptions/Handler.php :
And change this function
public function render($request, Exception $e)
{
return parent::render($request, $e);
}
to
public function render($request, Exception $e)
{
if ($this->isHttpException($e))
{
return $this->renderHttpException($e);
}
else
{
return parent::render($request, $e);
}
}
Also if you need to customize in the webview
Change your 404 blade \resources\views\errors\404.blade.php here
Related
I'm using Laravel as a backend API for my react app. I want to return an error message if the token is either expired or revoked access. However, on my Authenticate middleware, I only able to catch AuthenticationException. This exception only returns the message "Unauthenticated." Below is my current code inside Authenticate middleware
public function handle($request, Closure $next, ...$guards) {
try {
$this->authenticate($request, $guards);
} catch (AuthenticationException $e) {
$data['token'] = false;
return ResponseServices::error(__('Auth.token_expired', []))
->data($data)
->toJson();
}
return $next($request);
}
I tried to catch the exception using OAuthServerException and Throwable but not able to catch it. Only possible to catch AuthenticationException.
However, if I dd Throwable inside handler class on reportable function, I able to get this information
Is it possible to get the hint message inside my middleware?
Im testing my laravel rest API with postman, my end point only allow POST method but when i test it with GET method the body return webpage instead of json. How could i handle the response to return json although the response is 405?
You can handle it in function render of app/Exceptions/Handler.php
Ex:
public function render($request, Exception $e)
{
if ($e instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException) {
return response()->json(['message' => $e->getMessage(), 405);
}
return parent::render($request, $e);
}
The docs for it: https://laravel.com/docs/5.8/errors#render-method
I think Laravel has the response as JSON for it by default. You also can add to headers of request.
Content-Type: application/json
Accept: application/json
I am trying to implement exception handling in my application. For this Laravel framework has its own mechanism to handle the exception using report and render method. But to implement exception I need to track the source from where the exception has been raised e.g. specific page, route etc. For this I need to pass the url to report and render method but unable to do so. What needs to be done in order to implement this in below report and render function.
public function report(Exception $e)
{
parent::report($e);
}
public function render($request, Exception $e)
{
/* Token mismatch Exception handler start */
if ($e instanceof \Illuminate\Session\TokenMismatchException) {
return response()->view('errors.sessionExpire', [], 500);
}
/* Token mismatch Exception handler start */
return parent::render($request, $e);
}
As you can see from your own example, you have an instance of Request in the argument list. And Request has all request-specific details like current route, URL and so on.
$request->url(); // Current request URL
$request->fullUrl(); // With query parameters
$request->route(); // Get the route closure for this request path
You can also create your own exception classes that accept as many parameters as you wish!
And the less comfortable way already mentioned – you could go through the exception trace.
You need to use Exception::getTrace
var_dump($e->getTrace());
above line will give you all details regarding exception.
public function report(Exception $e){
echo '<pre>'; // add this line
print_r($e->getTrace()); // add this line
parent::report($e);
}
I'm looking to handle a MethodNotAllowedException. I've viewed other answers available on here that to create what i think should handle this in the exceptions/handler.php class. This is what i came up with.
public function render($request, Exception $e)
{
if ($e instanceof MethodNotAllowedHttpException) {
\Auth::logout();
\Session::flush();
return redirect()->('/')->withErrors(['error' => 'Something went wrong']);
}
return parent::render($request, $e);
}
However where i used to get an error before, all i recieve now is a blank page on the page where i usually recieve an error and a user is not logged out nor are they redirected. Am i placing this handler in the right place and if so, is the function shown below correct?
Thanks
I would like to show 404 page in Laravel 5 while MethodNotAllowedHttpException throws.
Can anyone help me in this regard?
Add this to app/Exceptions/Handler.php:
public function render($request, Exception $e)
{
if ($e instanceof MethodNotAllowedHttpException)
{
abort(404);
}
return parent::render($request, $e);
}
Then edit resource/views/errors/404.blade.php to personalize the page.
Simple: all you have to do is create a template at resources/views/errors/404.blade.php.
You can create views for other HTTP status codes if you’re feeling that way inclined, such as a 403.blade.php for Forbidden exceptions and so on.
I'm working on Laravel 5.2 and the answer given here worked for me.
You need to modify the render method in the app/Exceptions/Handler.php.
public function render($request, Exception $e)
{
if ($e instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException)
{
return response(view('errors.404'), 404);
}
return parent::render($request, $e);
}
abort(404) did not work for me. Looks like this method is removed in Laravel 5.
That's a 405 error, easiest way would be to create a new view in resources/views/errors/405.blade.php..
If you need fine control over what is displayed then you'll have to use the Handler.php to modify what's returned.