1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import org .springframework .core .MethodParameter ;
20
20
import org .springframework .http .HttpStatus ;
21
21
import org .springframework .lang .Nullable ;
22
+ import org .springframework .web .method .HandlerMethod ;
22
23
23
24
/**
24
- * Exception for errors that fit response status 500 (bad request) for use in
25
- * Spring Web applications. The exception provides additional fields (e.g.
26
- * an optional {@link MethodParameter} if related to the error) .
25
+ * Exception for an {@link HttpStatus#INTERNAL_SERVER_ERROR} that exposes extra
26
+ * information about a controller method that failed, or a controller method
27
+ * argument that could not be resolved .
27
28
*
28
29
* @author Rossen Stoyanchev
29
30
* @since 5.0
30
31
*/
31
32
@ SuppressWarnings ("serial" )
32
33
public class ServerErrorException extends ResponseStatusException {
33
34
35
+ @ Nullable
36
+ private final HandlerMethod handlerMethod ;
37
+
34
38
@ Nullable
35
39
private final MethodParameter parameter ;
36
40
@@ -39,27 +43,60 @@ public class ServerErrorException extends ResponseStatusException {
39
43
* Constructor with an explanation only.
40
44
*/
41
45
public ServerErrorException (String reason ) {
42
- this (reason , null , null );
46
+ super (HttpStatus .INTERNAL_SERVER_ERROR , reason , null );
47
+ this .handlerMethod = null ;
48
+ this .parameter = null ;
43
49
}
44
50
45
51
/**
46
- * Constructor for a 500 error linked to a specific {@code MethodParameter}.
52
+ * Constructor with a reason and root cause.
53
+ * @since 5.0.5
47
54
*/
48
- public ServerErrorException (String reason , MethodParameter parameter ) {
49
- this (reason , parameter , null );
55
+ public ServerErrorException (String reason , Throwable cause ) {
56
+ super (HttpStatus .INTERNAL_SERVER_ERROR , reason , cause );
57
+ this .handlerMethod = null ;
58
+ this .parameter = null ;
50
59
}
51
60
52
61
/**
53
- * Constructor for a 500 error with a root cause .
62
+ * Constructor for a 500 error with a {@link MethodParameter} .
54
63
*/
55
- public ServerErrorException (String reason , @ Nullable MethodParameter parameter , @ Nullable Throwable cause ) {
64
+ public ServerErrorException (String reason , MethodParameter parameter , @ Nullable Throwable cause ) {
56
65
super (HttpStatus .INTERNAL_SERVER_ERROR , reason , cause );
66
+ this .handlerMethod = null ;
57
67
this .parameter = parameter ;
58
68
}
59
69
70
+ /**
71
+ * Constructor for a 500 error with a root cause.
72
+ */
73
+ public ServerErrorException (String reason , HandlerMethod handlerMethod , @ Nullable Throwable cause ) {
74
+ super (HttpStatus .INTERNAL_SERVER_ERROR , reason , cause );
75
+ this .handlerMethod = handlerMethod ;
76
+ this .parameter = null ;
77
+ }
78
+
79
+ /**
80
+ * Constructor for a 500 error linked to a specific {@code MethodParameter}.
81
+ * @deprecated in favor of {@link #ServerErrorException(String, MethodParameter, Throwable)}
82
+ */
83
+ @ Deprecated
84
+ public ServerErrorException (String reason , MethodParameter parameter ) {
85
+ this (reason , parameter , null );
86
+ }
87
+
88
+
89
+ /**
90
+ * Return the controller method associated with the error, if any.
91
+ * @since 5.0.5
92
+ */
93
+ @ Nullable
94
+ public HandlerMethod getHandlerMethod () {
95
+ return this .handlerMethod ;
96
+ }
60
97
61
98
/**
62
- * Return the {@code MethodParameter} associated with this error, if any.
99
+ * Return the controller method argument associated with this error, if any.
63
100
*/
64
101
@ Nullable
65
102
public MethodParameter getMethodParameter () {
0 commit comments