Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Custom (http) exceptions
  • Recently I was trying to define a custom exception for 503 http status code, to my suprise, Fuel is of course allowing to do so, but in a very retarded way.

    I have created httpexceptions.php file with

    class HttpServiceUnavailableException extends \Fuel\Core\HttpException
    public function response()
    return new \Response('<h2>Service Unavailable - we will be back soon</h2>', 503);

    In the index.php I have added:

    catch (HttpServiceUnavailableException $e)
    $response = $routerequest('_503_', $e);

    Now, everything is fine until we have a _503_ route defined, in case we doesn't Fuel
    wont use a default response passed (<h2>Service Unavailable - we will be back soon</h2> in my case) like it does for 403, 404, 500 errors and throw a 404 error instead

    This happens because Fuel is forcing reserved modules for a request:

    $reserve_routes = array(
    '_root_' => $module,
    '_403_' => '_403_',
    '_404_' => '_404_',
    '_500_' => '_500_',
    $module => $module,

    Is there any specific reason doing so? This is very restricting for a developer.

    We need to have a route for our exception, otherwise you have an 404 instead of the default response defined.

    If you need to reserve some routes, maybe do so for all error codes globally like:

    '_error_(:code)_' then check if the route exist for a specific error code. If not - use a default response (if defined).
  • The reserve_routes snippet is only relevant in case of modules with their own route file, and it is there to make sure that when module routes are loaded, routes like _404_ are not converted to module/_404_.

    It has nothing to do with what you are experiencing.

    You get the 404 because although you have altered your index.php to handle your exception via a _503_ route, you haven't actually defined the _503_ route. So you get the 404 as a result of that route lookup.

    This in turn is because the routerequest closure also handles other route requests. The 404 you have is generated by

    elseif ($request)
        $response = Request::forge($request)->execute(array($e))->response();

    which fails, because $request is _503_ at this point, which is a non-existent route.

    So, either
    - remove that bit of code if you are not going to use custom routes
    - or define an _503_ in your routes
  • You did explain what I already know. I wrote the thread to either fix this or make a note of that in the docs. If someone would try to handle new http status errors the way it's done in Fuel core, he will fail. Returning a response object from the extended exception class is pointless then because it will never get used... and I seen few people did that already on github ;-) 
  • HarroHarro
    Accepted Answer
    I'll have to look into that, as I want to make sure not to break anything.
  • Can you try this closure in your index.php?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion