🚀 PHP API通用错误�?
errorcode
在写接�?�API返回错误�?时,我们以�?�?ï¿½èƒ½è¿™æ ·ï¿½?�:
$ret = [
'code'=>100,
'msg'=>'用户�??错误',
'data'=>[],
];
echo json_encode($ret);
$ret = [
'code'=>101,
'msg'=>'密�?错误',
'data'=>[],
];
echo json_encode($ret);
$ret = [
'code'=>1011,
'msg'=>'�?�数错误',
'data'=>[],
];
echo json_encode($ret);写�?�写�?ï¿½ï¼Œä½ ï¿½?�能�?常忘记了错误�?从哪里开始了... 🤦�?��?♂�?
用了这个扩展�?�,我们�?ï¿½ä»¥è¿™æ ·ï¼š
return [
['errDefine' => 'ERROR_SHOP_NAME', 'index' => 1000, 'errMsg' => '商城�??称错误'],
['errDefine' => 'ERROR_SHOP_NAME1','errMsg' => '商城�??称错误1'],
['errDefine' => 'ERROR_ORDER_ERROR', 'index' => 2000, 'errMsg' => '商城登录错误'],
];比如错误�?1000到1999是商城错误,定义好1000,ç¬¬äºŒä¸ªé”™è¯¯æ— éœ€å®šä¹‰é”™è¯¯ï¿½?,�?��?�自增
✨ Feature
- æ— éœ€æ‰‹åŠ¨å®šä¹‰æ‡µé€¼çš„é”™è¯¯ï¿½?æ•°å—
- 定义好错误�?�?始值�?��?�自动递增
- �?ç”¨åœ¨çº ç»“é”™è¯¯ï¿½?放在哪
🖥 Requirement
- PHP >= 5.4
- composer
📦 Installation
composer require "chomyeong/errorcode" "v1.0.0"🔨 Usage
基本使用:
在vendor�?�级目录新建errors目录,新增code.phpï¼Œæ ¼ï¿½?如下:
return [
['errDefine' => 'ERROR_SHOP_NAME', 'index' => 1000, 'errMsg' => '商城�??称错误'],
['errDefine' => 'ERROR_SHOP_LOGIN', 'index' => 2000, 'errMsg' => '商城登录错误'],
];引入扩展包:
use chomyeong\errorcode\Error;
$err = new Error();�?应错误:
$err->responseError(ERROR_PARAM);è¿”å›žæ ¼ï¿½?:
{
"err": 1,
"errMsg": "�?�数错误�?",
"data": {},
"isError": true
}�?应�?功:
// �?返回的数�?�
$data = [
'info' => [
'id' => 1,
'username' => 'chomyeong',
],
];
$err->responseSuccess($data);è¿”å›žæ ¼ï¿½?:
{
"err": 0,
"errMsg": "",
"data": {
"info": {
"id": 1,
"username": "chomyeong"
}
},
"isError": false
}修改�?åº”æ ¼ï¿½?:
$options = [
'dataBody'=>'info',
'errBody'=>'code',
'isErrorBody'=>'error',
'errMsgBody'=>'message',
];
$err = new Error($options);è¿”å›žæ ¼ï¿½?:
{
"code": 0,
"message": "",
"info": {
"info": {
"id": 1,
"name": "chomyeong"
}
},
"error": false
}�?�数说明:
dataBody:�?应数�?�体
errBody:错误�?
isErrorBodyï¼šé”™è¯¯æ ‡è®°
errMsgBody:消�?�文本
比如�?�想修改错误�?:
$options = [
'errBody'=>'code',
];
$err = new Error($options);è¿”å›žæ ¼ï¿½?:
{
"code": 0,
"errMsg": "",
"data": {
"info": {
"id": 1,
"name": "chomyeong"
}
},
"isError": false
}默认错误�?:
ERROR_PARAM �?�数错误
ERROR_SYSTEM 系统错误
ERROR_SESSION_ERROR 会�?�?å˜åœ¨
ERROR_SESSION_PRIVILEGE_ERROR �?��?�?æ£ç¡®
ERROR_MODIFY_INFO_FAILED 修改数�?�失败
ERROR_ACCOUNT_LOGIN_FAILED 账�?��??或密�?错误
ERROR_ACCOUNT_RELOGIN_FAILED_WRONG_TOKEN �?登录失败:令牌错误
ERROR_ACCOUNT_RELOGIN_FAILED_TOKEN_TIMEOUT �?登录失败:令牌已超时
ERROR_ACCOUNT_PASSPORT_EXISTS 账�?�已å˜åœ¨
License
MIT

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
