Search in sources :

Example 1 with HttpRequestException

use of com.tvd12.ezyhttp.core.exception.HttpRequestException in project ezyhttp by youngmonkeys.

the class BlockingServlet method addDefaultExceptionHandlers.

protected void addDefaultExceptionHandlers() {
    exceptionHandlerManager.addUncaughtExceptionHandler(DeserializeValueException.class, (args, e) -> {
        DeserializeValueException deException = (DeserializeValueException) e;
        Map<String, String> errorData = new HashMap<>();
        errorData.put(deException.getValueName(), "invalid");
        errorData.put("exception", e.getClass().getName());
        return ResponseEntity.create(StatusCodes.BAD_REQUEST, errorData);
    });
    exceptionHandlerManager.addUncaughtExceptionHandler(HttpRequestException.class, (args, e) -> {
        HttpRequestException requestException = (HttpRequestException) e;
        int errorStatus = requestException.getCode();
        Object errorData = requestException.getData();
        if (errorData == null) {
            errorData = Collections.emptyMap();
        }
        return ResponseEntity.create(errorStatus, errorData);
    });
}
Also used : DeserializeValueException(com.tvd12.ezyhttp.core.exception.DeserializeValueException) HashMap(java.util.HashMap) HttpRequestException(com.tvd12.ezyhttp.core.exception.HttpRequestException)

Example 2 with HttpRequestException

use of com.tvd12.ezyhttp.core.exception.HttpRequestException in project ezyhttp by youngmonkeys.

the class HttpRequestExceptionTest method test.

@Test
public void test() {
    // given
    int code = StatusCodes.BAD_REQUEST;
    String data = "error";
    // when
    HttpRequestException sut = new HttpRequestException(code, data);
    // then
    Asserts.assertEquals(code, sut.getCode());
    Asserts.assertEquals(data, sut.getData());
}
Also used : HttpRequestException(com.tvd12.ezyhttp.core.exception.HttpRequestException) Test(org.testng.annotations.Test)

Aggregations

HttpRequestException (com.tvd12.ezyhttp.core.exception.HttpRequestException)2 DeserializeValueException (com.tvd12.ezyhttp.core.exception.DeserializeValueException)1 HashMap (java.util.HashMap)1 Test (org.testng.annotations.Test)1