Skip to content

generate_error_code

Joe Huss edited this page Nov 10, 2022 · 1 revision

Auto-generate error code component

Description

Ability to automatically maintain error code generation based on the given rules。

The code parameter in the convention return data, all custom codes, positive numbers for normal service, negative numbers for abnormal service。

Project address

https://github.com/teamones-open/response-code-msg

Install

composer require teamones/response-code-msg

Usage

empty ErrorCode class file

  • File path ./support/ErrorCode.php
<?php
/**
 * Automatically generated file, please do not modify it manually.
 * @Author:$Id$
 */
namespace support;

class ErrorCode
{
}

configuration file

The error code will be automatically generated by incrementing the parameters configured below, e.g. if the current system_number = 201 and start_min_number = 10000, then the first error code generated 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
    "system_number" => 201, // System identification
    "start_min_number" => 10000 // Error code generation range e.g. 10000-99999
];

Add automatic error code generation for startup in start.php

  • File path ./start.php
// put Config::load(config_path(), ['route', 'container']); back

// Generate error codes, only in APP_DEBUG mode
if (config("app.debug")) {
    $errorCodeConfig = config('error_code');
    (new \teamones\responseCodeMsg\Generate($errorCodeConfig))->run();
}

Use in code

is a single instance ErrorCode::ModelAddOptionsError is an error code, where ModelAddOptionsError It needs to be written by the user with semantic initial capitalization according to the current requirements。

After writing you will find that it is unusable, and then the next reboot will automatically generate the corresponding error code. Note that sometimes you need to restart twice。

<?php
/**
 * Navigation-related operations service class
 */

namespace app\service;

use app\model\Demo as DemoModel;

// Introduce 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 [];
    }
}

The generated . /support/ErrorCode.php file

<?php
/**
 * Automatically 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;
}

Clone this wiki locally