From 7f133a4f4838c246402021f6d474711293af613b Mon Sep 17 00:00:00 2001 From: "zxBCN Farre_Basurte,Juan_Antonio (IT EDS) EXTERNAL" Date: Tue, 19 Apr 2022 00:40:28 +0200 Subject: [PATCH] Cleanup of error handling and using 400 and 500 status codes --- .../ods/doc/gen/core/api/HealthController.groovy | 7 +------ .../external/modules/git/BitbucketService.groovy | 15 +++++++++------ .../gen/external/modules/jira/JiraService.groovy | 9 +++++---- .../external/modules/nexus/NexusService.groovy | 6 ++---- .../modules/xunit/JUnitReportsService.groovy | 4 ++-- .../doc/gen/leva/doc/api/LevaDocController.groovy | 12 +++++------- .../gen/leva/doc/services/DocGenService.groovy | 10 +++------- .../gen/pdf/builder/api/DocGenController.groovy | 2 -- .../api/RestResponseEntityExceptionHandler.groovy | 14 +++++++------- 9 files changed, 34 insertions(+), 45 deletions(-) diff --git a/src/main/groovy/org/ods/doc/gen/core/api/HealthController.groovy b/src/main/groovy/org/ods/doc/gen/core/api/HealthController.groovy index 077dcd08..1630db1e 100644 --- a/src/main/groovy/org/ods/doc/gen/core/api/HealthController.groovy +++ b/src/main/groovy/org/ods/doc/gen/core/api/HealthController.groovy @@ -34,25 +34,20 @@ class HealthController { return result } - private byte[] generatePdfData() { + private void generatePdfData() { Path tmpDir = Files.createTempDirectory("generatePdfDataFolderTest") def documentHtmlFile = Files.createTempFile("document", ".html") << "document" - def pdfBytesToString try { def documentPdf = htmlToPDFService.convert(tmpDir, documentHtmlFile) def data = Files.readAllBytes(documentPdf) if (!new String(data).startsWith("%PDF-1.4\n")) { throw new RuntimeException( "Conversion form HTML to PDF failed, corrupt data.") } - pdfBytesToString = data.encodeBase64().toString() - } catch (e) { - throw new RuntimeException( "Conversion form HTML to PDF failed, corrupt data.", e) } finally { Files.delete(documentHtmlFile) FileUtils.deleteDirectory(tmpDir.toFile()) } - return pdfBytesToString } } diff --git a/src/main/groovy/org/ods/doc/gen/external/modules/git/BitbucketService.groovy b/src/main/groovy/org/ods/doc/gen/external/modules/git/BitbucketService.groovy index 55bd1e3b..b4bdc2ea 100644 --- a/src/main/groovy/org/ods/doc/gen/external/modules/git/BitbucketService.groovy +++ b/src/main/groovy/org/ods/doc/gen/external/modules/git/BitbucketService.groovy @@ -67,14 +67,17 @@ class BitbucketService { } protected void downloadRepoWithFallBack(String project, String repo, String branch, Path zipArchive) { - try { - bitBucketClientConfig - .getClient() - .getRepoZipArchive(project, repo, branch) - .withCloseable { Response response -> + boolean found = false; + bitBucketClientConfig + .getClient() + .getRepoZipArchive(project, repo, branch) + .withCloseable { Response response -> + if (response.status() != 404) { streamResult(response, zipArchive) + found = true; } - } catch (Exception callException) { + } + if (!found) { log.warn("Branch [${branch}] doesn't exist, using branch: [${MAIN_BRANCH}]") bitBucketClientConfig .getClient() diff --git a/src/main/groovy/org/ods/doc/gen/external/modules/jira/JiraService.groovy b/src/main/groovy/org/ods/doc/gen/external/modules/jira/JiraService.groovy index 00273f79..af182b27 100644 --- a/src/main/groovy/org/ods/doc/gen/external/modules/jira/JiraService.groovy +++ b/src/main/groovy/org/ods/doc/gen/external/modules/jira/JiraService.groovy @@ -43,10 +43,11 @@ class JiraService { } try { - this.baseURL = new URIBuilder(baseURL).build() - this.targetURL = new URIBuilder(baseURL).build() - } catch (e) { - throw new IllegalArgumentException("Error: unable to connect to Jira. '${baseURL}' is not a valid URI.").initCause(e) + URI jiraURI = new URIBuilder(baseURL).build() + this.baseURL = jiraURI + this.targetURL = jiraURI + } catch (URISyntaxException e) { + throw new IllegalArgumentException("FATAL: Jira URL is invalid: jira.url='${baseURL}'", e) } this.username = username diff --git a/src/main/groovy/org/ods/doc/gen/external/modules/nexus/NexusService.groovy b/src/main/groovy/org/ods/doc/gen/external/modules/nexus/NexusService.groovy index 6e8df944..8d800f38 100644 --- a/src/main/groovy/org/ods/doc/gen/external/modules/nexus/NexusService.groovy +++ b/src/main/groovy/org/ods/doc/gen/external/modules/nexus/NexusService.groovy @@ -34,10 +34,8 @@ class NexusService { checkParams(baseURL, username, password) try { this.baseURL = new URIBuilder(baseURL).build() - } catch (e) { - throw new IllegalArgumentException( - "Error: unable to connect to Nexus. '${baseURL}' is not a valid URI." - ).initCause(e) + } catch (URISyntaxException e) { + throw new IllegalArgumentException("FATAL: Nexus URL is invalid: nexus.url=${baseURL}", e) } this.username = username diff --git a/src/main/groovy/org/ods/doc/gen/external/modules/xunit/JUnitReportsService.groovy b/src/main/groovy/org/ods/doc/gen/external/modules/xunit/JUnitReportsService.groovy index 22d5b332..d8482cc4 100644 --- a/src/main/groovy/org/ods/doc/gen/external/modules/xunit/JUnitReportsService.groovy +++ b/src/main/groovy/org/ods/doc/gen/external/modules/xunit/JUnitReportsService.groovy @@ -59,7 +59,7 @@ class JUnitReportsService { tests.each { String targetFolder = it.value.targetFolder as String - Map testResult = getTestResults(it.key.capitalize(), targetFolder, component) + Map testResult = getTestResults(targetFolder) it.value.testReportFiles = testResult.testReportFiles it.value.testResults = testResult.testResults } @@ -104,7 +104,7 @@ class JUnitReportsService { return testData } - private Map getTestResults(String typeIn, String targetFolder, String component) { + private Map getTestResults(String targetFolder) { if (targetFolder == null) { // When targetFolder is null, we need to initialize testsuites !! return [ diff --git a/src/main/groovy/org/ods/doc/gen/leva/doc/api/LevaDocController.groovy b/src/main/groovy/org/ods/doc/gen/leva/doc/api/LevaDocController.groovy index c2a380db..4b06b9ea 100644 --- a/src/main/groovy/org/ods/doc/gen/leva/doc/api/LevaDocController.groovy +++ b/src/main/groovy/org/ods/doc/gen/leva/doc/api/LevaDocController.groovy @@ -70,14 +70,12 @@ class LevaDocController { try { prepareServiceDataParam(projectId, buildNumber, levaDocType, data) return levaDocType.buildDocument.apply(leVADocumentService, data) - } catch (Throwable e) { - String msg = "Error building document: ${levaDocType} with data:${data}" - log.error(msg, e) - String msgWithDetails = msg; - if (! StringUtils.isEmpty(e.getMessage())) { - msgWithDetails = msg + ". " + e.getMessage(); + } catch (e) { + if (log.isDebugEnabled()) { + String msg = "Error building document: ${levaDocType} with data:${data}" + log.debug(msg, e) } - throw new RuntimeException(msgWithDetails, e) + throw e } } diff --git a/src/main/groovy/org/ods/doc/gen/leva/doc/services/DocGenService.groovy b/src/main/groovy/org/ods/doc/gen/leva/doc/services/DocGenService.groovy index ac33e845..9759bb3e 100644 --- a/src/main/groovy/org/ods/doc/gen/leva/doc/services/DocGenService.groovy +++ b/src/main/groovy/org/ods/doc/gen/leva/doc/services/DocGenService.groovy @@ -35,13 +35,9 @@ class DocGenService { Path tmpDir Path documentPdf def bytes - try { - tmpDir = Files.createTempDirectory("${body.metadata.type}-v${body.metadata.version}") - documentPdf = pdfGenerationService.generatePdfFile(body.metadata as Map, body.data as Map, tmpDir) - bytes = Files.readAllBytes(documentPdf) - } catch (Throwable e) { - throw new RuntimeException( "Conversion form HTML to PDF failed, corrupt data.", e) - } + tmpDir = Files.createTempDirectory("${body.metadata.type}-v${body.metadata.version}") + documentPdf = pdfGenerationService.generatePdfFile(body.metadata as Map, body.data as Map, tmpDir) + bytes = Files.readAllBytes(documentPdf) return bytes } diff --git a/src/main/groovy/org/ods/doc/gen/pdf/builder/api/DocGenController.groovy b/src/main/groovy/org/ods/doc/gen/pdf/builder/api/DocGenController.groovy index a979080f..2e6e94e0 100644 --- a/src/main/groovy/org/ods/doc/gen/pdf/builder/api/DocGenController.groovy +++ b/src/main/groovy/org/ods/doc/gen/pdf/builder/api/DocGenController.groovy @@ -45,8 +45,6 @@ class DocGenController { tmpDir = fileSystemHelper.createTempDirectory("${body.metadata.type}-v${body.metadata.version}") Path documentPdf = pdfGeneration.generatePdfFile(body.metadata as Map, body.data as Map, tmpDir) pdfBytesToString = Files.readAllBytes(documentPdf).encodeBase64().toString() - } catch (Throwable e) { - throw new RuntimeException( "Conversion form HTML to PDF failed, corrupt data.", e) } finally { if (tmpDir) { FileUtils.deleteDirectory(tmpDir.toFile()) diff --git a/src/main/groovy/org/ods/doc/gen/pdf/builder/api/RestResponseEntityExceptionHandler.groovy b/src/main/groovy/org/ods/doc/gen/pdf/builder/api/RestResponseEntityExceptionHandler.groovy index e3d7ebf3..2e647ba4 100644 --- a/src/main/groovy/org/ods/doc/gen/pdf/builder/api/RestResponseEntityExceptionHandler.groovy +++ b/src/main/groovy/org/ods/doc/gen/pdf/builder/api/RestResponseEntityExceptionHandler.groovy @@ -13,16 +13,16 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep @ControllerAdvice class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { - @ExceptionHandler(value = [ IllegalArgumentException.class ]) - protected ResponseEntity handleArgumentError(RuntimeException ex, WebRequest request) { - log.error("ExceptionHandler, handleArgumentError:${ex.message}", ex) - return handleExceptionInternal(ex, ex.message, new HttpHeaders(), HttpStatus.PRECONDITION_FAILED, request) + @ExceptionHandler(value = IllegalArgumentException.class) + protected ResponseEntity handleArgumentError(IllegalArgumentException ex, WebRequest request) { + log.error("Bad request: ", ex) + return handleExceptionInternal(ex, ex.message, new HttpHeaders(), HttpStatus.BAD_REQUEST, request) } - @ExceptionHandler(value = [ RuntimeException.class ]) + @ExceptionHandler(value = RuntimeException.class) protected ResponseEntity runtimeError(RuntimeException ex, WebRequest request) { - log.error("ExceptionHandler, runtimeError:${ex.message}", ex) - return handleExceptionInternal(ex, ex.message, new HttpHeaders(), HttpStatus.CONFLICT, request) + log.error("Internal server error: ", ex) + return handleExceptionInternal(ex, ex.message, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request) } } \ No newline at end of file