@Test void canHandleGenieCheckedExceptions() { final Map<GenieCheckedException, HttpStatus> exceptions = Maps.newHashMap(); exceptions.put(new GenieJobResolutionException(), HttpStatus.PRECONDITION_FAILED); exceptions.put(new JobArchiveException(), HttpStatus.INTERNAL_SERVER_ERROR); exceptions.put(new GenieConversionException(), HttpStatus.INTERNAL_SERVER_ERROR); exceptions.put(new GenieCheckedException(), HttpStatus.INTERNAL_SERVER_ERROR); exceptions.put(new IdAlreadyExistsException(), HttpStatus.CONFLICT); exceptions.put(new JobArchiveException(), HttpStatus.INTERNAL_SERVER_ERROR); exceptions.put(new SaveAttachmentException(), HttpStatus.INTERNAL_SERVER_ERROR); exceptions.put(new JobNotFoundException(), HttpStatus.NOT_FOUND); exceptions.put(new NotFoundException(), HttpStatus.NOT_FOUND); exceptions.put(new PreconditionFailedException(), HttpStatus.BAD_REQUEST); exceptions.put(new AttachmentTooLargeException(), HttpStatus.PAYLOAD_TOO_LARGE); for (final Map.Entry<GenieCheckedException, HttpStatus> exception : exceptions.entrySet()) { final ResponseEntity<GenieCheckedException> response = this.mapper.handleGenieCheckedException(exception.getKey()); Assertions.assertThat(response.getStatusCode()).isEqualByComparingTo(exception.getValue()); } }
@ExceptionHandler(GenieCheckedException.class) public ResponseEntity<GenieCheckedException> handleGenieCheckedException(final GenieCheckedException e) { this.countExceptionAndLog(e); if (e instanceof GenieJobResolutionException) { return new ResponseEntity<>(e, HttpStatus.PRECONDITION_FAILED); } else if (e instanceof IdAlreadyExistsException) { return new ResponseEntity<>(e, HttpStatus.CONFLICT); } else if (e instanceof JobNotFoundException | e instanceof NotFoundException) { return new ResponseEntity<>(e, HttpStatus.NOT_FOUND); } else if (e instanceof PreconditionFailedException) { return new ResponseEntity<>(e, HttpStatus.BAD_REQUEST); } else if (e instanceof AttachmentTooLargeException) { return new ResponseEntity<>(e, HttpStatus.PAYLOAD_TOO_LARGE); } else { return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR); } }
GenieExceptionMapper { @ExceptionHandler(GenieCheckedException.class) public ResponseEntity<GenieCheckedException> handleGenieCheckedException(final GenieCheckedException e) { this.countExceptionAndLog(e); if (e instanceof GenieJobResolutionException) { return new ResponseEntity<>(e, HttpStatus.PRECONDITION_FAILED); } else if (e instanceof IdAlreadyExistsException) { return new ResponseEntity<>(e, HttpStatus.CONFLICT); } else if (e instanceof JobNotFoundException | e instanceof NotFoundException) { return new ResponseEntity<>(e, HttpStatus.NOT_FOUND); } else if (e instanceof PreconditionFailedException) { return new ResponseEntity<>(e, HttpStatus.BAD_REQUEST); } else if (e instanceof AttachmentTooLargeException) { return new ResponseEntity<>(e, HttpStatus.PAYLOAD_TOO_LARGE); } else { return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR); } } }
GenieExceptionMapper { @ExceptionHandler(GenieCheckedException.class) public ResponseEntity<GenieCheckedException> handleGenieCheckedException(final GenieCheckedException e) { this.countExceptionAndLog(e); if (e instanceof GenieJobResolutionException) { return new ResponseEntity<>(e, HttpStatus.PRECONDITION_FAILED); } else if (e instanceof IdAlreadyExistsException) { return new ResponseEntity<>(e, HttpStatus.CONFLICT); } else if (e instanceof JobNotFoundException | e instanceof NotFoundException) { return new ResponseEntity<>(e, HttpStatus.NOT_FOUND); } else if (e instanceof PreconditionFailedException) { return new ResponseEntity<>(e, HttpStatus.BAD_REQUEST); } else if (e instanceof AttachmentTooLargeException) { return new ResponseEntity<>(e, HttpStatus.PAYLOAD_TOO_LARGE); } else { return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR); } } @Autowired  GenieExceptionMapper(final MeterRegistry registry); }
GenieExceptionMapper { @ExceptionHandler(GenieCheckedException.class) public ResponseEntity<GenieCheckedException> handleGenieCheckedException(final GenieCheckedException e) { this.countExceptionAndLog(e); if (e instanceof GenieJobResolutionException) { return new ResponseEntity<>(e, HttpStatus.PRECONDITION_FAILED); } else if (e instanceof IdAlreadyExistsException) { return new ResponseEntity<>(e, HttpStatus.CONFLICT); } else if (e instanceof JobNotFoundException | e instanceof NotFoundException) { return new ResponseEntity<>(e, HttpStatus.NOT_FOUND); } else if (e instanceof PreconditionFailedException) { return new ResponseEntity<>(e, HttpStatus.BAD_REQUEST); } else if (e instanceof AttachmentTooLargeException) { return new ResponseEntity<>(e, HttpStatus.PAYLOAD_TOO_LARGE); } else { return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR); } } @Autowired  GenieExceptionMapper(final MeterRegistry registry); @ExceptionHandler(GenieException.class) ResponseEntity<GenieException> handleGenieException(final GenieException e); @ExceptionHandler(GenieRuntimeException.class) ResponseEntity<GenieRuntimeException> handleGenieRuntimeException(final GenieRuntimeException e); @ExceptionHandler(GenieCheckedException.class) ResponseEntity<GenieCheckedException> handleGenieCheckedException(final GenieCheckedException e); @ExceptionHandler(ConstraintViolationException.class) ResponseEntity<GeniePreconditionException> handleConstraintViolation(
        final ConstraintViolationException cve
    ); @ExceptionHandler(MethodArgumentNotValidException.class) ResponseEntity<GeniePreconditionException> handleMethodArgumentNotValidException(
        final MethodArgumentNotValidException e
    ); }
