boltics / http-exception
An exception that contains http status code
Installs: 7 205
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
pkg:composer/boltics/http-exception
Requires
- php: >=7.1
- symfony/http-foundation: ^4.1 || ^5.0
Requires (Dev)
- phpunit/phpunit: ^5.7 || ^6.0 || ^7.0 || ^8.0
This package is auto-updated.
Last update: 2025-11-27 14:13:57 UTC
README
A exception that extends PHP exception and also contains http status code.
Now you can easily manage your error code and http status code together!
Installation
composer require boltics\http-exception
Declaration
use Boltics\HttpException\Exception; use Symfony\Component\HttpFoundation\Response;
Usage
$errorInfo = [ 'message' => 'Hola', 'errorCode' => 1234, 'httpCode' => Response::HTTP_BAD_REQUEST ]; throw new Exception($errorInfo); // For Laravel Response // You can use one exception to manage both error code and http code class CustomizedException extends Exception { const FIRST_ERROR = [ 'message' => 'Hola', 'errorCode' => 1234, 'httpCode' => Response::HTTP_BAD_REQUEST ]; } try { // do something throw new CustomizedException(CustomizedException::FIRST_ERROR); } catch (CustomizedException $e) { return $reponse()->json($data, $e->getHttpCode()) }
Documentation
__construct()The constructor checks that the value exist in the enumgetHttpCode()Returns http codesetHttpCode()Set http codegetErrorCode()Another approach to get exception codesetAdditionalData()Set additional datagetAdditionalData()Get additional dataappendAdditionalData()Append data to the additional data
Static methods:
isValidHttpCode()method Returns boolean that http code is valid (in the supported list)sanitizeException()method Returns valid exception info for __construct()