Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package fr.ans.psc.toggle.controller;

import fr.ans.psc.toggle.exception.InvalidParameterException;
import fr.ans.psc.toggle.model.PsIdType;
import fr.ans.psc.toggle.service.ToggleService;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -42,9 +43,20 @@ public ToggleController(ToggleService toggleService) {
@PostMapping(value = "/toggle", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Void> toggleRegistrySource(@RequestParam("from") String from, @RequestParam("to") String to, @RequestParam("toggleFile") MultipartFile mpFile) {
toggleService.toggle(mpFile, PsIdType.valueOf(from.toUpperCase()), PsIdType.valueOf(to.toUpperCase()));

final PsIdType sourceIdType = decodeIdType(from, "from");
final PsIdType destinationIdType = decodeIdType(to, "to");
toggleService.toggle(mpFile, sourceIdType, destinationIdType);
return new ResponseEntity<>(HttpStatus.ACCEPTED);

}


private PsIdType decodeIdType(String name, final String parmName) {
try {
return PsIdType.valueOf(name.toUpperCase());
} catch (IllegalArgumentException e) {
throw new InvalidParameterException(parmName + ": " + e.getMessage(), e);
}
}
}
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);
}
}
Comment on lines +40 to +50
Copy link
Copy Markdown
Collaborator

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 ?

Copy link
Copy Markdown
Contributor Author

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.

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;
}

}
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);
}
}
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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si je comprends bien la logique,

  • on identifie toutes les erreurs donc on sait qu'elles sont liées à la requête
  • on les gère avec un code 400 et une information minimale dans le corps de la réponse
  • on gère un handleAll en dernier lieu avec un code 500 pour ce qui nous aurait échappé
  • on ne renvoie au client qu'un rapport d'erreur limité à timestamp + message de l'exception
  • on log côté serveur pour pouvoir retrouver l'info a posteriori an croisant avec le timestamp fourni par le client

C'est bien ça ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effectivement, c'est ça. Double bénéfice attendu :

  • retour appelant plus clair (typologie d'erreurs)
  • moins de fuite d'info sur la stack

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);
}

}