public static function error(\Exception $e) { $app = Slim::getInstance(); if ($e instanceof APIException) { $app->log->addError("API error [{$e->getHTTPCode()}][{$e->getCode()}]: " . print_r(['error' => $e->getErrors()], 1)); $app->halt($e->getHTTPCode(), json_encode(['error' => $e->getErrors()], JSON_FORCE_OBJECT)); } else { $app->log->addError("API error [500][{$e->getCode()}]: {$e->getMessage()} \n {$e->getTraceAsString()}"); $app->syslog->addError("API error [500][{$e->getCode()}]: {$e->getMessage()} \n {$e->getTraceAsString()}"); if ($app->config('mode') == 'development') { self::registerWhoops(); $app->whoops->handleException($e); } $app->halt(500); } }
/** * Renders the exception. * @param \Exception $exception the exception to be rendered. */ protected function renderException($exception) { //如果存在未提交的事务,则对事务进行回滚 $transaction = Yii::$app->db->getTransaction(); if ($transaction) { $transaction->rollback(); } //对返回内容进行渲染 if (Yii::$app->has('response')) { $response = Yii::$app->getResponse(); // reset parameters of response to avoid interference with partially created response data // in case the error occurred while sending the response. $response->isSent = false; $response->stream = null; $response->data = null; $response->content = null; } else { $response = new Response(); } $useErrorView = $response->format === Response::FORMAT_HTML && !YII_DEBUG; //如果是用户定义的异常,则需要将异常错误信息抛出,如果是接口类型的并且有model类型的错误 if ($useErrorView) { if ($this->errorAction !== null) { $result = Yii::$app->runAction($this->errorAction); if ($result instanceof Response) { $result = $result; } else { $result->data = $result; } } else { //在没有默认异常处理action的情况下,直接渲染文件 $file = $useErrorView ? $this->errorView : $this->exceptionView; $responseData = $this->renderFile($file, ['exception' => $exception]); } } else { if ($response->format === Response::FORMAT_JSON || $response->format === Response::FORMAT_XML) { $response->data = ['code' => $exception->getCode(), 'message' => $exception->getMessage()]; if ($exception instanceof LBUserException) { $response->data['errors'] = $exception->getErrors(); } } else { $response->data = $this->convertExceptionToArray($exception); } } //调试状态状态码为500, 非调试状态不抛出异常 if (!YII_DEBUG || $exception instanceof LBUserException) { $response->setStatusCode(200); } else { $response->setStatusCode(500); } $response->send(); }
/** * Mask actual exception for security reasons in case when it should not be exposed to API clients. * * Convert any exception into \Magento\Webapi\Exception. * * @param \Exception $exception Exception to convert to a WebAPI exception * * @return WebapiException */ public function maskException(\Exception $exception) { $isDevMode = $this->_appState->getMode() === State::MODE_DEVELOPER; $stackTrace = $isDevMode ? $exception->getTraceAsString() : null; if ($exception instanceof LocalizedException) { // Map HTTP codes for LocalizedExceptions according to exception type if ($exception instanceof NoSuchEntityException) { $httpCode = WebapiException::HTTP_NOT_FOUND; } elseif ($exception instanceof AuthorizationException || $exception instanceof AuthenticationException) { $httpCode = WebapiException::HTTP_UNAUTHORIZED; } else { // Input, Expired, InvalidState exceptions will fall to here $httpCode = WebapiException::HTTP_BAD_REQUEST; } if ($exception instanceof AbstractAggregateException) { $errors = $exception->getErrors(); } else { $errors = null; } $maskedException = new WebapiException($exception->getRawMessage(), $exception->getCode(), $httpCode, $exception->getParameters(), get_class($exception), $errors, $stackTrace); } elseif ($exception instanceof WebapiException) { $maskedException = $exception; } else { $message = $exception->getMessage(); $code = $exception->getCode(); //if not in Dev mode, make sure the message and code is masked for unanticipated exceptions if (!$isDevMode) { /** Log information about actual exception */ $reportId = $this->_critical($exception); $message = sprintf(self::INTERNAL_SERVER_ERROR_MSG, $reportId); $code = 0; } $maskedException = new WebapiException($message, $code, WebapiException::HTTP_INTERNAL_ERROR, [], '', null, $stackTrace); } return $maskedException; }