PHPackages                             chomyeong/errorcode - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. chomyeong/errorcode

ActiveLibrary[API Development](/categories/api)

chomyeong/errorcode
===================

API error code for php

v1.0.1(6y ago)111MITPHPPHP &gt;=7.0

Since Apr 6Pushed 6y ago1 watchersCompare

[ Source](https://github.com/chomyeong/errorcode)[ Packagist](https://packagist.org/packages/chomyeong/errorcode)[ RSS](/packages/chomyeong-errorcode/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

🚀PHP API通用错误码
=============

[](#rocketphp-api通用错误码)

errorcode
---------

[](#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
--------

[](#feature)

- 无需手动定义懵逼的错误码数字
- 定义好错误码初始值即可自动递增
- 不用在纠结错误码放在哪

🖥Requirement
------------

[](#requirement)

1. PHP &gt;= 5.4
2. **[composer](https://getcomposer.org/)**

📦Installation
-------------

[](#installation)

```
composer require "chomyeong/errorcode" "v1.0.0"
```

🔨Usage
------

[](#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
-------

[](#license)

MIT

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~17 days

Total

2

Last Release

2212d ago

PHP version history (2 changes)v1.0.0PHP &gt;=5.4

v1.0.1PHP &gt;=7.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8610646?v=4)[chomyeong](/maintainers/chomyeong)[@chomyeong](https://github.com/chomyeong)

---

Top Contributors

[![chomyeong](https://avatars.githubusercontent.com/u/8610646?v=4)](https://github.com/chomyeong "chomyeong (8 commits)")

---

Tags

apierrorcodephp

### Embed Badge

![Health badge](/badges/chomyeong-errorcode/health.svg)

```
[![Health](https://phpackages.com/badges/chomyeong-errorcode/health.svg)](https://phpackages.com/packages/chomyeong-errorcode)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
