dingo
一般 dingo 的请求封装
还有就是关闭debug
可是即使关闭了debug, 错误提示依旧不如意(英文)。
解决方式
效果图
1)修改 App\Providers\AppServiceProvider 文件 register 内容
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('api.exception', function () {
return new \App\Exceptions\DingoExceptionHandler(
$this->app['Illuminate\Contracts\Debug\ExceptionHandler'],
config('api.errorFormat'),
config('api.debug')
);
});
}
- 新建 DingoExceptionHandler 文件
在App\Exceptions下创建 DingoExceptionHandler异常处理文件
放入以下内容即可
可自行添加
<?php
namespace App\Exceptions;
use Dingo\Api\Contract\Debug\ExceptionHandler;
use Dingo\Api\Exception\Handler as DingoHandler;
use Dingo\Api\Exception;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DingoExceptionHandler extends DingoHandler implements ExceptionHandler
{
public function handle($exception)
{
if ($exception instanceof Exception\RateLimitExceededException) {
return response([
'status_code' => -1,
'message' => '当前请求太过频繁',
])->withHeaders($exception->getHeaders());
}
if ($exception instanceof NotFoundHttpException) {
return response([
'status_code' => -1,
'message' => '抱歉,未找到数据!',
], 404);
}
if ($exception instanceof \Exception) {
return response([
'status_code' => -1,
'message' => '请求有误!',
], 500);
}
// TODO: 此处可对其它异常进行同样方式的处理
return parent::handle($exception);
}
}