Coder Social home page Coder Social logo

exceptions-bundle's Introduction

WebfactoryExceptionsBundle

A Symfony Bundle with some Twig templates and blocks to create helpful, user friendly error pages.

⚠️ Please note: At webfactory, we do no longer use this bundle in new projects, since its main functionality was merged into Symfony 2.6 and we prefer to have project-specific templates. However, we still use the wording that we provide with this bundle.

Basic Usage

First, follow the Symfony Documentation to learn how to create and preview custom error pages. Been there, done that? Fine!

Let's say your generic error page extends the base layout of MyWebsiteBundle. Then you may want to have your error.html.twig to look something like this:

{# error.html.twig #}
{% extends '@MyWebsite/Layout/base.html.twig' %}
{% use '@WebfactoryExceptions/Exception/blocks.html.twig' %}

{% block myMainContentContainer %}
    {{ block('webfactory_exceptions_standardExceptionPage') }}
{% endblock %}

The webfactory_exceptions_standardExceptionPage block has headings, the translated exception description and provides the user with a list of alternatives what they can do next: get back (simulating a browser back), get to the homepage, get to the contact page or google the domain. It may look like this:

Sample rendering of the webfactory_exceptions_standardExceptionPage block

Links to Homepage and Contact Page

A default block in the bundle provides a link to the homepage with the default target /. If your application does not start at /, you need to set the variable homepageUrl.

Also, you may want to set the variable contactUrl to get a link to your contact page in the listed alternatives.

{# error.html.twig #}
{% extends '@MyWebsite/Layout/base.html.twig' %}
{% use '@WebfactoryExceptions/Exception/blocks.html.twig' %}

{% set homepageUrl = "https://www.webfactory.de" %}
{% set contactUrl = path('name_of_a_route') %}

{# your blocks and definitions... #}

Filling in Blocks of Base Layouts

If your base layout already features blocks you need to fill with exception specific content, you can do it this way:

{# error.html.twig #}
{% extends '@MyWebsite/Layout/base.html.twig' %}

{% use '@WebfactoryExceptions/Exception/blocks.html.twig' with
        webfactory_exceptions_error_title as title,
        webfactory_exceptions_error_headline as stage_headline
%}

This loads the webfactory_exceptions_error_title block directly into the title block of your base layout, as well as the webfactory_exceptions_error_headline block into the stage_headline block.

Happy error-styling!

Credits, Copyright and License

This bundle was started at webfactory GmbH, Bonn. It was inspired by the blog post How Symfony2 turns exceptions into error pages and how to customize those.

Previous releases of this bundle contained extra code that would help during development to preview your error pages. This code, however, could be merged into the Symfony core in Symfony 2.6, so we could finally remove it from this bundle. Read the announcement when you're interested in historic details.

Copyright 2012-2022 webfactory GmbH, Bonn. Code released under the MIT license.

exceptions-bundle's People

Contributors

dritter avatar drthief avatar fabianschmick avatar jroenf avatar maltewunsch avatar marioblazek avatar matthimatiker avatar mpdude avatar nicodmf avatar polarbirke avatar relthyg avatar sebastiankugler avatar weaverryan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

exceptions-bundle's Issues

Got routing error while accessing to an error page

Hi,

got this error while accessing to an inexistant page :

[2015-08-19 12:45:21] app.ERROR: An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "toto_home" as such route does not exist.") in "SiteTotoBundle:Modules/materiaux:nav-main.html.twig" at line 18. [] []

in my routing.yml hte route exists

toto_home:
    pattern:  /
    defaults: { _controller: SiteTotoBundle:Default:Home}

Don't understand why I have this error...

Registering bundle failed?

Hi there,

after installing your bundle, I wanted to clear my cache (with & without warmup).
On the cli I'll get this error:

[Symfony\Component\Config\Exception\FileLoaderLoadException]
Cannot load resource "@WebfactoryExceptionsBundle/Controller/TestController.php". Make sure the "WebfactoryExceptionsBundle" bundle is corr
ectly registered and loaded in the application kernel class.

Same on http://{host}/app_dev.php/_error/404
Everything else is working.
I added the mentioned code at my AppKernel and the routes in routing_dev.yml.

Well, the Routes are fine and in phpStorm (with sf2 Plugin) they are all accessible.
Do you have any idea, how I can solve this issue?

Thanks,
Erik

MIsleading documentation about blocks.html.twig

Hi!
I've been following you documentation for integrating your (nice) error page on production.

Following the docs, everything suggest to put the bundle only on dev environment: add it to composer require-dev, register the bundle only when the kernel is in test/dev bundle. This is correct as long as you don't use block.html.twig. If you do, when going to production all error pages (4xx, 5xx) result in a ugly white-screen 500 page, since Twig fails to load the template.

So, what is the "official way" of dealing with this? Copying the file under Acme/MyBundle? Register the whole bundle even for production use?

For now, I worked around the issue by copying the file, since IMHO the bundle is not required on production.

(By the way: thanks for this useful bundle!)

Add a message to the exception

It would be nice to be able to add a message to the generated exception. I have a template that displays exception.message, and I can't test it easily right now.

develop through PRs

could you please develop through PRs so we can follow the changes and get notifications to help maintain/contribute to this bundle? thanks

Add route name.

Please, add the route name to TestController::testErrorPageAction() like this:

    /**
     * Let the error page for the HTTP status code $code be rendered in the format $format.
     *
     * @Route("/{code}/{_format}", defaults={"_format" = "html"}, requirements={"code" = "\d+"}, name="webfactory_exceptions")
     * @param int $code HTTP error status code
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
     */
    public function testErrorPageAction($code)

Sometimes we need to check route name and do some actions. Now route is called webfactory_exceptions_test_testerrorpage.

How to use the Bundle

Hi,

I think I'm missing something really obvious - I can see error pages for 404 etc using the _error syntax you mention, but how do I see error pages for exceptions that are thrown? Am I supposed to write my own test controller, or use yours, and if yours how do I configure it in twig config?

thanks
Dave

Document building blocks for error pages

This bundle also contains some building-blocks for swiftly creating helpful, user-friendly error pages.

We'd need to document those and change the naming of some of the blocks/macros to English.

(@MalteWunsch you might find a good starting point in our Wiki, feel free to move/copy that to here.)

Replacing the default Exception Controller causes problem with viewing Error Pages

Symfony:
2.5.0

Custom Exception Controller:

class ExceptionController extends \Webfactory\Bundle\ExceptionsBundle\Controller\ExceptionController implements ContainerAwareInterface

Application's configuration:

# Twig Configuration
twig:
    exception_controller: my_custom.controller.exception_controller:showAction

Effect:
Cannot display the Error Pages in "dev" environment. Only exception without message is diplayed:

HttpException:

    in /vendor/webfactory/exceptions-bundle/Webfactory/Bundle/ExceptionsBundle/Controller/TestController.php line 36
    at TestController->testErrorPageAction('403')
    at call_user_func_array(array(object(TestController), 'testErrorPageAction'), array('403')) in /app/bootstrap.php.cache line 2947
    at HttpKernel->handleRaw(object(Request), '1') in /app/bootstrap.php.cache line 2909
    at HttpKernel->handle(object(Request), '1', true) in /app/bootstrap.php.cache line 3058
    at ContainerAwareHttpKernel->handle(object(Request), '1', true) in /app/bootstrap.php.cache line 2308
    at Kernel->handle(object(Request)) in /web/app_dev.php line 34

Question:
I have created empty Symfony 2.5.0 project / application and there is the same behaviour.
What I have done wrong?

Cannot seem to make it works

Hi,
I followed your install documentation however in my dev env, when I access the following url : http://localhost/app_dev.php/_error/404, I have the following exception thrown :

HttpException:
in vendor/webfactory/exceptions-bundle/Webfactory/Bundle/ExceptionsBundle/Controller/TestController.php line 36
at TestController->testErrorPageAction('404')
at call_user_func_array(array(object(TestController), 'testErrorPageAction'), array('404')) in app/bootstrap.php.cache line 2951
at HttpKernel->handleRaw(object(Request), '1') in app/bootstrap.php.cache line 2923
at HttpKernel->handle(object(Request), '1', true) in app/bootstrap.php.cache line 3062
at ContainerAwareHttpKernel->handle(object(Request), '1', true) in app/bootstrap.php.cache line 2305
at Kernel->handle(object(Request)) in web/app_dev.php line 28

My error404.html.twig template seems to be working the right way in prod environment.

For information, I am running symfony v2.4.5.

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.