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

0
已邀请:

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);

要回复问题请先登录注册