Developer Blog

  • Blog
  • /
  • Breaking changes in error and request handling
By Dracony on 17 July 2013

I was looking to find a way of simplifying error handling and I think I’ve found the right solution. Now instead of registering an exception handler and running execute() in index.php HTTP handling has been moved to the handle_http_request() method with simple try-catch inside of it.

A new \PHPixie\Exception\PageNotFound class has been added that will be thrown when a routing error occurs. It’s now much simpler to handle these exceptions, and there is no need in overriding the Debug class anymore.

To adjust your existing project to this new scheme you need to edit your /web/index.php file:

1
2
3
4
5
6
// /web/index.php
//Instead of this line:
$pixie->bootstrap($root)->http_request()->execute()->send_headers()->send_body();

//Use this line:
$pixie->bootstrap($root)->handle_http_request();

If you have previously extended the Debug class to handle your errors, you can remove it now, and use the handle_exception() method of the Pixie class itself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// /classes/App/Pixie.php
namespace App;

class Pixie extends \PHPixie\Pixie{

    public function handle_exception($exception){
        //If its a Page Not Found error redirect the user to 404 Page
        if ($exception instanceof \PHPixie\Exception\PageNotFound){
            header('Location: /sorry/'); 
        }else{
            $http_status = "503 Service Temporarily Unavailable";
            header($_SERVER["SERVER_PROTOCOL"].' '.$http_status);
            header("Status: ".$http_status);
            echo("Sorry, something is wrong");
        }
    }
}

I hope this makes a lot more sense and will make handling exceptions a whole world easier.

comments powered by Disqus