From b9a900574d99bbc137aa09ed11d3e751586b1cfa Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 12 Sep 2014 20:27:47 +0200 Subject: [PATCH] error handling --- 1-setup.md | 4 ++-- 2-http.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/1-setup.md b/1-setup.md index b113572..1aa5ab3 100644 --- a/1-setup.md +++ b/1-setup.md @@ -1,4 +1,4 @@ -[Next step >>](2-http.md) +[next >>](2-http.md) ### Setting up the front controller @@ -89,4 +89,4 @@ This will exclude the included file and folder from your commits. For which woul Now you have successfully created an empty playground which you can use to set up your project. -[Next step >>](2-http.md) \ No newline at end of file +[next >>](2-http.md) \ No newline at end of file diff --git a/2-http.md b/2-http.md index 4cceeff..d47461b 100644 --- a/2-http.md +++ b/2-http.md @@ -1,3 +1,56 @@ -[<< Previous step](1-setup.md) [Next step >>](3-.md) +[<< previous](1-setup.md) | [next >>](3-.md) ### Error handling + +The first package for the project will be an error handler that will provide you with nice errors. A nice error page with a lot of information for debugging goes a long way during development. + +I like [filp/whoops](https://github.com/filp/whoops) , so I will show how you can install that package for your project. If you prefer another package, feel free to install that one. This is the beauty of programming without a framework, you have total control over your project. + +To install a new package, open up your `composer.json` and add the package to the require part. It should now look like this: + +``` +"require": { + "php": ">=5.5.0", + "filp/whoops": "~1.0" +}, +``` + +Now run `composer update` in your console and it will be installed. + +But you can't use it yet. PHP won't know where to find the files for the classes. For this you will need an autoloader, ideally a [PSR-4](http://www.php-fig.org/psr/psr-4/) autoloader. Composer already takes care of this for you, so you only have to add a `require '../vendor/autoload.php';` to your `Bootstrap.php`. + +Now before you start adding the error handler code to the + +**Important:** Never show any errors in your production environment. A stack trace or even just a simple error message can help someone to gain access to your system. Always show a user friendly error page instead and send an email to yourself, write to a log or something similar. So only you can see the errors in the production environment. + +For development that does not make sense though and you want a nice error page. So the solution is to have an environment switch in your code. For now you can just set it to `development`. + +Then after the error handler registration, throw an Exception to test if everything is working correctly. Your `Bootstrap.php` should now look similar to this: + +``` +pushHandler(new \Whoops\Handler\PrettyPageHandler); +} else { + $woops->pushHandler(function($e){ + echo 'Friendly error page and send an email to the developer'; + }); +} +$woops->register(); + +throw new \Exception; + +``` \ No newline at end of file