laravel接管Dingo-api和默认的错误处理方式
作者:zhangatle 时间:2023-11-21 23:29:37
接管Dingo-api的错误
如上图所示,AppServiceProvider.php中的register()方法中添加如下代码
\API::error(function (\Illuminate\Validation\ValidationException $exception){
$data =$exception->validator->getMessageBag();
$msg = collect($data)->first();
if(is_array($msg)){
$msg = $msg[0];
}
return response()->json(['message'=>$msg,'status_code'=>400], 200);
});
\API::error(function (\Dingo\Api\Exception\ValidationHttpException $exception){
$errors = $exception->getErrors();
return response()->json(['message'=>$errors->first(),'status_code'=>400], 200);
});
接管laravel的错误
在Exceptions的Handler.php的render中写入以下代码
public function render($request, Exception $exception)
{
if($exception instanceof \Illuminate\Validation\ValidationException){
$data = $exception->validator->getMessageBag();
$msg = collect($data)->first();
if(is_array($msg)){
$msg = $msg[0];
}
return response()->json(['message'=>$msg],200);
}
if (in_array('api',$exception->guards())){
if($exception instanceof AuthenticationException){
return response()->json(['message'=>'token错误'],200);
}
if($exception instanceof ModelNotFoundException){
return response()->json(['message'=>'该模型未找到'],200);
}
}
return parent::render($request, $exception);
}
来源:https://blog.csdn.net/zhangatle/article/details/80945041
标签:laravel,Dingo-api
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python matplotlib画曲线例题解析
2022-10-18 14:17:30
![](https://img.aspxhome.com/file/2023/4/86324_0s.png)
Python利用reportlab实现制作pdf报告
2023-10-25 05:26:36
![](https://img.aspxhome.com/file/2023/9/126949_0s.jpg)
对python中的iter()函数与next()函数详解
2022-01-29 19:05:36
利用Python Matlab绘制曲线图的简单实例
2021-05-16 07:21:38
![](https://img.aspxhome.com/file/2023/4/78494_0s.png)
使用 Python 实现文件递归遍历的三种方式
2022-08-19 18:49:08
zen coding的dreamweaver插件安装教程
2010-01-12 13:30:00
![](https://img.aspxhome.com/file/UploadPic/20101/12/zencoding-13s.jpg)
mysql 导入导出数据库以及函数、存储过程的介绍
2024-01-20 15:26:51
python验证码识别教程之滑动验证码
2023-12-24 00:19:59
python 进程间数据共享multiProcess.Manger实现解析
2021-03-25 14:06:53
Python实现向PPT中插入表格与图片的方法详解
2023-08-20 03:43:09
![](https://img.aspxhome.com/file/2023/9/127259_0s.jpg)
Python多线程编程之多线程加锁操作示例
2023-11-01 00:37:19
43个应当避免的Web设计错误
2007-09-30 12:57:00
举例详解Python中smtplib模块处理电子邮件的使用
2023-10-08 04:46:14
Python上下文管理器Content Manager
2021-08-22 23:47:11
Python简单定义与使用字典dict的方法示例
2023-03-10 03:22:48
![](https://img.aspxhome.com/file/2023/8/93668_0s.jpg)
Hugo 游乐场内容初始化示例详解
2024-05-10 13:58:13
Python算法输出1-9数组形成的结果为100的所有运算式
2022-05-02 22:45:48
MySQL无法启动1067错误的又一种解决方法(机房断电)
2024-01-13 08:55:25
![](https://img.aspxhome.com/file/2023/2/109822_0s.jpg)
一个asp正则替换的方法
2008-11-25 14:05:00
em和strong的区别
2008-12-23 12:08:00