请问 dingoapi 如何自定义异常信息

项目现在使用了dingoapi,现在默认抛出异常好像只能自定义message和http status,现在有个需求是可以自定义错误code码,请问有什么解决方案

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 5

自定义 异常类 单独处理异常业务

4年前 评论
Epona

自己写一个方法就可以了。

public function customMessage($message, $customCode = 10000)
{
    return response()->json([
        'message' => $message,
        'code' => $customCode,
    ], 200);
}
4年前 评论

@Epona 我make了一个exception,然后在render里面写这个,然后进不到render的方法

4年前 评论

写个工具类封装一下 Symfony\Component\HttpKernel\Exception\HttpException 不就行了,
示例方法:

    use Symfony\Component\HttpKernel\Exception\HttpException;

    /**
     * @param string $message 异常信息
     * @param int $code 自定义错误号
     * @param int $http_status http状态码
     * @param array $headers 自定义http头部
     */
    public function throwHttpException($message, $code, $http_status = 400, $headers = [])
    {
        throw new HttpException($http_status, $message, null, $headers, $code);
    }
4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!