Skip to content

Commit 98f1df9

Browse files
committed
Expose request and response in ResourceException.
1 parent 214720d commit 98f1df9

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

modules/org.restlet/src/main/java/org/restlet/resource/ClientResource.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,42 @@ public Representation delete(MediaType mediaType) throws ResourceException {
612612

613613
/**
614614
* By default, it throws a new resource exception.
615-
*
615+
* Call {@link #doError(org.restlet.data.Status, org.restlet.Request, org.restlet.Response)}.
616+
*
617+
* @param request
618+
* The associated request.
619+
* @param response
620+
* The associated response.
621+
*/
622+
public void doError(Request request, Response response) {
623+
doError(response.getStatus(), request, response);
624+
}
625+
626+
/**
627+
* By default, it throws a new resource exception.
628+
* Call {@link #doError(org.restlet.data.Status, org.restlet.Request, org.restlet.Response)}.
629+
*
616630
* @param errorStatus
617631
* The error status received.
618632
*/
619633
@Override
620634
public void doError(Status errorStatus) {
621-
throw new ResourceException(errorStatus, this);
635+
doError(errorStatus, getRequest(), getResponse());
636+
}
637+
638+
/**
639+
* By default, it throws a new resource exception.
640+
* This can be overridden to provide a different behavior.
641+
*
642+
* @param errorStatus
643+
* The error status received.
644+
* @param request
645+
* The associated request.
646+
* @param response
647+
* The associated response.
648+
*/
649+
public void doError(Status errorStatus, Request request, Response response) {
650+
throw new ResourceException(errorStatus, request, response);
622651
}
623652

624653
/**

modules/org.restlet/src/main/java/org/restlet/resource/ResourceException.java

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ public class ResourceException extends RuntimeException {
3737

3838
private static final long serialVersionUID = 1L;
3939

40-
/** The resource associated to this exception. Could be null. */
41-
private final Resource resource;
42-
4340
/** The status associated to this exception. */
4441
private final Status status;
4542

43+
/** The request associated to this exception. Could be null. */
44+
private final Request request;
45+
46+
/** The response associated to this exception. Could be null. */
47+
private final Response response;
48+
4649
/**
4750
* Constructor.
4851
*
@@ -186,22 +189,34 @@ public ResourceException(int code, Throwable throwable, String reasonPhrase, Str
186189
* The status to associate.
187190
*/
188191
public ResourceException(Status status) {
189-
this(status, (Throwable) ((status == null) ? null : status.getThrowable()));
192+
this(status, (status == null) ? null : status.getThrowable());
190193
}
191194

192195
/**
193196
* Constructor.
194197
*
195198
* @param status
196199
* The status to associate.
200+
* @deprecated use constructor with status, request and response instead.
197201
*/
202+
@Deprecated
198203
public ResourceException(Status status, Resource resource) {
199-
this(status, (Throwable) ((status == null) ? null : status.getThrowable()), resource);
204+
this(status, (status == null) ? null : status.getThrowable(), resource.getRequest(), resource.getResponse());
200205
}
201206

202207
/**
203208
* Constructor.
204-
*
209+
*
210+
* @param status
211+
* The status to associate.
212+
*/
213+
public ResourceException(Status status, Request request, Response response) {
214+
this(status, (status == null) ? null : status.getThrowable(), request, response);
215+
}
216+
217+
/**
218+
* Constructor.
219+
*
205220
* @param status
206221
* The status to copy.
207222
* @param description
@@ -227,34 +242,36 @@ public ResourceException(Status status, String description, Throwable cause) {
227242

228243
/**
229244
* Constructor.
230-
*
245+
*
231246
* @param status
232247
* The status to associate.
233248
* @param cause
234249
* The wrapped cause error or exception.
235250
*/
236251
public ResourceException(Status status, Throwable cause) {
237-
this(status, cause, null);
252+
this(status, cause, null, null);
238253
}
239254

240255
/**
241256
* Constructor.
242-
*
257+
*
243258
* @param status
244259
* The status to associate.
245260
* @param cause
246261
* The wrapped cause error or exception.
247262
*/
248-
public ResourceException(Status status, Throwable cause, Resource resource) {
263+
public ResourceException(Status status, Throwable cause, Request request, Response response) {
249264
super((status == null) ? null : status.toString(), cause);
250265
this.status = status;
251-
this.resource = resource;
266+
this.request = request;
267+
this.response = response;
252268
}
253269

254270
/**
255-
* Constructor that set the status to {@link org.restlet.data.Status#SERVER_ERROR_INTERNAL} including the
271+
* Constructor that set the status to
272+
* {@link org.restlet.data.Status#SERVER_ERROR_INTERNAL} including the
256273
* related error or exception.
257-
*
274+
*
258275
* @param cause
259276
* The wrapped cause error or exception.
260277
*/
@@ -264,34 +281,25 @@ public ResourceException(Throwable cause) {
264281

265282
/**
266283
* Returns the request associated to this exception.
267-
*
284+
*
268285
* @return The request associated to this exception.
269286
*/
270287
public Request getRequest() {
271-
return (this.resource != null) ? this.resource.getRequest() : null;
272-
}
273-
274-
/**
275-
* Returns the resource associated to this exception.
276-
*
277-
* @return The resource associated to this exception.
278-
*/
279-
public Resource getResource() {
280-
return this.resource;
288+
return this.request;
281289
}
282290

283291
/**
284292
* Returns the response associated to this exception.
285-
*
293+
*
286294
* @return The response associated to this exception.
287295
*/
288296
public Response getResponse() {
289-
return (this.resource != null) ? this.resource.getResponse() : null;
297+
return this.response;
290298
}
291299

292300
/**
293301
* Returns the status associated to this exception.
294-
*
302+
*
295303
* @return The status associated to this exception.
296304
*/
297305
public Status getStatus() {

0 commit comments

Comments
 (0)