webman api 出现404或者全局报错怎么以json格式返回

abc123
阅读 562
1个回答

weijer

support/exception/Handler.php

 public function render(Request $request, Throwable $exception): Response
    {
        return new Response(200, ['Content-Type' => 'application/json'], $exception->getMessage());
    }

我是定义了一个 HttpResponseException.php , 然后封装了异常抛出函数

<?php

namespace support\exception;

class HttpResponseException extends \Exception
{}

support/exception/Handler.php

       if ($exception instanceof HttpResponseException) {
            // 抛出运行时异常
            return new Response(200, ['Content-Type' => 'application/json'], $exception->getMessage());
        }
        return parent::render($request, $exception);
  • 暂无评论