From 5df1950a37bae01415db17baa14b94c88ec754ae Mon Sep 17 00:00:00 2001 From: emanipravallika Date: Wed, 16 Oct 2024 14:27:46 +0530 Subject: [PATCH] Added common utility method to handle the response map construction. --- .../exception/RetrofitErrorHandler.groovy | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/gate-web/src/main/groovy/com/opsmx/spinnaker/gate/exception/RetrofitErrorHandler.groovy b/gate-web/src/main/groovy/com/opsmx/spinnaker/gate/exception/RetrofitErrorHandler.groovy index 11654bbeb8..3ccdf6d00e 100644 --- a/gate-web/src/main/groovy/com/opsmx/spinnaker/gate/exception/RetrofitErrorHandler.groovy +++ b/gate-web/src/main/groovy/com/opsmx/spinnaker/gate/exception/RetrofitErrorHandler.groovy @@ -78,34 +78,32 @@ class RetrofitErrorHandler { @ExceptionHandler(PipelineController.PipelineException) @ResponseBody ResponseEntity> handlePipelineException(PipelineController.PipelineException ex) { - Map response = [:] - response.put("error", "Pipeline Save Error") - response.put("message", ex.message) - response.put("status", HttpStatus.BAD_REQUEST.value()) - response.put("timestamp", System.currentTimeMillis()) - log.error("Pipeline Exception occurred: {}", ex.getMessage(), ex); + log.error("PipelineException occurred: {}", ex.message, ex) + Map response = createResponseMap(ex, "Pipeline Save Error") if (ex.additionalAttributes) { response.put("additionalAttributes", ex.additionalAttributes) } - - log.error("PipelineException occurred: {}", ex.message, ex) return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST) } - @ExceptionHandler(OesRequestException) @ResponseBody ResponseEntity> handleOesRequestException(OesRequestException ex) { + log.error("OesRequestException occurred: {}", ex.message, ex) + Map response = createResponseMap(ex, "OES Request Exception") + return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST) + } + + private Map createResponseMap(Exception ex, String errorMessage) { Map response = [:] - response.put("error", "OES Request Exception") + response.put("error", errorMessage) response.put("message", ex.message) response.put("status", HttpStatus.BAD_REQUEST.value()) response.put("timestamp", System.currentTimeMillis()) - log.error("OesRequest Exception occurred: {}", ex.getMessage(), ex); - return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); - } + return response + } private ErrorResponseModel populateDefaultErrorResponseModel() {