Automatically Generate Error Code Component
Description
This component can automatically maintain the generation of error codes based on given rules.
It is agreed that in the returned data, the code parameter indicates that all custom codes, with positive numbers representing normal service and negative numbers representing abnormal service.
Project Address
https://github.com/teamones-open/response-code-msg
Installation
composer require teamones/response-code-msg
Usage
Empty ErrorCode Class File
- File Path: ./support/ErrorCode.php
<?php
/**
* This is an auto-generated file, please do not modify it manually.
* @Author:$Id$
*/
namespace support;
class ErrorCode
{
}
Configuration File
Error codes will automatically increment based on the parameters configured below. For instance, if the current system_number = 201 and start_min_number = 10000, the first generated error code will be -20110001.
- File Path: ./config/error_code.php
<?php
return [
"class" => new \support\ErrorCode(), // ErrorCode class file
"root_path" => app_path(), // Current code root directory
"system_number" => 201, // System identifier
"start_min_number" => 10000 // Range for error code generation, e.g., 10000-99999
];
Add Auto-Generate Error Code Code in start.php
- File Path: ./start.php
// Place after Config::load(config_path(), ['route', 'container']);
// Generate error codes, only in APP_DEBUG mode
if (config("app.debug")) {
$errorCodeConfig = config('error_code');
(new \teamones\responseCodeMsg\Generate($errorCodeConfig))->run();
}
Using in Code
In the following code, ErrorCode::ModelAddOptionsError is the error code, where ModelAddOptionsError should semantically be written in PascalCase according to user requirements.
You will find that it is not usable at first. It will automatically generate the corresponding error code after the next restart. Note that sometimes you may need to restart twice.
<?php
/**
* Navigation related service class
*/
namespace app\service;
use app\model\Demo as DemoModel;
// Import ErrorCode class file
use support\ErrorCode;
class Demo
{
/**
* Add
* @param $data
* @return array|mixed
* @throws \exception
*/
public function add($data): array
{
try {
$demo = new DemoModel();
foreach ($data as $key => $value) {
$demo->$key = $value;
}
$demo->save();
return $demo->getData();
} catch (\Throwable $e) {
// Output error message
throw_http_exception($e->getMessage(), ErrorCode::ModelAddOptionsError);
}
return [];
}
}
Generated ./support/ErrorCode.php File
<?php
/**
* This is an auto-generated file, please do not modify it manually.
* @Author:$Id$
*/
namespace support;
class ErrorCode
{
const LoginNameOrPasswordError = -20110001;
const UserNotExist = -20110002;
const TokenNotExist = -20110003;
const InvalidToken = -20110004;
const ExpireToken = -20110005;
const WrongToken = -20110006;
const ClientIpNotEqual = -20110007;
const TokenRecordNotFound = -20110008;
const ModelAddUserError = -20110009;
const NoInfoToModify = -20110010;
const OnlyAdminPasswordCanBeModified = -20110011;
const AdminAccountCannotBeDeleted = -20110012;
const DbNotExist = -20110013;
const ModelAddOptionsError = -20110014;
const UnableToDeleteSystemConfig = -20110015;
const ConfigParamKeyRequired = -20110016;
const ExpiryCanNotGreaterThan7days = -20110017;
const GetPresignedPutObjectUrlError = -20110018;
const ObjectStorageConfigNotExist = -20110019;
const UpdateNavIndexSortError = -20110020;
const TagNameAttNotExist = -20110021;
const ModelUpdateOptionsError = -20110022;
}