From 41f1d92d5baebc6d1311c488e20e0208f0c91ed6 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Thu, 8 Aug 2024 17:02:24 +0530 Subject: [PATCH] fix: 500 error to return actual message --- CHANGELOG.md | 1 + .../supertokens/webserver/WebserverAPI.java | 4 +- .../io/supertokens/test/PathRouterTest.java | 73 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb771b811..a35e28407 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/io/supertokens/webserver/WebserverAPI.java b/src/main/java/io/supertokens/webserver/WebserverAPI.java index 658f7fe2c..3cf62be23 100644 --- a/src/main/java/io/supertokens/webserver/WebserverAPI.java +++ b/src/main/java/io/supertokens/webserver/WebserverAPI.java @@ -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(), diff --git a/src/test/java/io/supertokens/test/PathRouterTest.java b/src/test/java/io/supertokens/test/PathRouterTest.java index 3f5879adb..677c404a0 100644 --- a/src/test/java/io/supertokens/test/PathRouterTest.java +++ b/src/test/java/io/supertokens/test/PathRouterTest.java @@ -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,