Throwing an exception in a view causes a 500 response #3193

Closed
macnibblet opened this Issue Dec 10, 2012 · 10 comments

Comments

Projects
None yet
3 participants
Contributor

macnibblet commented Dec 10, 2012

Something has changed between 2.0.5 and the current dev-develop that causes a 500 response if a exception is thrown in a view.

Owner

weierophinney commented Dec 10, 2012

This is intentional. We have also added a new event, render.error, that you
can listen to in order to report the error or recover from it.
On Dec 10, 2012 4:24 AM, "Antoine Hedgecock" notifications@github.com
wrote:

Something has changed between 2.0.5 and the current dev-develop that
causes a 500 response if a exception is thrown in a view.


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/issues/3193.

Member

Ocramius commented Dec 10, 2012

@weierophinney doesn't this mean that ZF2 should at least provide a default listener attached to it? Or is there one already?

Owner

weierophinney commented Dec 10, 2012

Marco - we'll likely do that in the skeleton application. We need to
discuss what it should do - render errors could occur in the layout, which
makes providing a default view error page more difficult.

On Monday, December 10, 2012, Marco Pivetta wrote:

@weierophinney https://github.com/weierophinney doesn't this mean that
ZF2 should at least provide a default listener attached to it? Or is there
one already?


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/issues/3193#issuecomment-11193057.

Matthew Weier O'Phinney
matthew@weierophinney.net
http://mwop.net/

Member

Ocramius commented Dec 10, 2012

@weierophinney even just returning a plain string (after all, the view layer failed, we shouldn't use it again) works... Ok on having it in skeleton :)

Contributor

macnibblet commented Dec 10, 2012

I agree with @Ocramius on this one, since uncaught exceptions are most likely to occur when the page in development a simple error message and stacktrace is what is needed. Because i can't think of any good use case where i would want to throw an exception in a viewhelper or view that i want my application to handle.

Contributor

macnibblet commented Dec 11, 2012

Another thing seams to have changed since i added an event for my own exception handler to handle the render.error event. I want to respond with a 500 status code but with my own template / view (static html page) this does not seam to be possible?

Member

Ocramius commented Dec 11, 2012

@macnibblet should be possible by using the View object manually. I wouldn't do that though. If the view failed, then the entire view layer may be unusable for <set reason here>

Contributor

macnibblet commented Dec 11, 2012

@Ocramius even if you change the response code in the event that is triggered by the render.error to something else to get output it seams the Zend\View has committed sucicide and refuses to render anything.

Member

Ocramius commented Dec 11, 2012

Again, I don't think that Zend\View should be used if it failed once.

A string should be built (manually) in a listener and put (appended?) in the response object.

Owner

weierophinney commented Dec 20, 2012

A suggestion: instead of calling exit, return the response object. This
will bubble out of the event loop, and retains testability.

On Wednesday, December 19, 2012, Gary Lockett wrote:

for reference if anyone find this, here is a suggested implementation for
development (i.e. remove it, or implement something better for production
purposes):

public function onBootstrap(EventInterface $event)
{
    // Load event manager
    $eventManager = $event->getApplication()->getEventManager();

    // Attach listeners
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    $eventManager->attach(
            __NAMESPACE__, 'finish', array($this, 'renderError')
            );
}

public function renderError(MvcEvent $event)
{
    if ($event->isError()) {
        $response = $event->getResponse();
        $response->setStatusCode(200);
        exit;
    }
}

Of course, feel free to correct me, but still learning zf2 and it took me
a while to figure this out.

ise


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/issues/3193#issuecomment-11556618.

Matthew Weier O'Phinney
matthew@weierophinney.net
http://mwop.net/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment