PHP code example of treblle / error-codes
1. Go to this page and download the library: Download treblle/error-codes library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
treblle / error-codes example snippets
use Treblle\ErrorCodes\Enums\ErrorCode;
$badRequest = ErrorCode::BAD_REQUEST;
$title = $badRequest->getDescription()->title; // Bad Request
$code = $badRequest->getDescription()->code; // HTTP_400
$link = $badRequest->getDescription()->link; // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400
$status = $badRequest->getDescription()->status; // 400
final class Handler extends ExceptionHandler
{
public function register(): void
{
$this->renderable(function (ModelNotFoundException $exception, Request $request) {
$errorCode = \Treblle\ErrorCodes\Enums\ErrorCode::NOT_FOUND;
return new ErrorResponse(
data: new ApiError(
title: $errorCode->getDescription()->title,
detail: $exception->getMessage(),
instance: $request->path(),
code: $errorCode->getDescription()->code,
link: $errorCode->getDescription()->link,
),
status: Status::NOT_FOUND,
);
});
}
}