GenieExceptionMapper { @ExceptionHandler(GenieCheckedException.class) public ResponseEntity<GenieCheckedException> handleGenieCheckedException(final GenieCheckedException e) { this.countExceptionAndLog(e); if (e instanceof GenieJobResolutionException) { return new ResponseEntity<>(e, HttpStatus.PRECONDITION_FAILED); } else if (e instanceof IdAlreadyExistsException) { return new ResponseEntity<>(e, HttpStatus.CONFLICT); } else if (e instanceof JobNotFoundException | e instanceof NotFoundException) { return new ResponseEntity<>(e, HttpStatus.NOT_FOUND); } else if (e instanceof PreconditionFailedException) { return new ResponseEntity<>(e, HttpStatus.BAD_REQUEST); } else if (e instanceof AttachmentTooLargeException) { return new ResponseEntity<>(e, HttpStatus.PAYLOAD_TOO_LARGE); } else { return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR); } } @Autowired  GenieExceptionMapper(final MeterRegistry registry); @ExceptionHandler(GenieException.class) ResponseEntity<GenieException> handleGenieException(final GenieException e); @ExceptionHandler(GenieRuntimeException.class) ResponseEntity<GenieRuntimeException> handleGenieRuntimeException(final GenieRuntimeException e); @ExceptionHandler(GenieCheckedException.class) ResponseEntity<GenieCheckedException> handleGenieCheckedException(final GenieCheckedException e); @ExceptionHandler(ConstraintViolationException.class) ResponseEntity<GeniePreconditionException> handleConstraintViolation(
        final ConstraintViolationException cve
    ); @ExceptionHandler(MethodArgumentNotValidException.class) ResponseEntity<GeniePreconditionException> handleMethodArgumentNotValidException(
        final MethodArgumentNotValidException e
    );  }