UNPKG

node-web-mvc

Version:
47 lines (46 loc) 2.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ControllerAdvice_1 = __importDefault(require("../../annotations/ControllerAdvice")); const ExceptionHandler_1 = __importDefault(require("../../annotations/ExceptionHandler")); const RuntimeAnnotation_1 = __importDefault(require("../../annotations/annotation/RuntimeAnnotation")); const ModelAndView_1 = __importDefault(require("../../models/ModelAndView")); const ModelAndViewContainer_1 = __importDefault(require("../../models/ModelAndViewContainer")); const HandlerMethod_1 = __importDefault(require("../HandlerMethod")); const ServletInvocableHandlerMethod_1 = __importDefault(require("../ServletInvocableHandlerMethod")); class ExceptionHandlerExceptionResolver { constructor(returnValueHandlers, beanFactory) { this.returnValueHandlers = returnValueHandlers; this.beanFactory = beanFactory; } async resolveException(servletContext, handlerMethod, error) { try { const mavContainer = new ModelAndViewContainer_1.default(); const adviceAnnotation = RuntimeAnnotation_1.default.getAnnotation(ControllerAdvice_1.default); const globalAnnotation = RuntimeAnnotation_1.default.getAnnotation(ExceptionHandler_1.default, adviceAnnotation === null || adviceAnnotation === void 0 ? void 0 : adviceAnnotation.ctor); const scopeAnnotation = RuntimeAnnotation_1.default.getAnnotation(ExceptionHandler_1.default, handlerMethod === null || handlerMethod === void 0 ? void 0 : handlerMethod.beanType); const exceptionAnnotation = scopeAnnotation || globalAnnotation; if (exceptionAnnotation) { const bean = this.beanFactory.getBean(exceptionAnnotation.ctor); const handlerMethod = new HandlerMethod_1.default(bean, exceptionAnnotation.method, this.beanFactory); const exceptionHandlerMethod = new ServletInvocableHandlerMethod_1.default(handlerMethod); exceptionHandlerMethod.setReturnValueHandlers(this.returnValueHandlers); // 自定义异常处理 await exceptionHandlerMethod.invoke(servletContext, mavContainer, [error]); if (mavContainer.requestHandled) { // 如果请求已处理 return new ModelAndView_1.default(); } return new ModelAndView_1.default(mavContainer.view, mavContainer.model, mavContainer.status); } } catch (ex) { console.warn(`${ExceptionHandlerExceptionResolver.name} resolveException failure:`); console.warn(ex); } return null; } } exports.default = ExceptionHandlerExceptionResolver;