手动抛出异常: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);
}
没问题