logger = $logger; } public function handle(Throwable $throwable, ResponseInterface $response) { $data = []; $code = ResultCode::SERVER_ERROR; $message = ''; $httpCode = 200; if ($throwable instanceof BusinessException) { $this->stopPropagation(); $code = $throwable->getCode(); $message = $throwable->getMessage(); } elseif ($throwable instanceof ModelNotFoundException) { $this->stopPropagation(); $code = ResultCode::RECORD_NOT_FOUND; $message = ResultCode::getMessage($code); } else { if (config('app_env') == 'dev') { $message = $throwable->getMessage(); $data = [ 'line' => $throwable->getLine(), 'file' => $throwable->getFile(), ]; } else { $message = ResultCode::getMessage($code); } $this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile())); $this->logger->error($throwable->getTraceAsString()); } $result = new Result($code, $message, $data); return $response ->withAddedHeader('content-type', 'application/json; charset=utf-8') ->withStatus($httpCode) ->withBody(new SwooleStream(strval($result))); } public function isValid(Throwable $throwable): bool { return true; } }