Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Is there any way to set RedisException as 500?
  • Hello there,

    I try to modify public/index.php like following.

    try       
    {           
      // Boot the app...
      require APPPATH.'bootstrap.php';  
      // ... and execute the main request
      $response = $routerequest();
    }         
    catch (HttpBadRequestException $e)
    {           
      $response = $routerequest('_400_', $e);
    }          
    catch (HttpNoAccessException $e)  
    {           
      $response = $routerequest('_403_', $e);
    }           
    catch (HttpNotFoundException $e)
    {          
      $response = $routerequest('_404_', $e);
    }         
    catch (HttpServerErrorException $e)
    {          
      $response = $routerequest('_500_', $e);
    }        
    catch (RedisException $e) 
    {            
      $response = $routerequest('_500_', $e);  

    However, it gives me 404 even I set _500_.

    Do you have any suggestion for this?

    I also have 404.php and 500.php under fuel/app/views.

    Best regards,
  • Harro VertonHarro Verton
    Accepted Answer
    It doesn't work that way.

    Exceptions are programme errors, and cause the application to abort with either an Error page or an Oops page, depending on the environment the app runs in.

    If you get a HttpNotFoundException in your index.php, it means your RedisException is captured and handled somewhere in your app. The standard way to handle it is to display an error from that location, either by using a custom view, or throwing one of the HttpExceptions:

    try
    {
       .... your code
    }
    catch (\RedisException $e)
    {
         throw new \HttpServerErrorException($e->getMessage());
    }


    If you want a custom http exception you can capture in your index.php, you need to create an exception that extends \HttpException, and add an exception handler that returns the desired response.

    see /fuel/core/classes/httpexceptions.php.
  • Harro VertonHarro Verton
    Accepted Answer
    I see by the way that the current index.php doesn't reflect how it is supposed to work. It should be:

    // Generate the request, execute it and send the output.
    try
    {
        // Boot the app...
        require APPPATH.'bootstrap.php';

        // ... and execute the main request
        $response = $routerequest();
    }
    catch (Exception $e)
    {
        if (method_exists($e, 'response'))
        {
            $response = $e->response();
        }
        else
        {
            throw $e;
        }
    }

    There is no need for a new route request (and loose the history and status of the original one), HttpExceptions have a response() method that already returns the correct response.

    This really needs to be fixed for the next release.
  • Have to retract that last remark, the current code works as intended if there is no route defined for the exception, which is the intended behavior.
  • Thanks your suggestion.
    We will waiting for the next release as well :)
  • I just want let you know that 500 seems dose not work and so I replace like following in index.php.

    $response = $routerequest('_500_', $e);

    To

    throw new HttpServerErrorException($e->getMessage())

    The complete source code is as following.

    // Generate the request, execute it and send the output.
    try
    {
      // Boot the app...
      require APPPATH.'bootstrap.php';

      // ... and execute the main request
      $response = $routerequest();
    }
    catch (HttpBadRequestException $e)
    {
      $response = $routerequest('_400_', $e);
    }
    catch (HttpNoAccessException $e)
    {
      $response = $routerequest('_403_', $e);
    }
    catch (HttpNotFoundException $e)
    {
      $response = $routerequest('_404_', $e);
    }
    catch (HttpServerErrorException $e)
    {
      throw new HttpServerErrorException($e->getMessage());                                                                               
    }
  • I just want let you know that 500 seems dose not work

    Works fine here.

    If I add

    throw new HttpServerErrorException();

    In the action_index() of a controller, I get the 500 view, no problem.

Howdy, Stranger!

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

In this Discussion