@@ -146,15 +146,17 @@ public void service(HttpServletRequest request, HttpServletResponse response, Co
146146 AsyncContext asyncContext = request .getAsyncContext ();
147147 if (asyncContext != null ) {
148148 filterChain = new ProxyFilterChain (this .dispatcher );
149- ((ProxyAsyncContext ) asyncContext ).addDispatchHandler (() -> {
150- try {
151- new ProxyFilterChain (this .dispatcher ).doFilter (request , response );
152- asyncContext .complete ();
153- }
154- catch (Exception e ) {
155- throw new IllegalStateException (e );
156- }
157- });
149+ if (asyncContext instanceof ProxyAsyncContext proxyAsyncContext ) {
150+ proxyAsyncContext .addDispatchHandler (() -> {
151+ try {
152+ new ProxyFilterChain (this .dispatcher ).doFilter (request , response );
153+ asyncContext .complete ();
154+ }
155+ catch (Exception e ) {
156+ throw new IllegalStateException (e );
157+ }
158+ });
159+ }
158160 }
159161
160162 if (latch != null ) {
@@ -262,20 +264,12 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
262264 }
263265 else {
264266 this .delegateServlet .service (request , response );
265- if (((HttpServletResponse ) response ).getStatus () != HttpStatus .OK .value () && request instanceof ProxyHttpServletRequest ) {
266- ((HttpServletRequest ) request ).setAttribute (RequestDispatcher .ERROR_STATUS_CODE , ((HttpServletResponse ) response ).getStatus ());
267- this .setErrorMessageAttribute ((ProxyHttpServletRequest ) request , (ProxyHttpServletResponse ) response , null );
268- ((HttpServletRequest ) request ).setAttribute (RequestDispatcher .ERROR_REQUEST_URI , ((HttpServletRequest ) request ).getRequestURI ());
269-
270- ((ProxyHttpServletRequest ) request ).setRequestURI ("/error" );
271- this .delegateServlet .service (request , response );
272- }
273267 }
274268 }
275269 catch (Exception e ) {
276270 if (request instanceof ProxyHttpServletRequest ) {
277- ((HttpServletRequest ) request ).setAttribute (RequestDispatcher .ERROR_STATUS_CODE , HttpStatus .INTERNAL_SERVER_ERROR );
278- this .setErrorMessageAttribute ((ProxyHttpServletRequest ) request , (ProxyHttpServletResponse ) response , e );
271+ ((HttpServletRequest ) request ).setAttribute (RequestDispatcher .ERROR_STATUS_CODE , HttpStatus .INTERNAL_SERVER_ERROR . value () );
272+ this .setErrorMessageAttribute ((HttpServletRequest ) request , (HttpServletResponse ) response , e );
279273 ((HttpServletRequest ) request ).setAttribute (RequestDispatcher .ERROR_EXCEPTION_TYPE , e );
280274 ((HttpServletRequest ) request ).setAttribute (RequestDispatcher .ERROR_REQUEST_URI , ((HttpServletRequest ) request ).getRequestURI ());
281275 ((ProxyHttpServletRequest ) request ).setRequestURI ("/error" );
@@ -287,12 +281,12 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
287281 }
288282 }
289283
290- private void setErrorMessageAttribute (ProxyHttpServletRequest request , ProxyHttpServletResponse response , Exception exception ) {
284+ private void setErrorMessageAttribute (HttpServletRequest request , HttpServletResponse response , Exception exception ) {
291285 if (exception != null && StringUtils .hasText (exception .getMessage ())) {
292286 request .setAttribute (RequestDispatcher .ERROR_MESSAGE , exception .getMessage ());
293287 }
294- else if (StringUtils .hasText (response .getErrorMessage ())) {
295- request .setAttribute (RequestDispatcher .ERROR_MESSAGE , response .getErrorMessage ());
288+ else if (response instanceof ProxyHttpServletResponse proxyResponse && StringUtils .hasText (proxyResponse .getErrorMessage ())) {
289+ request .setAttribute (RequestDispatcher .ERROR_MESSAGE , proxyResponse .getErrorMessage ());
296290 }
297291 else {
298292 request .setAttribute (RequestDispatcher .ERROR_MESSAGE , HttpStatus .valueOf (response .getStatus ()).getReasonPhrase ());
0 commit comments