Package Data | |
---|---|
Maintainer Username: | ckryo |
Maintainer Contact: | ckryo@qq.com (ckryo) |
Package Create Date: | 2017-06-01 |
Package Last Update: | 2017-06-02 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-14 15:01:39 |
Package Statistics | |
---|---|
Total Downloads: | 429 |
Monthly Downloads: | 18 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 2 |
Total Forks: | 2 |
Total Open Issues: | 0 |
错误处理及错误码管理
composer require ckryo/laravel_hander
\Ckryo\Laravel\Handler\HandlerServiceProvider::class
php artisan vendor:publish
/**
* 登录授权错误码
* 代码范围 200 - 399
*
* 登录授权 200 - 299
* 权限验证 300 - 399
*/
return [
'auth' => [
'200' => '授权信息验证失败, 需要重新登录',
'201' => '账号异地登录提醒',
'210' => '账号不存在',
'211' => '账号密码错误',
'212' => '账号被禁用, 无法使用',
'240' => '当前账号不属于该平台,无法从此处登录'
],
'permission' => [
'300' => '当前账号无权进行此操作'
]
];
$errCodes = require __DIR__.'/../config/errCode.php';
foreach ($errCodes as $model => $codes) {
$errorCode->regist($model, $codes);
}
throw new ErrorCodeException(200);
// 或者
throw new ErrorCodeException(错误码, '错误描述', '错误详情');
$this->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
\Ckryo\Laravel\Handler\Handler::class
);
创建Exception需要实现 \Ckryo\Laravel\Handler\Contracts\ExceptionFactory
接口;
该接口只申明了一个handle()
函数,用于返回响应信息。
<?php
namespace Ckryo\Laravel\Handler;
use Ckryo\Laravel\Handler\Contracts\ExceptionFactory;
use Ckryo\Laravel\Handler\Facades\ErrorCode;
class ErrorCodeException extends \Exception implements ExceptionFactory
{
protected $code;
protected $message;
protected $data;
public function __construct($code, $message = null, $data = null)
{
$this->code = $code;
$this->data = $data ?: $this;
$this->message = $message ?: ErrorCode::getErrMsg($code);
}
public function handle()
{
return response()->json([
'errCode' => $this->code,
'errMsg' => $this->message,
'data' => $this->data
]);
}
}
具体请参考laravel源码及文档