From 0b54932080cc8680d2fd718bd41b1004e6f8bbb9 Mon Sep 17 00:00:00 2001 From: tillias Date: Mon, 26 Oct 2020 21:24:04 +0100 Subject: [PATCH] SonarCloud bugfix #54 --- .../microcatalog/config/WebConfigurer.java | 3 ++- .../web/rest/errors/ExceptionTranslator.java | 21 ++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/github/microcatalog/config/WebConfigurer.java b/src/main/java/com/github/microcatalog/config/WebConfigurer.java index 6de3380..8cc3434 100644 --- a/src/main/java/com/github/microcatalog/config/WebConfigurer.java +++ b/src/main/java/com/github/microcatalog/config/WebConfigurer.java @@ -114,7 +114,8 @@ private String resolvePathPrefix() { public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = jHipsterProperties.getCors(); - if (config.getAllowedOrigins() != null && !config.getAllowedOrigins().isEmpty()) { + List allowedOrigins = config.getAllowedOrigins(); + if (allowedOrigins != null && !allowedOrigins.isEmpty()) { log.debug("Registering CORS filter"); source.registerCorsConfiguration("/api/**", config); source.registerCorsConfiguration("/management/**", config); diff --git a/src/main/java/com/github/microcatalog/web/rest/errors/ExceptionTranslator.java b/src/main/java/com/github/microcatalog/web/rest/errors/ExceptionTranslator.java index a4a42bf..78eaaa8 100644 --- a/src/main/java/com/github/microcatalog/web/rest/errors/ExceptionTranslator.java +++ b/src/main/java/com/github/microcatalog/web/rest/errors/ExceptionTranslator.java @@ -67,11 +67,18 @@ public ResponseEntity process(@Nullable ResponseEntity entity, if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) { return entity; } + ProblemBuilder builder = Problem.builder() .withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType()) .withStatus(problem.getStatus()) - .withTitle(problem.getTitle()) - .with(PATH_KEY, request.getNativeRequest(HttpServletRequest.class).getRequestURI()); + .withTitle(problem.getTitle()); + + HttpServletRequest nativeRequest = request.getNativeRequest(HttpServletRequest.class); + if (nativeRequest != null) { + builder.with(PATH_KEY, nativeRequest.getRequestURI()); + } else { + builder.with(PATH_KEY, "Error: unknown URI"); + } if (problem instanceof ConstraintViolationProblem) { builder @@ -110,13 +117,13 @@ public ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotVal @ExceptionHandler public ResponseEntity handleEmailAlreadyUsedException(com.github.microcatalog.service.EmailAlreadyUsedException ex, NativeWebRequest request) { EmailAlreadyUsedException problem = new EmailAlreadyUsedException(); - return create(problem, request, HeaderUtil.createFailureAlert(applicationName, true, problem.getEntityName(), problem.getErrorKey(), problem.getMessage())); + return create(problem, request, HeaderUtil.createFailureAlert(applicationName, true, problem.getEntityName(), problem.getErrorKey(), problem.getMessage())); } @ExceptionHandler public ResponseEntity handleUsernameAlreadyUsedException(com.github.microcatalog.service.UsernameAlreadyUsedException ex, NativeWebRequest request) { LoginAlreadyUsedException problem = new LoginAlreadyUsedException(); - return create(problem, request, HeaderUtil.createFailureAlert(applicationName, true, problem.getEntityName(), problem.getErrorKey(), problem.getMessage())); + return create(problem, request, HeaderUtil.createFailureAlert(applicationName, true, problem.getEntityName(), problem.getErrorKey(), problem.getMessage())); } @ExceptionHandler @@ -140,7 +147,7 @@ public ResponseEntity handleConcurrencyFailure(ConcurrencyFailureExcept @Override public ProblemBuilder prepare(final Throwable throwable, final StatusType status, final URI type) { - + Collection activeProfiles = Arrays.asList(env.getActiveProfiles()); if (activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_PRODUCTION)) { @@ -155,7 +162,7 @@ public ProblemBuilder prepare(final Throwable throwable, final StatusType status .map(this::toProblem) .orElse(null)); } - + if (throwable instanceof DataAccessException) { return Problem.builder() .withType(type) @@ -167,7 +174,7 @@ public ProblemBuilder prepare(final Throwable throwable, final StatusType status .map(this::toProblem) .orElse(null)); } - + if (containsPackageName(throwable.getMessage())) { return Problem.builder() .withType(type)