Refine SyncInvocableHandlerMethod error handling

Ensure the error is wrapped as ServerErrorException
This commit is contained in:
Rossen Stoyanchev
2018-03-31 12:03:03 -04:00
parent 912c270f2b
commit d9e17a62ce
4 changed files with 60 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,11 +19,12 @@ package org.springframework.web.server;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
import org.springframework.web.method.HandlerMethod;
/**
* Exception for errors that fit response status 500 (bad request) for use in
* Spring Web applications. The exception provides additional fields (e.g.
* an optional {@link MethodParameter} if related to the error).
* Exception for an {@link HttpStatus#INTERNAL_SERVER_ERROR} that exposes extra
* information about a controller method that failed, or a controller method
* argument that could not be resolved.
*
* @author Rossen Stoyanchev
* @since 5.0
@@ -31,6 +32,9 @@ import org.springframework.lang.Nullable;
@SuppressWarnings("serial")
public class ServerErrorException extends ResponseStatusException {
@Nullable
private final HandlerMethod handlerMethod;
@Nullable
private final MethodParameter parameter;
@@ -39,27 +43,60 @@ public class ServerErrorException extends ResponseStatusException {
* Constructor with an explanation only.
*/
public ServerErrorException(String reason) {
this(reason, null, null);
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, null);
this.handlerMethod = null;
this.parameter = null;
}
/**
* Constructor for a 500 error linked to a specific {@code MethodParameter}.
* Constructor with a reason and root cause.
* @since 5.0.5
*/
public ServerErrorException(String reason, MethodParameter parameter) {
this(reason, parameter, null);
public ServerErrorException(String reason, Throwable cause) {
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause);
this.handlerMethod = null;
this.parameter = null;
}
/**
* Constructor for a 500 error with a {@link MethodParameter}.
*/
public ServerErrorException(String reason, MethodParameter parameter, @Nullable Throwable cause) {
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause);
this.handlerMethod = null;
this.parameter = parameter;
}
/**
* Constructor for a 500 error with a root cause.
*/
public ServerErrorException(String reason, @Nullable MethodParameter parameter, @Nullable Throwable cause) {
public ServerErrorException(String reason, HandlerMethod handlerMethod, @Nullable Throwable cause) {
super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause);
this.parameter = parameter;
this.handlerMethod = handlerMethod;
this.parameter = null;
}
/**
* Constructor for a 500 error linked to a specific {@code MethodParameter}.
* @deprecated in favor of {@link #ServerErrorException(String, MethodParameter, Throwable)}
*/
@Deprecated
public ServerErrorException(String reason, MethodParameter parameter) {
this(reason, parameter, null);
}
/**
* Return the {@code MethodParameter} associated with this error, if any.
* Return the controller method associated with the error, if any.
* @since 5.0.5
*/
@Nullable
public HandlerMethod getHandlerMethod() {
return this.handlerMethod;
}
/**
* Return the controller method argument associated with this error, if any.
*/
@Nullable
public MethodParameter getMethodParameter() {