Skip to content

Commit 259236a

Browse files
committed
Fix error handling
1 parent 28ccf04 commit 259236a

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/main/java/eu/openanalytics/containerproxy/ui/ErrorController.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public String handleError(ModelMap map, HttpServletRequest request, HttpServletR
6868
if (statusCode == HttpStatus.NOT_FOUND.value()) {
6969
shortError = "Not found";
7070
description = "The requested page was not found";
71+
} else if (statusCode == HttpStatus.BAD_REQUEST.value()) {
72+
shortError = "Bad Request";
73+
description = "You are not allowed to send this request.";
7174
} else if (statusCode == HttpStatus.FORBIDDEN.value()) {
7275
shortError = "Forbidden";
7376
description = "You do not have access to this page";
@@ -99,11 +102,17 @@ public ResponseEntity<ApiResponse<Object>> error(HttpServletRequest request, Htt
99102
return ApiResponse.fail("bad request");
100103
}
101104

105+
if (exception.isPresent() && exception.get() instanceof RequestRejectedException) {
106+
return ApiResponse.fail("bad request");
107+
}
108+
102109
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
103110
if (status != null) {
104111
int statusCode = Integer.parseInt(status.toString());
105112
if (statusCode == HttpStatus.NOT_FOUND.value()) {
106113
return ApiResponse.failNotFound();
114+
} else if (statusCode == HttpStatus.BAD_REQUEST.value()) {
115+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ApiResponse<>("fail", "bad request"));
107116
} else if (statusCode == HttpStatus.UNAUTHORIZED.value()) {
108117
return ApiResponse.failUnauthorized();
109118
} else if (statusCode == HttpStatus.FORBIDDEN.value()) {
@@ -123,7 +132,7 @@ private boolean isAccountStatusException(Throwable exception) {
123132
}
124133

125134
private Optional<Throwable> getException(HttpServletRequest request) {
126-
Throwable exception = (Throwable) request.getAttribute("javax.servlet.error.exception");
135+
Throwable exception = (Throwable) request.getAttribute("jakarta.servlet.error.exception");
127136
if (exception == null) {
128137
exception = (Throwable) request.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
129138
}

0 commit comments

Comments
 (0)