Skip to content

Commit

Permalink
fix: 500 error to return actual message
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Aug 8, 2024
1 parent f0dfd83 commit 41f1d92
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [9.1.2] -2024-07-24

- Fixes path routing which rejected tenantId stop words even if it was not an exact stop word match. For example, `/hellotenant` is a valid tenantId prefix, however, it was being rejected for the stop word `hello`. - https://github.com/supertokens/supertokens-core/issues/1021
- 500 errors in core returns actual exception, since these APIs are developer facing, it makes easier to debug these errors.

## [9.1.1] -2024-07-24

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/supertokens/webserver/WebserverAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,10 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
} else if (rootCause instanceof BadPermissionException) {
sendTextResponse(403, rootCause.getMessage(), resp);
} else {
sendTextResponse(500, "Internal Error", resp);
sendTextResponse(500, rootCause.getMessage(), resp);
}
} else {
sendTextResponse(500, "Internal Error", resp);
sendTextResponse(500, e.getMessage(), resp);
}
}
Logging.info(main, tenantIdentifier, "API ended: " + req.getRequestURI() + ". Method: " + req.getMethod(),
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/io/supertokens/test/PathRouterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,79 @@ public void beforeEach() {
Utils.reset();
}

@Test
public void test500ErrorMessage() throws Exception {
String[] args = {"../"};
TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STARTED));

Webserver.getInstance(process.getProcess()).addAPI(new WebserverAPI(process.getProcess(), "") {

private static final long serialVersionUID = 1L;

@Override
public boolean checkAPIKey(HttpServletRequest req) {
return false;
}

@Override
public String getPath() {
return "/test/servlet-exception";
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
throw new ServletException(new RuntimeException("Test Exception"));
}
});

Webserver.getInstance(process.getProcess()).addAPI(new WebserverAPI(process.getProcess(), "") {

private static final long serialVersionUID = 1L;

@Override
public boolean checkAPIKey(HttpServletRequest req) {
return false;
}

@Override
public String getPath() {
return "/test/runtime-exception";
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
throw new RuntimeException("Runtime Exception");
}
});

{
try {
String response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
"http://localhost:3567/test/servlet-exception", new HashMap<>(), 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "");
fail();
} catch (HttpResponseException e) {
assertEquals(500, e.statusCode);
assertEquals("Http error. Status Code: 500. Message: Test Exception", e.getMessage());
}
}

{
try {
String response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
"http://localhost:3567/test/runtime-exception", new HashMap<>(), 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "");
fail();
} catch (HttpResponseException e) {
assertEquals(500, e.statusCode);
assertEquals("Http error. Status Code: 500. Message: Runtime Exception", e.getMessage());
}
}
}

@Test
public void basicTenantIdFetchingTest()
throws InterruptedException, IOException, HttpResponseException, InvalidProviderConfigException,
Expand Down

0 comments on commit 41f1d92

Please sign in to comment.