Skip to content

Commit 4ebae70

Browse files
committed
Let http server handle most exceptions, and only log unexpected exceptions as error.
1 parent feaed02 commit 4ebae70

File tree

7 files changed

+47
-13
lines changed

7 files changed

+47
-13
lines changed

build.savant

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dropWizardVersion = "3.2.6"
1919
easyMockVersion = "5.2.0"
2020
freemarkerVersion = "2.3.32"
2121
fusionAuthJWTVersion = "5.3.2"
22-
javaHTTPVersion = "1.1.2-{integration}"
22+
javaHTTPVersion = "1.2.0-{integration}"
2323
jsonPatchVersion = "1.13.0"
2424
guavaVersion = "32.1.2-jre"
2525
guiceVersion = "6.0.0"

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<dependency>
110110
<groupId>io.fusionauth</groupId>
111111
<artifactId>java-http</artifactId>
112-
<version>1.1.2-{integration}</version>
112+
<version>1.2.0-{integration}</version>
113113
<type>jar</type>
114114
<scope>compile</scope>
115115
<optional>false</optional>

prime-mvc.iml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@
126126
<orderEntry type="module-library">
127127
<library>
128128
<CLASSES>
129-
<root url="jar://$USER_HOME$/.savant/cache/io/fusionauth/java-http/1.1.2-{integration}/java-http-1.1.2-{integration}.jar!/" />
129+
<root url="jar://$USER_HOME$/.savant/cache/io/fusionauth/java-http/1.2.0-{integration}/java-http-1.2.0-{integration}.jar!/" />
130130
</CLASSES>
131131
<JAVADOC />
132132
<SOURCES>
133-
<root url="jar://$USER_HOME$/.savant/cache/io/fusionauth/java-http/1.1.2-{integration}/java-http-1.1.2-{integration}-src.jar!/" />
133+
<root url="jar://$USER_HOME$/.savant/cache/io/fusionauth/java-http/1.2.0-{integration}/java-http-1.2.0-{integration}-src.jar!/" />
134134
</SOURCES>
135135
</library>
136136
</orderEntry>

src/main/java/org/primeframework/mvc/BasePrimeMain.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ public void start() {
123123

124124
// Create the server
125125
var server = new HTTPServer().withConfiguration(config)
126+
// Note that by calling each of these methods directly, we are functionally
127+
// ignoring these three values on the configuration object provided by
128+
// BasePrimeMain. This just means that the implementor cannot currently
129+
// provide their own request handler, exception handler or instrumentor.
126130
.withHandler(requestHandler)
131+
.withUnexpectedExceptionHandler(new PrimeMVCUnexpectedExceptionHandler())
127132
.withInstrumenter(instrumenter);
128133

129134
servers.add(new PrimeHTTPServer(requestHandler, server));

src/main/java/org/primeframework/mvc/PrimeMVCRequestHandler.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
package org.primeframework.mvc;
1717

1818
import java.io.Closeable;
19-
import java.net.SocketException;
2019

2120
import com.google.inject.Injector;
2221
import io.fusionauth.http.HTTPMethod;
2322
import io.fusionauth.http.server.HTTPHandler;
2423
import io.fusionauth.http.server.HTTPRequest;
2524
import io.fusionauth.http.server.HTTPResponse;
26-
import io.fusionauth.http.server.io.ConnectionClosedException;
2725
import org.primeframework.mvc.action.result.MVCWorkflowFinalizer;
2826
import org.primeframework.mvc.guice.GuiceBootstrap;
2927
import org.primeframework.mvc.http.HTTPObjectsHolder;
@@ -70,14 +68,12 @@ public void handle(HTTPRequest request, HTTPResponse response) throws Exception
7068
HTTPObjectsHolder.setRequest(request);
7169
HTTPObjectsHolder.setResponse(response);
7270

71+
// Do not catch any exceptions, the HTTP server will handle exceptions.
72+
// - If we do want to log or perform any specific handling when an unexpected
73+
// exception is thrown, you may configure an UnexpectedException handler.
74+
// See HTTPServerConfiguration.withUnexpectedExceptionHandler
7375
try {
7476
injector.getInstance(MVCWorkflow.class).perform(null);
75-
} catch (ConnectionClosedException | SocketException e) {
76-
// Catch, ignore and let java-http handle these
77-
throw e;
78-
} catch (Throwable t) {
79-
logger.error("Error encountered", t);
80-
throw t; // java-http will cause this error to write back a 500 if possible and close the socket
8177
} finally {
8278
HTTPObjectsHolder.clearRequest();
8379
HTTPObjectsHolder.clearResponse();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2025, Inversoft Inc., All Rights Reserved
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an
12+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*/
16+
package org.primeframework.mvc;
17+
18+
import io.fusionauth.http.server.HTTPResponse;
19+
import io.fusionauth.http.server.HTTPUnexpectedExceptionHandler;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
/**
24+
* @author Daniel DeGroff
25+
*/
26+
public class PrimeMVCUnexpectedExceptionHandler implements HTTPUnexpectedExceptionHandler {
27+
private static final Logger logger = LoggerFactory.getLogger(PrimeMVCUnexpectedExceptionHandler.class);
28+
29+
@Override
30+
public void handle(HTTPResponse response, Throwable t) {
31+
logger.error("Error encountered", t);
32+
}
33+
}

src/test/java/org/primeframework/mvc/StaticResourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void get_classpath_resolution() {
5454
simulator.test("/org/primeframework/mvc/PrimeMVCRequestHandler.class")
5555
.get()
5656
.assertStatusCode(200)
57-
.assertContentLength(2939);
57+
.assertContentLength(2613);
5858
}
5959

6060
@Test

0 commit comments

Comments
 (0)