I'm currently using Laravel and I'd like to be able to extend the withError call to log it everytime it's called. I know it goes through the RedirectResponse object in this call:
public function __call($method, $parameters)
{
if (starts_with($method, 'with'))
{
return $this->with(snake_case(substr($method, 4)), $parameters[0]);
}
throw new \BadMethodCallException("Method [$method] does not exist on Redirect.");
}
however this is part of the Illuminate (Laravel) vendor code and I Don't want to modify it in case I Want to update Laravel later. Is there a better way to do what I'm trying to do?
This should work.
App::error(function(Exception $exception)
{
Log::error($exception);
});
References:
http://laravel.com/docs/errors
http://fideloper.com/laravel4-error-handling
Related
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 am new in zendframework. I am using apigility for rest services and ApiProblemListner to return the response if any error occures.
I have one function in model and this function just through an exception using php exception to use in catch block
I am using model function as the utility function in controller to catch those Exception. While catching exception I am using as
try{
imageUploade(); // this function in model and throwing exception if any error
}catch(\Exception $e){
return new ApiProblemResponse(new ApiProblem(500 , $e->getMessage()));
}
if imageUploade() throw an exception if the image size is more then I am able to catch the exception in catch block. I tried echo $e->getMessage(); and its printing the exception bt if I use new ApiProblem(500 , $e->getMessage()) it is not retuning the json error response with the 500 message. It is returning nothing. even it is not showing any error.
Seems like it is unable to render the error with this class. I am not sure if any event needs to add.
I have tried to search for documents but unable to find it.
Thanks in advance.
Normally it should work if you return an ApiProblemResponse straight from a controller action.
Are you sure your Api-Problem module is active?
Try once like this:
<?php
Application\Namespace;
use ZF\ApiProblem\ApiProblem;
use ZF\ApiProblem\ApiProblemResponse;
class IndexController
{
/**
* #return ApiProblemResponse
*/
public function indexAction()
{
return new ApiProblemResponse(new ApiProblem(500, 'test'));
}
}
If that doesn't work then I think your Api-Problem module is not running or the exception is never caught.
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
I have a query with try-catch block. I am going to create object and if it fails it will throw error message or say redirect on some other page.
Code
function __construct() {
try{
$this->ServiceObj = AnyClass::getService('XXX');
}
catch{
return Redirect::to('/');
}
}
public function MyOwnFunction() {
$getValueofCode = $this->_ServiceObj->set($endPoint_Url); //it's only example method not want to set anything
}
Is this correct way to define and use the try catch block in a constructor? Can I use try & catch block in construction? If NO than is there a better way to achieve this?
Thanks in Advance!!!
You can definitely use try catch in a constructor, just don't forget to specify the exception class you want to catch like catch (\Exception $e). However Laravel will not process the returned redirect. (By definition constructors shouldn't even return something)
An easy fix for this would be calling send() on the redirect. This will return it immediately to the client and stop the app.
try{
$this->ServiceObj = AnyClass::getService('XXX');
}
catch(\Exception $e){
Redirect::to('/')->send();
}
But since you mentioned a login page, you might be better of using a before filter to check and redirect. Like the built in auth filter does.
A third solution would be App::error. In app/start/global.php you could do something like this:
App::error(function(FooBarException $exception)
{
return Redirect::to('/');
});
Of course this only works if your exception is specific enough (replace FooBarException with the exception you want to catch)
I'm trying to do a redirect from my service class. I don't want to return the url to the caller because it's not being accessed directly from controller and it should return other data in some cases.
However I found that i can do the redirect this way:
Redirect::away('http://someexternalurl.com')->send();
This seems to work fine - the user gets redirected to the url I entered. The problem is that in this case the app "lives on" so following commands get executed. That's not what I need.
If I die(); immediately after that my session changes don't seem to be "saved".
Is there a way to make a redirect and stop the App immediately after that without simply "killing" it?
Basically is there a way to rewrite this code
Session::forget('myval');
Redirect::away('http://someexternalurl.com')->send();
mail('my#mail.com', 'Test', 'Still running');
So that myval would be gone from the session, the user would be redirected to the url and mail would not be sent (actually anything after the redirect shouldn't be executed)?
Thank you
The solution is to throw an Exception and adapt the render method of the Exception Handler.
In Your Controller:
// app/Controllers/DashboardController.php
public function index()
{
\App\Services\Test::test();
return view('dashboard');
}
My example service :
// app/Services/Test.php
<?php
namespace App\Services;
class Test {
public static function test() {
throw new \App\Exceptions\TestException('test');
}
}
In your Exception Handler :
// app/Exceptions/Handler.php
public function render($request, Exception $exception)
{
if ($exception instanceof TestException) {
return redirect(url('/?error=' . $exception->getMessage()));
}
return parent::render($request, $exception);
}
My example Exception (you can name it whatever you want), it just needs to extend the default Exception.
<?php
namespace App\Exceptions;
class TestException extends \Exception {
}
It looks like you just need to return on you Redirect?
return Redirect::away('http://someexternalurl.com')->send();
Here's the solution for Laravel 5.2. I reckon it would be work at all 5.*
abort(301, '', ['Location' => 'http://antondukhanin.ru']);
methodThatWouldntBeExecuted();
the function throws HttpException, so that will interrupt execution of any next commands