Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Allow error handlers to be overridden #32

Closed
akuzemchak opened this issue Jan 14, 2013 · 3 comments
Closed

[Request] Allow error handlers to be overridden #32

akuzemchak opened this issue Jan 14, 2013 · 3 comments

Comments

@akuzemchak
Copy link

There are certain cases where I think developers may want to override previously defined error handlers. For example, let's say I have my main app, and also a REST API. I may want to define a 404 handler for the main app that displays a page:

App::missing(function($e)
{
    return Response::make(View::make('errors.404'), 404);
});

If the request is to one of my API's routes though, I might want my 404s to do this:

App::missing(function($e)
{
    return Response::json([
        'error' => 'Method not found',
    ], 404);
});

From what I can tell, there is no way to override the default 404 handler if the request is to an API route. This was possible in L3 with Event::override() and it would be awesome to get an equivalent in L4.

Thanks for the consideration!

@cmenke
Copy link

cmenke commented Jan 14, 2013

Unless I'm missing something, I think you could do something like this in your start/global.php:

App::error(function(Exception $exception, $code)
{
    Log::error($exception);

    if ($code == '404')
    {
        if (Request::ajax())
            return Response::json(array('error' => 'Method not found', 'code' => 404));

        return View::make('error.404');
    }
});

@akuzemchak
Copy link
Author

I suppose that certainly would work for all intents and purposes... but say I were developing a package for distribution that needed to override an error handler (like for an admin bundle or something like that), it would be great to keep that code in the package's service provider, instead of needing to modify additional areas of the code.

@taylorotwell
Copy link
Member

Done.

gonzalom pushed a commit to Hydrane/tmp-laravel-framework that referenced this issue Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants