The Wayback Machine - https://web.archive.org/web/20201102021352/https://github.com/chomyeong/errorcode
Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 

README.md

🚀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

  1. PHP >= 5.4
  2. 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

About

😈api error code for php

Topics

Resources

License

Packages

No packages published

Languages

You can’t perform that action at this time.