diff --git a/examples/errors_all_options.php b/examples/errors_all_options.php index 4fbeb9d..69773fa 100644 --- a/examples/errors_all_options.php +++ b/examples/errors_all_options.php @@ -28,7 +28,7 @@ // the http status code // @note it is better to set this on the jsonapi\errors object .. // .. as only a single one can be consumed by the browser -$error->set_http_status($http_status=404); +$error->set_http_status($http_status=\alsvanzelf\jsonapi\base::STATUS_NOT_FOUND); // if not set during construction, set them here $error->set_error_message($error_message='too much options'); @@ -40,7 +40,7 @@ */ $another_error = new \alsvanzelf\jsonapi\error('kiss', 'Error objects can be small and simple as well.'); -$some_exception = new Exception('please don\'t throw things', 500); +$some_exception = new Exception('please don\'t throw things', \alsvanzelf\jsonapi\base::STATUS_INTERNAL_SERVER_ERROR); /** * building up the json response @@ -56,7 +56,7 @@ $jsonapi->add_error($another_error); $jsonapi->add_exception($some_exception); -$jsonapi->set_http_status(400); +$jsonapi->set_http_status(\alsvanzelf\jsonapi\base::STATUS_BAD_REQUEST); /** * sending the response diff --git a/examples/errors_exception.php b/examples/errors_exception.php index 6fcaccc..f4ed2eb 100644 --- a/examples/errors_exception.php +++ b/examples/errors_exception.php @@ -19,7 +19,7 @@ */ try { - throw new Exception('unknown user', 404); + throw new Exception('unknown user', \alsvanzelf\jsonapi\base::STATUS_NOT_FOUND); } catch (Exception $e) { $jsonapi = new \alsvanzelf\jsonapi\errors($e); diff --git a/src/base.php b/src/base.php index a6d3587..7230799 100644 --- a/src/base.php +++ b/src/base.php @@ -10,6 +10,18 @@ class base { const CONTENT_TYPE_OFFICIAL = 'application/vnd.api+json'; const CONTENT_TYPE_DEBUG = 'application/json'; +/** + * advised http status codes + */ +const STATUS_BAD_REQUEST = 400; +const STATUS_UNAUTHORIZED = 401; +const STATUS_FORBIDDEN = 403; +const STATUS_NOT_FOUND = 404; +const STATUS_METHOD_NOT_ALLOWED = 405; +const STATUS_UNPROCESSABLE_ENTITY = 422; +const STATUS_INTERNAL_SERVER_ERROR = 500; +const STATUS_SERVICE_UNAVAILABLE = 503; + /** * internal data containers */ diff --git a/src/errors.php b/src/errors.php index a25b4c9..d5c8a47 100644 --- a/src/errors.php +++ b/src/errors.php @@ -31,7 +31,7 @@ class errors extends base { /** - * advised http status codes + * http status messages used for string output */ public static $http_status_messages = array( 400 => 'Bad Request',