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

abc123
2300 1 0
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);
  • 暂无评论
年代过于久远,无法发表回答
×
🔝