public static function process(\Exception $e, Request $request, Response $response) { if ($e instanceof BaseException) { $response->withStatus($e->getHttpStatus()); $response->write(Code::json($e->getCode(), $e->getMessage())); } else { if ($e instanceof MethodNotAllowedException) { $response->withStatus(405); $allow = implode(', ', $e->getAllowedMethods()); $response->withHeader('Allow', $allow); $response->write(Code::json(Code::METHOD_NOT_ALLOWED, 'allow: ' . $allow)); } else { if ($e instanceof NotFoundException) { $response->withStatus(404); $response->write(Code::json(Code::RESOURCE_NOT_FOUND)); } else { if ($e instanceof UnsupportedMediaType) { $response->withStatus(415); $response->write(Code::json(Code::UNSUPPORTED_MEDIA_TYPE, $e->getMessage())); } else { if ($e instanceof BadBodyException) { $response->withStatus(400); $response->write(Code::json(Code::BAD_BODY, $e->getMessage())); } else { if ($e instanceof \PDOException) { $response->withStatus(502); $response->write(Code::json(Code::DATABASE_ERROR)); trigger_error(Str::exceptionToString($e), E_USER_WARNING); } else { if ($e instanceof \RedisException) { $response->withStatus(502); $response->write(Code::json(Code::CACHE_ERROR)); trigger_error(Str::exceptionToString($e), E_USER_WARNING); } else { $response->withStatus(500); $response->write(Code::json(Code::INTERNAL_SERVER_ERROR)); trigger_error(Str::exceptionToString($e), E_USER_WARNING); } } } } } } } }
public function displayError(\Exception $exception) { $originalException = $exception; $code = $exception->getCode(); //if extended exception was thrown, we have additional details if ($exception instanceof ExceptionInterface) { $httpStatus = $exception->getHttpStatus(); $details = $exception->getDetails(); } else { $httpStatus = 500; $details = array(); } $development = ini_get('display_errors') && APPLICATION_ENV === "development"; $message = $exception->getMessage(); //determine whether this is an error the user should see if (($code < 100000 || $code >= 200000) && !$development) { //exception code is in the range where the message should be //displayed to the user $message = "An unexpected error has occurred"; $details['detail'] = "This might be the developer's fault. The developer has been notified of this occurrence"; } //show exception details if in development //redundant conditional? if ($development) { $exceptions = array(); do { $exceptions['exception'][] = array('message' => $exception->getMessage(), 'code' => $exception->getCode(), 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => $exception->getTraceAsString()); } while ($exception = $exception->getPrevious()); $details['exceptions'] = $exceptions; } //TODO attach to this event, email if not in user visible range $this->getEventManager()->trigger('displayError', $this, $originalException); $result = $this->getError($message, $details, $httpStatus, $code); return $this->display($result); }
/** * 输出异常信息 * @param \Exception $exception * @param Array $vars 异常信息 * @return null */ public static function output(\Exception $exception, array $vars) { if ($exception instanceof Exception) { http_response_code($exception->getHttpStatus()); } else { http_response_code(500); } // header('Content-Type: application/json'); // echo json_encode($vars);exit; $type = Config::get('default_return_type'); if (IS_API && 'html' != $type) { // 异常信息输出监听 APP_HOOK && Hook::listen('error_output', $data); // 输出异常内容 Response::send($data, $type, Config::get('response_return')); } else { ob_end_clean(); extract($vars); include Config::get('exception_tmpl'); } }
/** * Given an exception, return a severity level for logging purposes. * * @param \Exception $error Exception to analyze * * @return int */ protected function getSeverityFromException($error) { // Treat unexpected or 5xx errors as more severe than 4xx errors. if ($error instanceof \VuFind\Exception\HttpStatusInterface && in_array($error->getHttpStatus(), [403, 404])) { return BaseLogger::WARN; } return BaseLogger::CRIT; }
/** * Interpret the given exception and decide whether it is due to an * unavailable storage, invalid storage or other. * This will either throw StorageInvalidException, StorageNotAvailableException * or do nothing. * * @param Exception $e sabre exception * * @throws StorageInvalidException if the storage is invalid, for example * when the authentication expired or is invalid * @throws StorageNotAvailableException if the storage is not available, * which might be temporary */ private function convertException(Exception $e) { \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); if ($e instanceof ClientHttpException) { if ($e->getHttpStatus() === 401) { // either password was changed or was invalid all along throw new StorageInvalidException(get_class($e) . ': ' . $e->getMessage()); } else { if ($e->getHttpStatus() === 405) { // ignore exception for MethodNotAllowed, false will be returned return; } } throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); } else { if ($e instanceof ClientException) { // connection timeout or refused, server could be temporarily down throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); } else { if ($e instanceof \InvalidArgumentException) { // parse error because the server returned HTML instead of XML, // possibly temporarily down throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage()); } else { if ($e instanceof StorageNotAvailableException || $e instanceof StorageInvalidException) { // rethrow throw $e; } } } } // TODO: only log for now, but in the future need to wrap/rethrow exception }
/** * 输出异常信息 * * @param \Exception $exception * @param array $data 异常信息 * * @return void */ public static function output($exception, array $data) { self::httpResponseCode($exception instanceof Exception ? $exception->getHttpStatus() : 500); $type = Config::get('default_return_type'); if (!APP_DEBUG && !Config::get('show_error_msg')) { // 不显示详细错误信息 $data['message'] = Config::get('error_message'); ##} ##if (IS_API && 'html' != $type) { // 异常信息输出监听 ##APP_HOOK && Hook::listen('error_output', $data); // 输出异常内容 ##Response::send($data, $type, Config::get('response_return')); } else { //ob_end_clean(); extract($data); // var_dump(Config::get('exception_tmpl'));exit; include Config::get('exception_tmpl'); } }