Fix :: Erreur 401 sur toutes les ressources à la levée d'une exception quelconque.

Modification des controlleurs et ajout des blocs try.. catch
This commit is contained in:
2025-03-19 08:34:15 +01:00
parent 368d38a936
commit 7ebca1e1a3
262 changed files with 8390 additions and 3883 deletions

View File

@@ -2,13 +2,15 @@ package io.gmss.fiscad.controllers.decoupage;
import io.gmss.fiscad.entities.decoupage.Arrondissement;
import io.gmss.fiscad.exceptions.NotFoundException;
import io.gmss.fiscad.exceptions.*;
import io.gmss.fiscad.interfaces.decoupage.ArrondissementService;
import io.gmss.fiscad.paylaods.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
@@ -16,7 +18,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
@RestController
@@ -27,6 +29,7 @@ import org.springframework.web.bind.annotation.*;
public class ArrondissementController {
private final ArrondissementService arrondissementService;
private static final Logger logger = LoggerFactory.getLogger(ArrondissementController.class);
public ArrondissementController(ArrondissementService arrondissementService) {
this.arrondissementService = arrondissementService;
@@ -40,18 +43,26 @@ public class ArrondissementController {
@io.swagger.v3.oas.annotations.responses.ApiResponse(description = "Unauthorized / Invalid Token", responseCode = "403")}
)
@PostMapping("/create")
public ResponseEntity<?> createArrondissement(@RequestBody @Valid @Validated Arrondissement arrondissement) {
try{
public ResponseEntity<?> createArrondissement(@RequestBody @Valid @Validated Arrondissement arrondissement) {
try {
arrondissement = arrondissementService.createArrondissement(arrondissement);
return new ResponseEntity<>(
new ApiResponse<>(true, arrondissement, "Arrondissement créé avec succès."),
HttpStatus.OK
);
}catch (Exception e){
return new ResponseEntity<>(
new ApiResponse<>(false, e.getMessage()),
HttpStatus.OK
);
} catch (HttpClientErrorException.MethodNotAllowed e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK);
} catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException |
FileStorageException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK);
} catch (NullPointerException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK);
}
}
@@ -64,73 +75,144 @@ public class ArrondissementController {
)
@PutMapping("/update/{id}")
public ResponseEntity<?> updateArrondissement(@PathVariable Long id, @RequestBody Arrondissement arrondissement) {
try{
try {
return new ResponseEntity<>(
new ApiResponse<>(true, arrondissementService.updateArrondissement(id, arrondissement), "Arrondissement mis à jour avec succès."),
HttpStatus.OK
);
}catch (Exception e){
return new ResponseEntity<>(
new ApiResponse<>(false, e.getMessage()),
HttpStatus.OK
);
} catch (HttpClientErrorException.MethodNotAllowed e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK);
} catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException |
FileStorageException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK);
} catch (NullPointerException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK);
}
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<?> deleteArrondissement(@PathVariable Long id) {
try{
try {
arrondissementService.deleteArrondissement(id);
return new ResponseEntity<>(
new ApiResponse<>(true,"Arrondissement supprimée avec succès."),
HttpStatus.OK
);
}catch (Exception e){
return new ResponseEntity<>(
new ApiResponse<>(false, e.getMessage()),
new ApiResponse<>(true, "Arrondissement supprimée avec succès."),
HttpStatus.OK
);
} catch (HttpClientErrorException.MethodNotAllowed e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK);
} catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException |
FileStorageException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK);
} catch (NullPointerException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK);
}
}
@GetMapping("/all")
public ResponseEntity<?> getAllArrondissementList() {
return new ResponseEntity<>(
new ApiResponse<>(true, arrondissementService.getArrondissementList(), "Liste des arrondissements chargée avec succès."),
HttpStatus.OK
);
try {
return new ResponseEntity<>(
new ApiResponse<>(true, arrondissementService.getArrondissementList(), "Liste des arrondissements chargée avec succès."),
HttpStatus.OK
);
} catch (HttpClientErrorException.MethodNotAllowed e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK);
} catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException |
FileStorageException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK);
} catch (NullPointerException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK);
}
}
@GetMapping("/all-paged")
public ResponseEntity<?> getAllArrondissementPaged(@RequestParam int pageNo, @RequestParam int pageSize) {
Pageable pageable = PageRequest.of(pageNo, pageSize);
return new ResponseEntity<>(
new ApiResponse<>(true, arrondissementService.getArrondissementList(pageable), "Liste des arrondissements chargée avec succès."),
HttpStatus.OK
);
try {
Pageable pageable = PageRequest.of(pageNo, pageSize);
return new ResponseEntity<>(
new ApiResponse<>(true, arrondissementService.getArrondissementList(pageable), "Liste des arrondissements chargée avec succès."),
HttpStatus.OK
);
} catch (HttpClientErrorException.MethodNotAllowed e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK);
} catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException |
FileStorageException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK);
} catch (NullPointerException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK);
}
}
@GetMapping("/id/{id}")
public ResponseEntity<?> getArrondissementById(@PathVariable Long id) {
return new ResponseEntity<>(
new ApiResponse<>(true, arrondissementService.getArrondissementById(id), "Arrondissement trouvé avec succès."),
HttpStatus.OK
);
try {
return new ResponseEntity<>(
new ApiResponse<>(true, arrondissementService.getArrondissementById(id), "Arrondissement trouvé avec succès."),
HttpStatus.OK
);
} catch (HttpClientErrorException.MethodNotAllowed e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK);
} catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException |
FileStorageException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK);
} catch (NullPointerException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK);
}
}
@GetMapping("/commune/{communeId}")
public ResponseEntity<?> getArrondissementByCommune(@PathVariable Long communeId) {
try{
try {
return new ResponseEntity<>(
new ApiResponse<>(true, arrondissementService.getArrondissementByComune(communeId), "Liste des arrondissements par commune chargée avec succès."),
HttpStatus.OK
);
}catch (NotFoundException e){
return new ResponseEntity<>(
new ApiResponse<>(false, e.getMessage()),
HttpStatus.OK
);
} catch (HttpClientErrorException.MethodNotAllowed e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK);
} catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException |
FileStorageException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK);
} catch (NullPointerException e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getLocalizedMessage());
return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK);
}
}
}