mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-14 10:34:06 +02:00
fix folder causing 500 toast when deleted on another machine (#6551)
This commit is contained in:
@@ -24,6 +24,7 @@ import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
import org.springframework.web.servlet.resource.NoResourceFoundException;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
@@ -993,6 +994,49 @@ public class GlobalExceptionHandler {
|
||||
.body(problemDetail);
|
||||
}
|
||||
|
||||
/** Unmapped path → clean 404 instead of falling through to the generic 500 catch-all. */
|
||||
@ExceptionHandler(NoResourceFoundException.class)
|
||||
public ResponseEntity<ProblemDetail> handleNoResourceFound(
|
||||
NoResourceFoundException ex, HttpServletRequest request) {
|
||||
// /api/* miss = likely missing controller (operator-relevant); other paths = favicons,
|
||||
// robots.txt, scanner noise. Demote the latter so prod logs aren't flooded.
|
||||
String uri = request.getRequestURI();
|
||||
if (uri != null && uri.startsWith("/api/")) {
|
||||
log.warn("No resource at {}: {}", uri, ex.getMessage());
|
||||
} else {
|
||||
log.debug("No resource at {}: {}", uri, ex.getMessage());
|
||||
}
|
||||
|
||||
String title = getLocalizedMessage("error.notFound.title", ErrorTitles.NOT_FOUND_DEFAULT);
|
||||
String detail =
|
||||
getLocalizedMessage(
|
||||
"error.notFound.detail",
|
||||
String.format(
|
||||
"No endpoint found for %s %s",
|
||||
request.getMethod(), request.getRequestURI()),
|
||||
request.getMethod(),
|
||||
request.getRequestURI());
|
||||
|
||||
ProblemDetail problemDetail =
|
||||
createBaseProblemDetail(HttpStatus.NOT_FOUND, detail, request);
|
||||
problemDetail.setType(URI.create(ErrorTypes.NOT_FOUND));
|
||||
problemDetail.setTitle(title);
|
||||
problemDetail.setProperty("title", title);
|
||||
problemDetail.setProperty("method", request.getMethod());
|
||||
addStandardHints(
|
||||
problemDetail,
|
||||
"error.notFound.hints",
|
||||
List.of(
|
||||
"Verify the URL path and HTTP method are correct.",
|
||||
"Check the API base path and version if applicable.",
|
||||
"Ensure there are no typos in the endpoint path."));
|
||||
problemDetail.setProperty("actionRequired", "Use a valid endpoint URL and method.");
|
||||
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||
.contentType(PROBLEM_JSON)
|
||||
.body(problemDetail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle IllegalArgumentException.
|
||||
*
|
||||
|
||||
+15
@@ -18,6 +18,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -27,6 +28,7 @@ import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
import org.springframework.web.servlet.resource.NoResourceFoundException;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
@@ -208,6 +210,19 @@ class GlobalExceptionHandlerTest {
|
||||
assertEquals(HttpStatus.NOT_FOUND, resp.getStatusCode());
|
||||
}
|
||||
|
||||
// ---- NoResourceFoundException ----
|
||||
// Regression guard: was falling through to the 500 catch-all.
|
||||
|
||||
@Test
|
||||
void handleNoResourceFound_returns_404_not_500() {
|
||||
when(request.getMethod()).thenReturn("GET");
|
||||
NoResourceFoundException ex =
|
||||
new NoResourceFoundException(HttpMethod.GET, "/api/v1/storage/folders", "");
|
||||
ResponseEntity<ProblemDetail> resp = handler.handleNoResourceFound(ex, request);
|
||||
assertEquals(HttpStatus.NOT_FOUND, resp.getStatusCode());
|
||||
assertEquals("GET", resp.getBody().getProperties().get("method"));
|
||||
}
|
||||
|
||||
// ---- IllegalArgumentException ----
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user