@Test void testCreateExceptionResponseErrorCoded() { CodedRuntimeException exception = mock(CodedRuntimeException.class); when(exception.getErrorCode()).thenReturn("A1"); when(exception.getLocalizedMessage()).thenReturn("message always exposed"); HttpStatus httpStatus = HttpStatus.BAD_REQUEST; Problem problem = Problem.builder() .setType(URI.create("http: .setTitle("Bad Request") .setStatus(400) .setDetail("message always exposed") .setErrorCode("A1") .build(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_PROBLEM_JSON); ResponseEntity<Problem> responseEntity = new ResponseEntity<>(problem, httpHeaders, httpStatus); assertEquals( responseEntity, problemExceptionResponseGenerator.createExceptionResponse(exception, httpStatus, false)); }
@Override public ResponseEntity<Problem> createExceptionResponse( Exception exception, HttpStatus httpStatus, boolean isDevEnvironment) { Builder builder = Problem.builder() .setType(getProblemType(exception)) .setStatus(httpStatus.value()) .setTitle(httpStatus.getReasonPhrase()); ExceptionUtils.getErrorCode(exception) .ifPresent( errorCode -> builder.setErrorCode(errorCode).setDetail(exception.getLocalizedMessage())); ExceptionUtils.getErrors(exception) .ifPresent(errors -> builder.setErrors(createProblemErrors(errors))); if (isDevEnvironment) { builder.setDetail(exception.getLocalizedMessage()).setStackTrace(getStackTrace(exception)); } HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_PROBLEM_JSON); return new ResponseEntity<>(builder.build(), httpHeaders, httpStatus); }
ProblemExceptionResponseGenerator implements ExceptionResponseGenerator<ResponseEntity<Problem>> { @Override public ResponseEntity<Problem> createExceptionResponse( Exception exception, HttpStatus httpStatus, boolean isDevEnvironment) { Builder builder = Problem.builder() .setType(getProblemType(exception)) .setStatus(httpStatus.value()) .setTitle(httpStatus.getReasonPhrase()); ExceptionUtils.getErrorCode(exception) .ifPresent( errorCode -> builder.setErrorCode(errorCode).setDetail(exception.getLocalizedMessage())); ExceptionUtils.getErrors(exception) .ifPresent(errors -> builder.setErrors(createProblemErrors(errors))); if (isDevEnvironment) { builder.setDetail(exception.getLocalizedMessage()).setStackTrace(getStackTrace(exception)); } HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_PROBLEM_JSON); return new ResponseEntity<>(builder.build(), httpHeaders, httpStatus); } }
ProblemExceptionResponseGenerator implements ExceptionResponseGenerator<ResponseEntity<Problem>> { @Override public ResponseEntity<Problem> createExceptionResponse( Exception exception, HttpStatus httpStatus, boolean isDevEnvironment) { Builder builder = Problem.builder() .setType(getProblemType(exception)) .setStatus(httpStatus.value()) .setTitle(httpStatus.getReasonPhrase()); ExceptionUtils.getErrorCode(exception) .ifPresent( errorCode -> builder.setErrorCode(errorCode).setDetail(exception.getLocalizedMessage())); ExceptionUtils.getErrors(exception) .ifPresent(errors -> builder.setErrors(createProblemErrors(errors))); if (isDevEnvironment) { builder.setDetail(exception.getLocalizedMessage()).setStackTrace(getStackTrace(exception)); } HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_PROBLEM_JSON); return new ResponseEntity<>(builder.build(), httpHeaders, httpStatus); } ProblemExceptionResponseGenerator(ContextMessageSource contextMessageSource); }
ProblemExceptionResponseGenerator implements ExceptionResponseGenerator<ResponseEntity<Problem>> { @Override public ResponseEntity<Problem> createExceptionResponse( Exception exception, HttpStatus httpStatus, boolean isDevEnvironment) { Builder builder = Problem.builder() .setType(getProblemType(exception)) .setStatus(httpStatus.value()) .setTitle(httpStatus.getReasonPhrase()); ExceptionUtils.getErrorCode(exception) .ifPresent( errorCode -> builder.setErrorCode(errorCode).setDetail(exception.getLocalizedMessage())); ExceptionUtils.getErrors(exception) .ifPresent(errors -> builder.setErrors(createProblemErrors(errors))); if (isDevEnvironment) { builder.setDetail(exception.getLocalizedMessage()).setStackTrace(getStackTrace(exception)); } HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_PROBLEM_JSON); return new ResponseEntity<>(builder.build(), httpHeaders, httpStatus); } ProblemExceptionResponseGenerator(ContextMessageSource contextMessageSource); @Override ExceptionResponseType getType(); @Override ResponseEntity<Problem> createExceptionResponse(
      Exception exception, HttpStatus httpStatus, boolean isDevEnvironment); }
ProblemExceptionResponseGenerator implements ExceptionResponseGenerator<ResponseEntity<Problem>> { @Override public ResponseEntity<Problem> createExceptionResponse( Exception exception, HttpStatus httpStatus, boolean isDevEnvironment) { Builder builder = Problem.builder() .setType(getProblemType(exception)) .setStatus(httpStatus.value()) .setTitle(httpStatus.getReasonPhrase()); ExceptionUtils.getErrorCode(exception) .ifPresent( errorCode -> builder.setErrorCode(errorCode).setDetail(exception.getLocalizedMessage())); ExceptionUtils.getErrors(exception) .ifPresent(errors -> builder.setErrors(createProblemErrors(errors))); if (isDevEnvironment) { builder.setDetail(exception.getLocalizedMessage()).setStackTrace(getStackTrace(exception)); } HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_PROBLEM_JSON); return new ResponseEntity<>(builder.build(), httpHeaders, httpStatus); } ProblemExceptionResponseGenerator(ContextMessageSource contextMessageSource); @Override ExceptionResponseType getType(); @Override ResponseEntity<Problem> createExceptionResponse(
      Exception exception, HttpStatus httpStatus, boolean isDevEnvironment);  }