Skip to content

Instantly share code, notes, and snippets.

@maul-esel
Last active December 12, 2015 03:48
Show Gist options
  • Save maul-esel/4709655 to your computer and use it in GitHub Desktop.
Save maul-esel/4709655 to your computer and use it in GitHub Desktop.
A small class used in some of my projects for handling or throwing HTTP errors.
<?php
class HttpException extends Exception
{
private $headers;
public function __construct($code, $message = NULL, $headers = NULL)
{
$this->code = $code;
$this->headers = $headers;
parent::__construct(self::getStatusMessage($code) . ($message ? '<p>' . $message . '</p>' : ''), $code);
}
public function getHeaders()
{
return $this->headers;
}
public static function getStatusMessage($code)
{
switch ($code)
{
case 200: return 'OK';
case 201: return 'Created';
case 204: return 'No Content';
case 400: return 'Bad request';
case 401: return 'Unauthorized';
case 403: return 'Forbidden';
case 404: return 'Not Found';
case 405: return 'Method not allowed';
case 406: return 'Not Acceptable';
case 409: return 'Conflict';
case 411: return 'Length Required';
case 413: return 'Request Entity Too Large';
case 415: return 'Unsupported Media Type';
case 423: return 'Locked';
case 500: return 'Internal Server Error';
case 501: return 'Not Implemented';
case 503: return 'Service Unavailable';
default: return '(Unknown error)';
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment