ReaderExceptionMapper extends CandlepinExceptionMapper implements ExceptionMapper<ReaderException> { @Override public Response toResponse(ReaderException exception) { Status status = null; if (exception.getResponse() != null) { status = Response.Status.fromStatusCode(exception.getResponse().getStatus()); } for (Throwable cause = exception.getCause(); cause != null; cause = cause.getCause()) { if (cause instanceof JsonMappingException) { status = Response.Status.BAD_REQUEST; break; } } if (status == null) { status = Response.Status.INTERNAL_SERVER_ERROR; } return this.getDefaultBuilder(exception, status, determineBestMediaType()).build(); }  @Override Response toResponse(ReaderException exception);  }
@Test public void handleExceptionWithoutResponse() { ReaderException nfe = new ReaderException("unacceptable"); ReaderExceptionMapper nfem = injector.getInstance(ReaderExceptionMapper.class); Response r = nfem.toResponse(nfe); assertEquals(500, r.getStatus()); verifyMessage(r, rtmsg("unacceptable")); }
@Test public void handleExceptionWithResponse() { Response mockr = mock(Response.class); when(mockr.getStatus()).thenReturn(400); ReaderException nfe = new ReaderException("unacceptable", mockr); ReaderExceptionMapper nfem = injector.getInstance(ReaderExceptionMapper.class); Response r = nfem.toResponse(nfe); assertEquals(400, r.getStatus()); verifyMessage(r, rtmsg("unacceptable")); }
@Test public void handleJsonMappingExceptionWithResponse() { Response mockr = mock(Response.class); when(mockr.getStatus()).thenReturn(400); ReaderException nfe = new ReaderException("kaboom", new JsonMappingException("nope")); ReaderExceptionMapper nfem = injector.getInstance(ReaderExceptionMapper.class); Response r = nfem.toResponse(nfe); assertEquals(400, r.getStatus()); verifyMessage(r, rtmsg("kaboom")); }