From 47fdc0e31b498f1132666ba37ef23d6b8a20107e Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 10 Jul 2014 22:47:05 -0400 Subject: [PATCH] Add HTTP Exceptions and convert to responses in Handler --- composer.json | 3 +- .../WellRESTed/Exceptions/HttpExceptions.php | 39 +++++++++++++++++++ src/pjdietz/WellRESTed/Handler.php | 12 +++++- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/pjdietz/WellRESTed/Exceptions/HttpExceptions.php diff --git a/composer.json b/composer.json index fed70ff..fd426f1 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "php": ">=5.3.0" }, "autoload": { - "psr-0": { "pjdietz\\WellRESTed": "src/" } + "psr-0": { "pjdietz\\WellRESTed": "src/" }, + "classmap": ["src/pjdietz/WellRESTed/Exceptions/HttpExceptions.php"] } } diff --git a/src/pjdietz/WellRESTed/Exceptions/HttpExceptions.php b/src/pjdietz/WellRESTed/Exceptions/HttpExceptions.php new file mode 100644 index 0000000..c18bc5c --- /dev/null +++ b/src/pjdietz/WellRESTed/Exceptions/HttpExceptions.php @@ -0,0 +1,39 @@ +request = $request; $this->args = $args; $this->response = new Response(); - $this->buildResponse(); + try { + $this->buildResponse(); + } catch (HttpException $e) { + $this->response->setStatusCode($e->getCode()); + $this->response->setBody($e->getMessage()); + } return $this->response; } @@ -51,6 +57,10 @@ abstract class Handler implements HandlerInterface * Prepare the Response. Override this method if your subclass needs to * repond to any non-standard HTTP methods. Otherwise, override the * get, post, put, etc. methods. + * + * An uncaught HttpException (or subclass) will be converted to a response + * using the exception's code as the status code and the exceptios message + * as the body. */ protected function buildResponse() {