Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/errors_all_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/errors_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down