PHP code example of 3sidedcube / laravel-api-errors
1. Go to this page and download the library: Download 3sidedcube/laravel-api-errors 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/ */
3sidedcube / laravel-api-errors example snippets
use ThreeSidedCube\LaravelApiErrors\ApiErrorResponse;
use ThreeSidedCube\LaravelApiErrors\Exceptions\ApiErrorException;
class UserBannedException extends ApiErrorException
{
/**
* A short error code describing the error.
*
* @return string
*/
public function code(): string
{
return 'user_account_banned';
}
/**
* A human-readable message providing more details about the error.
*
* @return string
*/
public function message(): string
{
return 'User account banned.';
}
/**
* The api error status code.
*
* @return int
*/
public function statusCode(): int
{
return 403;
}
}
$exception = new UserBannedException();
// This will return an instance of JsonResponse
$response = ApiErrorResponse::fromException($exception);
use ThreeSidedCube\LaravelApiErrors\Exceptions\ApiErrorException;
protected $dontReport = [
ApiErrorException::class,
];
use ThreeSidedCube\LaravelApiErrors\ApiErrorResponse;
// This will return an instance of JsonResponse
$response = ApiErrorResponse::create('user_account_banned', 'User account banned.', 403);
use ThreeSidedCube\LaravelApiErrors\ApiErrorResponse;
use ThreeSidedCube\LaravelApiErrors\Exceptions\ApiErrorException;
class UserBannedException extends ApiErrorException
{
/**
* A short error code describing the error.
*
* @return string
*/
public function code(): string
{
return 'user_account_banned';
}
/**
* A human-readable message providing more details about the error.
*
* @return string
*/
public function message(): string
{
return 'User account banned.';
}
/**
* The api error status code.
*
* @return int
*/
public function statusCode(): int
{
return 403;
}
/**
* Any additional metadata to be
use ThreeSidedCube\LaravelApiErrors\ApiErrorResponse;
// This will return an instance of JsonResponse
$response = ApiErrorResponse::create('user_account_banned', 'User account banned.', 403, ['foo' => 'bar']);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.