From b9269fd0de443eaae13547a546531ffe34eb5c45 Mon Sep 17 00:00:00 2001 From: Romans Malinovskis Date: Wed, 21 May 2014 20:52:41 +0100 Subject: [PATCH] Use native PHP Exception Code to transmit HTTP response code As per PHP manual, Exceptions supports specifying error codes through 2nd argument of constructor: http://www.php.net/manual/en/exception.getcode.php This patch makes use of standard PHP functionality to transmit error codes. This adds support for un-handled response types such as 402, 420 and others. --- Pest.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Pest.php b/Pest.php index f88216f..db74d6a 100644 --- a/Pest.php +++ b/Pest.php @@ -299,25 +299,25 @@ protected function checkLastResponseForError() $err = null; switch ($meta['http_code']) { case 400: - throw new Pest_BadRequest($this->processError($body)); + throw new Pest_BadRequest($this->processError($body), $meta['http_code']); break; case 401: - throw new Pest_Unauthorized($this->processError($body)); + throw new Pest_Unauthorized($this->processError($body), $meta['http_code']); break; case 403: - throw new Pest_Forbidden($this->processError($body)); + throw new Pest_Forbidden($this->processError($body), $meta['http_code']); break; case 404: - throw new Pest_NotFound($this->processError($body)); + throw new Pest_NotFound($this->processError($body), $meta['http_code']); break; case 405: - throw new Pest_MethodNotAllowed($this->processError($body)); + throw new Pest_MethodNotAllowed($this->processError($body), $meta['http_code']); break; case 409: - throw new Pest_Conflict($this->processError($body)); + throw new Pest_Conflict($this->processError($body), $meta['http_code']); break; case 410: - throw new Pest_Gone($this->processError($body)); + throw new Pest_Gone($this->processError($body), $meta['http_code']); break; case 422: // Unprocessable Entity -- see http://www.iana.org/assignments/http-status-codes @@ -325,14 +325,15 @@ protected function checkLastResponseForError() // a response to a request that is syntactically correct, // but semantically invalid (for example, when trying to // create a resource with some required fields missing) - throw new Pest_InvalidRecord($this->processError($body)); + throw new Pest_InvalidRecord($this->processError($body), $meta['http_code']); break; default: if ($meta['http_code'] >= 400 && $meta['http_code'] <= 499) - throw new Pest_ClientError($this->processError($body)); + throw new Pest_ClientError($this->processError($body), $meta['http_code']); elseif ($meta['http_code'] >= 500 && $meta['http_code'] <= 599) - throw new Pest_ServerError($this->processError($body)); elseif (!isset($meta['http_code']) || $meta['http_code'] >= 600) { - throw new Pest_UnknownResponse($this->processError($body)); + throw new Pest_ServerError($this->processError($body), $meta['http_code']); + elseif (!isset($meta['http_code']) || $meta['http_code'] >= 600) { + throw new Pest_UnknownResponse($this->processError($body), $meta['http_code']); } } }