@Test void canHandleGenieExceptions() { final List<GenieException> exceptions = Arrays.asList( new GenieBadRequestException("bad"), new GenieNotFoundException("Not Found"), new GeniePreconditionException("Precondition"), new GenieServerException("server"), new GenieServerUnavailableException("Server Unavailable"), new GenieTimeoutException("Timeout"), new GenieException(568, "Other") ); for (final GenieException exception : exceptions) { final ResponseEntity<GenieException> response = this.mapper.handleGenieException(exception); final HttpStatus expectedStatus = HttpStatus.resolve(exception.getErrorCode()) != null ? HttpStatus.resolve(exception.getErrorCode()) : HttpStatus.INTERNAL_SERVER_ERROR; Assertions.assertThat(response.getStatusCode()).isEqualByComparingTo(expectedStatus); Mockito .verify(this.registry, Mockito.times(1)) .counter( GenieExceptionMapper.CONTROLLER_EXCEPTION_COUNTER_NAME, Sets.newHashSet( Tag.of(MetricsConstants.TagKeys.EXCEPTION_CLASS, exception.getClass().getCanonicalName()) ) ); } Mockito .verify(this.counter, Mockito.times(exceptions.size())) .increment(); }
@ExceptionHandler(GenieException.class) public ResponseEntity<GenieException> handleGenieException(final GenieException e) { this.countExceptionAndLog(e); HttpStatus status = HttpStatus.resolve(e.getErrorCode()); if (status == null) { status = HttpStatus.INTERNAL_SERVER_ERROR; } return new ResponseEntity<>(e, status); }
GenieExceptionMapper { @ExceptionHandler(GenieException.class) public ResponseEntity<GenieException> handleGenieException(final GenieException e) { this.countExceptionAndLog(e); HttpStatus status = HttpStatus.resolve(e.getErrorCode()); if (status == null) { status = HttpStatus.INTERNAL_SERVER_ERROR; } return new ResponseEntity<>(e, status); } }
GenieExceptionMapper { @ExceptionHandler(GenieException.class) public ResponseEntity<GenieException> handleGenieException(final GenieException e) { this.countExceptionAndLog(e); HttpStatus status = HttpStatus.resolve(e.getErrorCode()); if (status == null) { status = HttpStatus.INTERNAL_SERVER_ERROR; } return new ResponseEntity<>(e, status); } @Autowired  GenieExceptionMapper(final MeterRegistry registry); }
GenieExceptionMapper { @ExceptionHandler(GenieException.class) public ResponseEntity<GenieException> handleGenieException(final GenieException e) { this.countExceptionAndLog(e); HttpStatus status = HttpStatus.resolve(e.getErrorCode()); if (status == null) { status = HttpStatus.INTERNAL_SERVER_ERROR; } return new ResponseEntity<>(e, status); } @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(GenieException.class) public ResponseEntity<GenieException> handleGenieException(final GenieException e) { this.countExceptionAndLog(e); HttpStatus status = HttpStatus.resolve(e.getErrorCode()); if (status == null) { status = HttpStatus.INTERNAL_SERVER_ERROR; } return new ResponseEntity<>(e, status); } @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
    );  }