How would i remove the lengthy stack trace errors that Laravel uses by default? (i understand it is using "Ignition")
Some resources that i've found that did not help:
this thread only mentions how to disable error reporting altogether
when i write anything in a render() method in the app/Exceptions/Handler.php like mentioned here i only get error 500 without any output.
person in this thread even suggests writing your own Laravel bootstrap application instead of using the default one, breaking Laravel framework semantics, but that's just plain mad.
I have also tried looking at configuration values for Ignition by publishing ignition config file via
php artisan vendor:publish --provider="Facade\Ignition\IgnitionServiceProvider" --tag="ignition-config"
But that file has nothing to configure, the only thing you can do is to hide the "share" message in the error.
I just want a simple classic php error page with file/line/error, no stack traces, or no html markup. The error page makes it really difficult to debug output in anything else than a web browser.
Simply override the render() method in your app's exception handler. Per comments in the base class, this method must return a response object.
public function render($request, \Throwable $e)
{
return response()->view("exception", ["exception" => $e]);
}
Then make your blade view look however you want.
<!doctype html>
<title>Exception</title>
<body>
<p>
Exception of type <code>{{ get_class($exception) }}</code>
thrown in <code>{{ $exception->getFile() }}</code>
on line <code>{{ $exception->getLine() }}</code>.
</p>
</body>
Related
I need to throw an Exception inside service
$isLangExist = $this->em->getRepository('TranslationBundle:Language')->findOneBy(array(
'locale' => $this->request->getMasterRequest()->getLocale()
));
if (!$isLangExist) {
throw new createNotFoundException('you are using unavailable langage');
}
but I got this page is not workinc in prod env
how can i show 404 page with createNotFoundException or any another Exception type
thanks
It looks like your code is not correct. I would expect it to look like this:
if (!$isLangExist) {
throw $this->createNotFoundException('you are using unavailable langage');
}
The method is part of the abstract base controller you can use, but it's not mandatory. What should work in cases where you don't extend this controller is:
if (!$isLangExist) {
throw new NotFoundHttpException('you are using unavailable langage');
}
Your problem goes beyond the above code, because you don't throw the exception inside a controller as I expected. You throw it inside a Twig extension. This exception will interrupt rendering, which is why the error is not converted into a 404 exception and instead is treated as a 500 error. Potentially you will see other 500-errors with your extension whenever one of the queries fails, which is probably not what you want. Addressing this issue likely requires rethinking how you use these global twig variables.
You could try moving the templates that use these variables into separate templates being rendered by a dedicated controller using sub requests or ESI:
{{
render(controller(
'AppBundle:Global:_listCategories',
{
'locale': app.request.attributes.get('_locale')
}
))
}}
Another solution might be to set these with null or an error-object whenever something fails and then react to these "alternative" results in your template, which is not what I would prefer.
There are probably many other ways to tackle this. The gist is: rendering error are different than http-exceptions thrown by controllers services. You have to ensure that your templates can either be rendered despite these missing/faulty variables or deal with these missing parameters before rendering the templates, e.g. in an event listener.
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 have a new project to inspect and manage. It is written with ZF1.
There are views, contained in .phtml files which contain following code:
echo $this->partial('error.phtml', array('error'=>$this->error));
My issue is that no business logic, controller code or other application code sets
that error variable explicitly. I am hunting it down and need to figure out if this
error is set by Zend Framework itself.
My main lead is the CallbackHandler.php in Stdlib folder. It seems to be responsible for setting
an error flag to true, when its errorHandler is executed. Problem is that I'm not sure.
My second guess is that it is dead code and if $this->error is simply not used anywhere.
Any guesses ?
It depens on kind of displayed error.
For system errors write something like ErrorController and redirect to error page after handling of exeption(in dev mode you can add displaying of stack traces, etc).
For form validation "errors" there is build-in instruments to display it in forms.
I have been reading the following question here: CakePHP 2.0 - How to make custom error pages?
About creating custom views for exception handling in CakePHP 2.0+ and have been using it as a base to start doing the same in my own application hence starting my own question.
However I'm not following the logic. For example how does the Throw NotFoundException know to call the notFound method in the Errors Controller as I don't see any direct relationship in terms of the naming... Unless I'm missing the point?
In any case I'm looking to add 404, 403, and 401 errors and then be able to create custom views and call them using the exception handler throughout my app.
Can anyone shed more light on this? I'm using the latest version of Cake 2.1
So I have the following code:
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
public function notFound($error) {
$this->controller->redirect(array('controller' => 'errors', 'action' => 'error404'));
}
}
And I want to replace that redirect with rendering a custom error view:
I've tried:
$this->controller->layout = null;
$this->controller->render('/Errors/error404');
But that just shows a blank page... Can anyone help me out as I don't want to do the redirect and would much rather follow conventions and render actual views with the same url when getting errors.
Update: Also noticed that the custom views ONLY get called when exceptions are manually called in the controller, and not for actual errors such as domain.com/garbageurl or something else... So it doesn't seem to be doing what I thought!
Have a look at these files from core Cake:
Cake/Error/ErrorHandler.php
Cake/Error/ExceptionRenderer.php
Here's what's happening:
ErrorHandler::handleException() is your exception handler. It gets called when an exception is thrown.
ErrorHandler::handleException() calls ExceptionRenderer::__construct() (your custom exception renderer must extend ExceptionRenderer) which parses the name of the Exception that was thrown, and from that, sets $this->method.
ErrorHandler::handleException() then calls ExceptionRenderer::render() which uses call_user_func_array() to call the method whose name is $this->method.
I was just looking for the same thing and could not find a neat way to do this using AppExceptionRenderer. It just won't allow you to have separate error403 and error404 template files.
So I just did this in my /app/View/Errors/error400.ctp file instead...
<? if ($error instanceof ForbiddenException) { ?>
<h4>Whoops! The page you attempted to access
requires permissions that you don't have.</h4>
<? } else { ?>
<h4>Whoops! We couldn't find the page you were looking for, sorry!</h4>
<? } ?>
When a user accesses /user/validate without the correct post parameters, my Zend application throws a zend exception. (I get the standard "An Error Occurred" message, framed within my layout). This is intentional.
I am now trying to test that behaviour with PHPUnit. Here's my test:
/**
* #expectedException Zend_Exception
*/
public function testEmptyUserValidationParametersCauseException() {
$this->dispatch('/user/validate');
}
When I run the test I get a message saying it failed, "Expected exception Zend_Exception". Any ideas?
I have other tests in the file which are working just fine...
Thanks!
The Zend_Controller_Plugin_ErrorHandler plugin handles exceptions for you and the default Error_Controller forces a 500 redirect which may mean the exception you are testing for no longer exists. Try the following unit test and see if it passes:
public function testUnknownUserRedirectsToErrorPage()
{
$this->dispatch('/user/validate');
$this->assertController('error');
$this->assertAction('error');
$this->assertResponseCode('500');
}
If this works then it shows that by the time you are rendering the error view the exception will no longer exist as it is contained in the code before the redirect.
Well, maybe it simply fails because no exception is thrown?
Did you try running the test without "#expectedException Zend_Exception"? Is there an exception at all?