Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Custom exceptions
  • I’d like to create my own exception class to handle an API failure that I’m using throughout the site. I don’t a normal 500 error message to come up.

    I’ve created the action for it as per the action_404().

    I've also created a httpexceptions.php class
    namespace Fuel\Core;

    class HttpApiErrorException extends \HttpException
    {
    public function response()
    {
    return new \Response(\View::forge('500'), 500);
    }
    }

    And added it to my bootstrap.php
    'HttpApiErrorException' => APPPATH.'classes/httpexceptions.php',

    I’ve added a HttpApiErrorException catch to index.php
    $route = array_key_exists('_api_error_', Router::$routes) ? Router::$routes['_api_error_']->translation : Config::get('routes._api_error_');

    And added that route to my routes.

    What else do I need to to do?

    I’m getting this error

    ErrorException [ Error ]: Class 'HttpApiErrorException' not found at the point I throw it:
    throw new HttpApiErrorException;
  • Extended namespace was wrong, of course!
    class HttpApiErrorException extends \Fuel\Core\HttpException
  • My HttpApiErrorException isn't being caught by index.php
    catch (HttpApiErrorException $e)
  • I think it’s to do with the namespace, I can't figure out what namespace (and how) to load in my httpexceptions.php file?
  • HarroHarro
    Accepted Answer
    Your stuff should not be in the \Fuel\Core namespace, that is a namespace that is only used internally, and should not appear anywhere in your code.

    So define your class in the global namespace, and catch \HttpApiErrorException.

Howdy, Stranger!

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

In this Discussion