From 50ca04147a72f35c976d141a4e95c4c0443a694c Mon Sep 17 00:00:00 2001 From: dylan Date: Fri, 31 Jan 2025 11:12:54 +0100 Subject: [PATCH] Catch 2 different types of exception --- .../SigningRequestDocumentController.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/main/java/ch/dlmw/swisssignchallenge/controllers/SigningRequestDocumentController.java b/src/main/java/ch/dlmw/swisssignchallenge/controllers/SigningRequestDocumentController.java index b2fb34d..ee26dc4 100644 --- a/src/main/java/ch/dlmw/swisssignchallenge/controllers/SigningRequestDocumentController.java +++ b/src/main/java/ch/dlmw/swisssignchallenge/controllers/SigningRequestDocumentController.java @@ -23,29 +23,23 @@ public class SigningRequestDocumentController implements SigningRequestDocumentA @Override public ResponseEntity patchSigningRequestDocument(String id, List patchOperations) { - // Fetch the existing document var document = signingRequestDocumentService.getSigningRequestDocument(id); try { - ObjectMapper mapper = new ObjectMapper(); - // Serialize patchOperations to JsonNode - JsonNode patchNode = mapper.valueToTree(patchOperations); + JsonNode patchNode = objectMapper.valueToTree(patchOperations); - // Create JsonPatch from JsonNode JsonPatch jsonPatch = JsonPatch.fromJson(patchNode); - // Apply patch to the document's JsonNode - JsonNode patchedNode = jsonPatch.apply(mapper.convertValue(document, JsonNode.class)); + JsonNode patchedNode = jsonPatch.apply(objectMapper.convertValue(document, JsonNode.class)); - // Convert the patched JsonNode back to SigningRequestDocument - SigningRequestDocument patchedDocument = mapper.treeToValue(patchedNode, SigningRequestDocument.class); + SigningRequestDocument patchedDocument = objectMapper.treeToValue(patchedNode, SigningRequestDocument.class); - // Now, update the document in the service layer signingRequestDocumentService.updateSigningRequestDocument(patchedDocument); return ResponseEntity.noContent().build(); - } catch (IOException | JsonPatchException e) { - // Handle exceptions appropriately + } catch (JsonPatchException e) { + throw new IllegalArgumentException("Failed to apply patch", e); + } catch (IOException e) { throw new RuntimeException("Failed to apply patch", e); } }