Skip to content

Commit 79261de

Browse files
committed
Add mutators to the context to make it more obvious to the user what is modifiable and what is not.
1 parent a629e0f commit 79261de

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

src/main/java/io/fusionauth/http/server/DefaultHTTPUnexpectedExceptionHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
public class DefaultHTTPUnexpectedExceptionHandler implements HTTPUnexpectedExceptionHandler {
2424
@Override
2525
public void handle(ExceptionHandlerContext context) {
26-
context.logger.error(String.format("[%s] Closing socket with status [%d]. An HTTP worker threw an exception while processing a request.", Thread.currentThread().threadId(), context.statusCode), context.throwable);
26+
context.getLogger()
27+
.error(String.format("[%s] Closing socket with status [%d]. An HTTP worker threw an exception while processing a request.",
28+
Thread.currentThread().threadId(),
29+
context.getStatusCode()),
30+
context.getThrowable());
2731
}
2832
}

src/main/java/io/fusionauth/http/server/ExceptionHandlerContext.java

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,64 @@
2323
* @author Daniel DeGroff
2424
*/
2525
public class ExceptionHandlerContext {
26-
public Logger logger;
26+
private final Logger logger;
2727

28-
public HTTPRequest request;
28+
private final HTTPRequest request;
2929

30-
public int statusCode;
30+
private final Throwable throwable;
3131

32-
public Throwable throwable;
32+
private int statusCode;
3333

3434
public ExceptionHandlerContext(Logger logger, HTTPRequest request, int statusCode, Throwable throwable) {
3535
this.logger = logger;
36-
this.request = request; // may be null
36+
this.request = request;
3737
this.statusCode = statusCode;
3838
this.throwable = throwable;
3939
}
40+
41+
/**
42+
* This is provided for convenience, but you may wish to use your own logger.
43+
*
44+
* @return the optional logger to use in the exception handler.
45+
*/
46+
public Logger getLogger() {
47+
return logger;
48+
}
49+
50+
/**
51+
* This may be useful if you wish to know additional context of the exception such as the URI of the current HTTP request.
52+
* <p>
53+
* Modifications to this object will have no effect on current or futures requests.
54+
*
55+
* @return the current HTTP request, or null if this exception was taking prior to constructing the HTTP request. This is unlikely but
56+
* please account for this value being null.
57+
*/
58+
public HTTPRequest getRequest() {
59+
return request;
60+
}
61+
62+
/**
63+
* @return the desired status code for the HTTP response.
64+
*/
65+
public int getStatusCode() {
66+
return statusCode;
67+
}
68+
69+
/**
70+
* Suggest a status code for the HTTP response. This value will be used unless the response has already been committed meaning bytes have
71+
* already been written to the client and the HTTP server is not able to modify the response code.
72+
*
73+
* @param statusCode the desired status code to set on the HTTP response.
74+
*/
75+
public void setStatusCode(int statusCode) {
76+
this.statusCode = statusCode;
77+
}
78+
79+
/**
80+
*
81+
* @return the unexpected exception to handle
82+
*/
83+
public Throwable getThrowable() {
84+
return throwable;
85+
}
4086
}

src/main/java/io/fusionauth/http/server/internal/HTTPWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public void run() {
277277
}
278278

279279
// Signal an error
280-
closeSocketOnError(response, context.statusCode);
280+
closeSocketOnError(response, context.getStatusCode());
281281
} finally {
282282
if (instrumenter != null) {
283283
instrumenter.workerStopped();

0 commit comments

Comments
 (0)