@@ -47,7 +47,8 @@ public void writeError_401() throws Exception {
47
47
@ Test
48
48
public void writeError_402 () throws Exception {
49
49
doWriteErrorTest (402 /* exceptionCode */ , 404 /* expectedCompatCode */ ,
50
- "unsupportedProtocol" /* compatReason */ , "paymentRequired" /* reason */ );
50
+ "unsupportedProtocol" /* compatReason */ , "paymentRequired" /* reason */ ,
51
+ "error" /* message */ );
51
52
}
52
53
53
54
@ Test
@@ -68,19 +69,21 @@ public void writeError_405() throws Exception {
68
69
@ Test
69
70
public void writeError_406 () throws Exception {
70
71
doWriteErrorTest (406 /* exceptionCode */ , 404 /* expectedCompatCode */ ,
71
- "unsupportedProtocol" /* compatReason */ , "notAcceptable" /* reason */ );
72
+ "unsupportedProtocol" /* compatReason */ , "notAcceptable" /* reason */ ,
73
+ "error" /* message */ );
72
74
}
73
75
74
76
@ Test
75
77
public void writeError_407 () throws Exception {
76
78
doWriteErrorTest (407 /* exceptionCode */ , 404 /* expectedCompatCode */ ,
77
- "unsupportedProtocol" /* compatReason */ , "proxyAuthentication" /* reason */ );
79
+ "unsupportedProtocol" /* compatReason */ , "proxyAuthentication" /* reason */ ,
80
+ "error" /* message */ );
78
81
}
79
82
80
83
@ Test
81
84
public void writeError_408 () throws Exception {
82
85
doWriteErrorTest (408 /* exceptionCode */ , 503 /* expectedCompatCode */ ,
83
- "backendError" /* compatReason */ , "requestTimeout" /* reason */ );
86
+ "backendError" /* compatReason */ , "requestTimeout" /* reason */ , "error" /* message */ );
84
87
}
85
88
86
89
@ Test
@@ -96,7 +99,8 @@ public void writeError_410() throws Exception {
96
99
@ Test
97
100
public void writeError_411 () throws Exception {
98
101
doWriteErrorTest (411 /* exceptionCode */ , 404 /* expectedCompatCode */ ,
99
- "unsupportedProtocol" /* compatReason */ , "lengthRequired" /* reason */ );
102
+ "unsupportedProtocol" /* compatReason */ , "lengthRequired" /* reason */ ,
103
+ "error" /* message */ );
100
104
}
101
105
102
106
@ Test
@@ -112,25 +116,25 @@ public void writeError_413() throws Exception {
112
116
@ Test
113
117
public void writeError_414 () throws Exception {
114
118
doWriteErrorTest (414 /* exceptionCode */ , 404 /* expectedCompatCode */ ,
115
- "unsupportedProtocol" /* compatReason */ , "uriTooLong" /* reason */ );
119
+ "unsupportedProtocol" /* compatReason */ , "uriTooLong" /* reason */ , "error" /* message */ );
116
120
}
117
121
118
122
@ Test
119
123
public void writeError_415 () throws Exception {
120
124
doWriteErrorTest (415 /* exceptionCode */ , 404 /* expectedCompatCode */ ,
121
- "unsupportedProtocol" /* compatReason */ , "unsupportedMediaType" );
125
+ "unsupportedProtocol" /* compatReason */ , "unsupportedMediaType" , "error" /* message */ );
122
126
}
123
127
124
128
@ Test
125
129
public void writeError_416 () throws Exception {
126
130
doWriteErrorTest (416 /* exceptionCode */ , 404 /* expectedCompatCode */ ,
127
- "unsupportedProtocol" /* compatReason */ , "rangeNotSatisfiable" );
131
+ "unsupportedProtocol" /* compatReason */ , "rangeNotSatisfiable" , "error" /* message */ );
128
132
}
129
133
130
134
@ Test
131
135
public void writeError_417 () throws Exception {
132
136
doWriteErrorTest (417 /* exceptionCode */ , 404 /* expectedCompatCode */ ,
133
- "unsupportedProtocol" /* compatReason */ , "expectationFailed" );
137
+ "unsupportedProtocol" /* compatReason */ , "expectationFailed" , "error" /* message */ );
134
138
}
135
139
136
140
@ Test
@@ -141,40 +145,54 @@ public void writeError_500s() throws Exception {
141
145
}
142
146
}
143
147
148
+ @ Test
149
+ public void writeError_nullMessage () throws Exception {
150
+ doWriteErrorTest (500 /* exceptionCode */ , 503 /* expectedCompatCode */ ,
151
+ "backendError" /* compatReason */ , "backendError" /* reason */ , null );
152
+ }
153
+
144
154
/**
145
155
* Tests that an error is translated according to Lily if specified, and the code is left alone
146
156
* if compatibility mode is off. Both cases test for the correct error structure in the response.
147
157
*/
148
158
private void doWriteErrorTest (int exceptionCode , int expectedCompatCode , String reason )
149
159
throws Exception {
150
- doWriteErrorTest (exceptionCode , expectedCompatCode , reason , reason );
160
+ doWriteErrorTest (exceptionCode , expectedCompatCode , reason , reason , "error" );
151
161
}
152
162
153
163
/**
154
164
* Tests that an error is translated according to Lily if specified, and the code is left alone
155
165
* if compatibility mode is off. Both cases test for the correct error structure in the response.
156
166
*/
157
167
private void doWriteErrorTest (int exceptionCode , int expectedCompatCode , String compatReason ,
158
- String reason ) throws Exception {
159
- writeError (exceptionCode , expectedCompatCode , compatReason , true );
160
- writeError (exceptionCode , exceptionCode , reason , false );
168
+ String reason , String message ) throws Exception {
169
+ writeError (exceptionCode , expectedCompatCode , compatReason , message , true );
170
+ writeError (exceptionCode , exceptionCode , reason , message , false );
161
171
}
162
172
163
- private void writeError (int exceptionCode , int expectedCode , String reason ,
173
+ private void writeError (int exceptionCode , int expectedCode , String reason , String message ,
164
174
boolean enableExceptionCompatibility ) throws Exception {
165
175
MockHttpServletResponse response = new MockHttpServletResponse ();
166
176
RestResponseResultWriter writer = new RestResponseResultWriter (
167
177
response , null , true /* prettyPrint */ , enableExceptionCompatibility );
168
- writer .writeError (new ServiceException (exceptionCode , "error" ));
178
+ writer .writeError (new ServiceException (exceptionCode , message ));
169
179
ObjectMapper mapper = ObjectMapperUtil .createStandardObjectMapper ();
170
180
ObjectNode content = mapper .readValue (response .getContentAsString (), ObjectNode .class );
171
181
JsonNode outerError = content .path ("error" );
172
182
assertThat (outerError .path ("code" ).asInt ()).isEqualTo (expectedCode );
173
- assertThat (outerError .path ("message" ).asText ()).isEqualTo ("error" );
183
+ if (message == null ) {
184
+ assertThat (outerError .path ("message" ).isNull ()).isTrue ();
185
+ } else {
186
+ assertThat (outerError .path ("message" ).asText ()).isEqualTo (message );
187
+ }
174
188
JsonNode innerError = outerError .path ("errors" ).path (0 );
175
189
assertThat (innerError .path ("domain" ).asText ()).isEqualTo ("global" );
176
190
assertThat (innerError .path ("reason" ).asText ()).isEqualTo (reason );
177
- assertThat (innerError .path ("message" ).asText ()).isEqualTo ("error" );
191
+ if (message == null ) {
192
+ assertThat (innerError .path ("message" ).isNull ()).isTrue ();
193
+ } else {
194
+ assertThat (innerError .path ("message" ).asText ()).isEqualTo (message );
195
+ }
178
196
}
179
197
180
198
@ Test
0 commit comments