-
Notifications
You must be signed in to change notification settings - Fork 0
Bug/25 report invalid parameters as 400 #29
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
3 commits
Select commit
Hold shift + click to select a range
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
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
50 changes: 50 additions & 0 deletions
50
...r/src/main/java/fr/ans/psc/toggle/controller/handlers/DataProcessingExceptionHandler.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,50 @@ | ||
| /* | ||
| * Copyright © 2022-2024 Agence du Numérique en Santé (ANS) (https://esante.gouv.fr) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package fr.ans.psc.toggle.controller.handlers; | ||
|
|
||
| import fr.ans.psc.toggle.exception.ToggleFileParsingException; | ||
| import org.junit.jupiter.params.shadow.com.univocity.parsers.common.DataProcessingException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.context.request.WebRequest; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
|
||
| /** | ||
| * This handler overrides the default error handling behavior to report toggle file parsing errors. | ||
| * | ||
| * @author edegenetais | ||
| */ | ||
| @ControllerAdvice | ||
| @Controller | ||
| @Order(Ordered.HIGHEST_PRECEDENCE) | ||
| public class DataProcessingExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(DataProcessingExceptionHandler.class); | ||
|
|
||
| @ExceptionHandler(value = DataProcessingException.class) | ||
| public final ResponseEntity handleAllExceptions( | ||
| DataProcessingException ex, WebRequest request) { | ||
| LOGGER.error("Invalid toggle file", ex); | ||
| return new ResponseEntity<>(new ErrorReport(ex.getMessage()), HttpStatus.BAD_REQUEST); | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
psc-toggle-manager/src/main/java/fr/ans/psc/toggle/controller/handlers/ErrorReport.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,33 @@ | ||
| /* | ||
| * Copyright © 2022-2024 Agence du Numérique en Santé (ANS) (https://esante.gouv.fr) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package fr.ans.psc.toggle.controller.handlers; | ||
|
|
||
| import java.util.Date; | ||
|
|
||
| /** | ||
| * Error report objet used to report errors to rest clients. | ||
| * | ||
| * @author edegenetais | ||
| */ | ||
| public class ErrorReport { | ||
| public final Date timestamp=new Date(); | ||
| public final String error; | ||
|
|
||
| public ErrorReport(String error) { | ||
| this.error = error; | ||
| } | ||
|
|
||
| } |
48 changes: 48 additions & 0 deletions
48
...src/main/java/fr/ans/psc/toggle/controller/handlers/InvalidParameterExceptionHandler.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,48 @@ | ||
| /* | ||
| * Copyright © 2022-2024 Agence du Numérique en Santé (ANS) (https://esante.gouv.fr) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package fr.ans.psc.toggle.controller.handlers; | ||
|
|
||
| import fr.ans.psc.toggle.exception.InvalidParameterException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.context.request.WebRequest; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
|
||
| /** | ||
| * This handler overrides the default error handling behavior to report invalid endpoint parameters. | ||
| * | ||
| * @author edegenetais | ||
| */ | ||
| @ControllerAdvice | ||
| @Controller | ||
| @Order(Ordered.HIGHEST_PRECEDENCE) | ||
| public class InvalidParameterExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(InvalidParameterException.class); | ||
|
|
||
| @ExceptionHandler(value = InvalidParameterException.class) | ||
| public final ResponseEntity handleAllExceptions(InvalidParameterException ex, WebRequest request) { | ||
| LOGGER.error("Invalid REST call parameters", ex); | ||
| return new ResponseEntity<>(new ErrorReport(ex.getMessage()), HttpStatus.BAD_REQUEST); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
...nager/src/main/java/fr/ans/psc/toggle/controller/handlers/UnexpectedExceptionHandler.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,47 @@ | ||
| /* | ||
| * Copyright © 2022-2024 Agence du Numérique en Santé (ANS) (https://esante.gouv.fr) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package fr.ans.psc.toggle.controller.handlers; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.context.request.WebRequest; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
|
||
| /** | ||
| * This default exception handler filters out server inner detail. | ||
| * | ||
| * @author edegenetais | ||
| */ | ||
| @ControllerAdvice | ||
| @Controller | ||
| @Order(Ordered.LOWEST_PRECEDENCE) | ||
| public class UnexpectedExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(UnexpectedExceptionHandler.class); | ||
|
|
||
| @ExceptionHandler(value = Throwable.class) | ||
| public final ResponseEntity handleAllExceptions(Throwable ex, WebRequest request) { | ||
| LOGGER.error("Unexpected error", ex); | ||
| return new ResponseEntity<>(new ErrorReport("Internal Server Error"), HttpStatus.INTERNAL_SERVER_ERROR); | ||
| } | ||
| } | ||
|
Comment on lines
+38
to
+47
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Si je comprends bien la logique,
C'est bien ça ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Effectivement, c'est ça. Double bénéfice attendu :
|
||
28 changes: 28 additions & 0 deletions
28
psc-toggle-manager/src/main/java/fr/ans/psc/toggle/exception/InvalidParameterException.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,28 @@ | ||
| /* | ||
| * Copyright © 2022-2024 Agence du Numérique en Santé (ANS) (https://esante.gouv.fr) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package fr.ans.psc.toggle.exception; | ||
|
|
||
| /** | ||
| * | ||
| * @author edegenetais | ||
| */ | ||
| public class InvalidParameterException extends RuntimeException{ | ||
|
|
||
| public InvalidParameterException(String string, Throwable thrwbl) { | ||
| super(string, thrwbl); | ||
| } | ||
|
|
||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Je n'ai pas le réflexe d'utiliser les handlers !
Quel était le comportement précédent en cas d'erreur ? On renvoyait quoi au client ? Une 500 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oui, on renvoyait une 500. Puisqu'on sait interrompre la requête avec retour d'erreur, autant faire un retour informatif.