webman throw new Exception() 抛出异常后无法获取错误码

邹意

问题描述

手动抛出异常:throw new \Exception('xxxx',100);

public function render(Request $request, Throwable $exception) : Response
    {
        $code = $exception->getCode(); //这始终是0,无法获取/////////////
        if ($request->expectsJson()) {
            $json = ['code' => $code ?: 500, 'message' => $this->debug ? $exception->getMessage() : 'Server internal error'];
            $this->debug && $json['traces'] = (string)$exception;
            return new Response(200, ['Content-Type' => 'application/json'],
                json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
        }
        $error = $this->debug ? nl2br((string)$exception) : 'Server internal error';
        return new Response(500, [], $error);
    }
627 1 0
1个回答

walkor
<?php
namespace app\controller;
use support\Request;

class IndexController
{
    public function index(Request $request)
    {
        throw new \Exception('xxxx',100);
    }
}

截图

没问题

  • 暂无评论
🔝