Unable to catch MethodArgumentTypeMismatchException on doFilter()

I have a filter that basically logs incoming and outgoing information, but it seems that using this custom RequestLoggingFilter implements Filter creates a problem, I get MethodArgumentTypeMismatchException when a request's URI is unmapped. Error as follows:
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; For input string: "all"
...
dev.realtards.kuenyawz.filters.RequestLoggingFilter.doFilter(RequestLoggingFilter.java:54) ~[classes/:na]
...
Caused by: java.lang.NumberFormatException: For input string: "all"
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; For input string: "all"
...
dev.realtards.kuenyawz.filters.RequestLoggingFilter.doFilter(RequestLoggingFilter.java:54) ~[classes/:na]
...
Caused by: java.lang.NumberFormatException: For input string: "all"
I believe adding the non-existent URI "all" which I don't even map caused this problem. I find it weird since I could not catch the exception in:
6 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @circle! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
circle
circleOP2mo ago
Somewhy it got filtered
circle
circleOP2mo ago
on the catch, I have tried adding the exception and Exception, but the error could not be handled by my overridden doFilter() method. It could only be handled by my global advice exception handlers, specifically the MethodArgumentTypeMismatchException handler, where I don't want that! I want it to return some kind of 404 since the handler is not found
circle
circleOP2mo ago
It never went through the catch. It is also important to see that the error came from the servlet handlers:
No description
circle
circleOP2mo ago
I'd like a guide on this handling. Or is there a simpler approach? Such as denying the request with 404 (I'm trying to do that) Severity: clogs the logger Currently doing this goofy ahh handler:
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatch(
MethodArgumentTypeMismatchException ex) {

String message = "Endpoint does not exist";
// message = String.format("Failed to convert value '%s' to type %s", ex.getValue(), Objects.requireNonNull(ex.getRequiredType()).getSimpleName());

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorResponse(message));
}
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatch(
MethodArgumentTypeMismatchException ex) {

String message = "Endpoint does not exist";
// message = String.format("Failed to convert value '%s' to type %s", ex.getValue(), Objects.requireNonNull(ex.getRequiredType()).getSimpleName());

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorResponse(message));
}
Wait Wait a minte\ maybe it's because it's trying to access GET /api/products/{productId}, but instead of Id, the provided thing was "all" which does not make sense and should throw that tehee
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@389034898666553344>.
Want results from more Discord servers?
Add your server