-
Notifications
You must be signed in to change notification settings - Fork 2
add logs when receiving unexpected request param values #937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a2b2916
add logs when receiving unexpected request param values
AbdelHedhili 0e0e117
Merge branch 'main' into add_logger_on_invalid_request_param
AbdelHedhili 15d3e7d
refacto to not modify the previous behavior
AbdelHedhili 83883fe
try to fix CI fails
AbdelHedhili ec3c84f
Revert "try to fix CI fails"
AbdelHedhili 8daaf4a
Merge branch 'main' into add_logger_on_invalid_request_param
AbdelHedhili f80abd2
Merge branch 'main' into add_logger_on_invalid_request_param
AbdelHedhili 522147d
add comment
AbdelHedhili 88644a8
trailing space
AbdelHedhili File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/main/java/org/gridsuite/study/server/error/RequestLoggingExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Copyright (c) 2026, RTE (http://www.rte-france.com) | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| package org.gridsuite.study.server.error; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import java.util.Map; | ||
| import java.util.StringJoiner; | ||
|
|
||
| import lombok.NonNull; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.lang.Nullable; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
| import org.springframework.web.servlet.HandlerExceptionResolver; | ||
| import org.springframework.web.servlet.ModelAndView; | ||
|
|
||
| /** | ||
| * @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com> | ||
| */ | ||
| @ControllerAdvice | ||
| @Order(Ordered.HIGHEST_PRECEDENCE) | ||
| /* | ||
| This handler is used to catch MethodArgumentTypeMismatchException exceptions | ||
| that do not provide enough information on their own and to log details about | ||
| the request that caused them. | ||
|
|
||
| After logging is completed, the exception propagates normally, so this class | ||
| does not modify any behavior. | ||
| */ | ||
| public class RequestLoggingExceptionHandler implements HandlerExceptionResolver { | ||
| private static final Logger A_LOGGER = LoggerFactory.getLogger(RequestLoggingExceptionHandler.class); | ||
|
|
||
| @Override | ||
| public @Nullable ModelAndView resolveException( | ||
| @NonNull HttpServletRequest request, | ||
| @NonNull HttpServletResponse response, | ||
| Object handler, | ||
| @NonNull Exception ex) { | ||
| if (ex instanceof MethodArgumentTypeMismatchException mismatch) { | ||
| String fullUri = buildFullUri(request); | ||
| A_LOGGER.error( | ||
| "{} : method={} uri={} paramCausingIssue={} valueOfParamCausingIssue={}", | ||
| mismatch.getMessage(), | ||
| request.getMethod(), | ||
| fullUri, | ||
| mismatch.getName(), | ||
| mismatch.getValue()); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static String buildFullUri(HttpServletRequest request) { | ||
| String query = buildQueryString(request.getParameterMap()); | ||
| return request.getRequestURI() + (query.isEmpty() ? "" : "?" + query); | ||
| } | ||
|
|
||
| private static String buildQueryString(Map<String, String[]> parameters) { | ||
| StringJoiner joiner = new StringJoiner("&"); | ||
| if (parameters == null || parameters.isEmpty()) { | ||
| return ""; | ||
| } | ||
| for (Map.Entry<String, String[]> entry : parameters.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String[] values = entry.getValue(); | ||
| //If the param doesn't have a value we can go on directly | ||
| if (values == null || values.length == 0) { | ||
| joiner.add(key); | ||
| continue; | ||
| } | ||
| for (String value : values) { | ||
| joiner.add(key + "=" + value); | ||
| } | ||
| } | ||
| return joiner.toString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.