How to show customized 404 error page - Symfony2 - symfony

I have this in my controller:
if(some stuff) {
throw $this->createNotFoundException('message');
}
When I test it in dev env, I get the Symfony's page telling Exception detected with my text, but I can't test it in prod env :( I run this php app/console cache:clear --env=prod --no-debug and then replace only in the URL app_dev.php with app.php but the toolbar and Symfony's error page stay.
I created this file - app/Resources/TwigBundle/views/Exception/error.html.twig. So will it be rendered in prod?
This is in it:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>An Error Occurred: {{ status_text }}</title>
</head>
<body>
<h1>Oops! An Error Occurred</h1>
<h2>The server returned a "{{ status_code }} {{ status_text }}".</h2>
</body>
</html>
where should I give values for status_code and status _text? Or they are taken from the exception?
So in general what I want to do is when a condition in my contoller`s action is true, mine customized error page to be shown in prod env. Did I make it already, and if not, how to make it and how to see the result in prod env?

I also tried and i noticed that custom error pages placed in app/Resources/TwigBundle/views/Exception/error.html.twig worked only in prod environment.

Better late than never I guess..
You probably know about overriding of templates.
You can override any template you want.
So if you want to override the dev error page, just for debugging, then you should override the template:
exception_full.html.twig
You can do so by creating such a file in this folder:
app/Resources/TwigBundle/views/Exception
Now you will see your customized 404 in dev mode.

you can access your detected text with following way:
{{ exception.message }}

Read the doc http://symfony.com/doc/2.0/cookbook/controller/error_pages.html
and Customizing the 404 Page and other Error Pages
http://symfony.com/doc/2.0/cookbook/controller/error_pages.html#customizing-the-404-page-and-other-error-pages

It appears Symfony has a new way of allowing you to see the error page templates in your dev environment. Taken from their docs:
While you're in the development environment, Symfony shows the big exception page instead of your shiny new customized error page. So, how can you see what it looks like and debug it?
Fortunately, the default ExceptionController allows you to preview your error pages during development.
To use this feature, you need to have a definition in your routing_dev.yml file like so:
# app/config/routing_dev.yml
_errors:
resource: "#TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
Read more: Symfony Error Pages Manual

Related

EXCEPTION: No Directive annotation found on e

I tried to deploy an angular2-meteor project on meteor server.
The app works well locally. And I already did meteor reset before deploying.
Right now it shows a blank page and error:
EXCEPTION: No Directive annotation found on e
I also tried add https, but same error. What does this error mean? Or what can possibly cause this? Thanks
Please open this page and see Console for more error details.
UPDATE: log file I got using meteor logs xxx.
You should include -dev.js files (for example angular2.dev.js) for Angular2 so you'll be able to see more readable errors and especially on which class you have the problem...
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script> <-----
<script src="node_modules/angular2/bundles/router.dev.js"></script> <-----
<script src="node_modules/angular2/bundles/http.dev.js"></script> <-----
Most of time, you have such error when providing something wrong in the directives attribute of a component. For example providers...
Now I found why it shows these errors.
If you met similar problems like these:
EXCEPTION: No Directive annotation found on e
Or
ngDoCheck is not a function when using minified bundles
They are all related with UglifyJS. Right now there is no good way to solve them.
Check here and here

Styling Jekyll website with Bootstrap

I am trying to set up a website using Jekyll and GitHub Pages (first-timer), and most importantly, to style it with Bootstrap.
You can check what I have already done:
GitHub repository: https://github.com/thibaudclement/wallaby
GitHub page: http://thibaudclement.github.io/wallaby/ (check gh-pages branch)
Also, I followed this tutorial to import Bootstrap into the Jekyll structure.
Layouts and includes seem to work just fine, but I don't understand why my index.html does not get "styled" as it should, fetching information into the css/style.css file.
Any idea of what I am doing wrong?
Thanks.
The path to your css file is incorrect - it's not loading if you check your browser console
"Failed to load resource: the server responded with a status of 404 (Not Found) - http://thibaudclement.github.io/wallaby/style/site.css"
In _config.yml, you need to set baseurl: /wallaby.
And use {{ site.baseurl }} to load resources like this :
<link href="{{ site.baseurl }}/css/style.css" media="screen" rel="stylesheet" type="text/css" />.
See Jekyll documentation.

overridden in Symfony 2 error customization?

I am working on error pages in symfony2 but I am not successful in override this template.
Can any one describe it How I can override all exception pages. I want three pages error.html.twig , 403.html.twig, 404.html.twig
I do this way:
first create file in this position:
app/Resources/TwigBundle/views/Exception
app/Resources/TwigBundle/views/layout.html.twig
I put all twig exceptions file in Exception folder and write some custom code.
But I am some time success and some time got blank pages.
And this page is working in dev env but in prod env not show.
I want when error then error page , if 404 then 404 page error, if forbidden then 403 page.
Any one describe me or tell me how I can do this.
If write some code It's good for me ?
In other think I am all problem handle through RedirectExceptionListner service but I do not do this because this is redirect to error pages.
Thanks!
Just create error + number of error + .hmlt.twig in app/Resources/TwigBundle/views/Exception.
For the 404, the file would be error404.html.twig.
Hope it helps.
There are two steps to override error pages.
First of all: Follow tutorial on this page: http://symfony.com/doc/current/cookbook/controller/error_pages.html. In app/Resources/TwigBundle/views/Exception create file error.html.twig. You can also create error404.html.twig and other error pages. Content of the file is your decision.
Second step is to add assetic in your config.yml
assetic:
bundles: ['TwigBundle']
Without that you will get blank pages.
And of course, don't forget to clear the cache and reinstall assetics:
php app/console assetic:dump --env=prod --no-debug
How to know which error twig will be rendered? For me, the best solution is to create method in controller like:
function findQuestionOr404($questionId)
{
$survey = $this
->getDoctrine()
->getRepository('MyGreatBundle:Question')
->find($questionId)
;
if (!$survey) {
throw $this->createNotFoundException());
}
return $survey;
}
And then in action:
public function someAction()
{
$queston = $this->findQuestionOr404($id);
//...
}
This will throw 404 error (if there is no survey with particullar $id) and show error404.html.twig. Of course, you're free to throw any Exception class in your controller.

Assetic Route Not Found

I have a twig extension whose purpose is to collect a list of CSS and JS file paths given to it by function calls throughout a template hierarchy and then at the end of the twig template to take the output buffer and include these files in the <head> section of the page. For the most part it has been straightforward to implement.
In my service definition for the twig extension I am injecting the assetic.helper.dynamic service into it. The problem is when I call the javascripts() or stylesheets() method to get a URL for a CSS or JS file I get an error like this:
An exception has been thrown during the rendering of a template ("None
of the chained routers were able to generate route: Route
'_assetic_bd311c7' not found")
service.yml:
admin.twig.asset_extension:
class: Zing\Delta\AdminBundle\Twig\AssetExtension
tags:
- { name: twig.extension }
arguments: ['#assetic.helper.dynamic']
In my extension I am essentially doing this to get the URL for an asset:
$assetic_helper->stylesheets(array(
'#SomeBundle/Resources/public/js/jquery.tablesort.min.js'
));
I don't understand why the router can't find the routes or why assetic is setting up the routes.
The fix ended up being to run the following commands in the following order from the project root.
$ php app/console assets:install
$ php app/console assetic:dump
$ php app/console cache:clear

How to display the 404 error page in Symfony2 dev environment

I want to work on the 404 page from the dev environment. I customize 404 using this file : app/Resources/TwigBundle/views/Exception/error.html.twig
This prod url work correctly : mysite.com/404
But this one mysite.com/app_dev.php/404 throw a NotFoundHttpException and give me the dev debug page.
Is it possible to display the error page instead of debug page ?
UPDATE:
The official documentation has now a chapter about that : Testing Error Pages during Development
To display error pages, in web/app_dev.php change second parameter to false
$kernel = new AppKernel('dev', false);
After testing what you need, change it back.
UPDATE
Thanks #user2019515 for pointing that out - now (2.3 and up) there's a link to WebfactoryExeptionsBundle in Symfony docs and the method I wrote above should not be used.
As of Symfony2.6 in dev environment, you can use the following route:
/_error/404.html
Where 404 is the error code to test and html the format of the request.
To be able to use this features, make sure you have the following entry in your routing_dev.yml file:
# app/config/routing_dev.yml
_errors:
resource: "#TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
You need to override the exception_full.html.twig template on development.
app/Resources/TwigBundle/views/Exception/exception_full.html.twig
Symfony2 uses this template to provide you with as much debugging information as possible during development.
When the kernel is in debug mode, Symfony2 will use exception_full.html.twig, otherwise it will use the specific templates you override.
See vendor/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php, specifically the showAction() and findTemplate() functions for more details.
Also you can add routes to your routing_dev.yml file
error404:
path: /404
defaults:
_controller: FrameworkBundle:Template:template
template: TwigBundle:Exception:error404.html.twig
error500:
path: /500
defaults:
_controller: FrameworkBundle:Template:template
template: TwigBundle:Exception:error500.html.twig

Resources