Search in sources :

Example 1 with ErrorResponse

use of core.framework.impl.web.service.ErrorResponse in project core-ng-project by neowu.

the class HTTPServerErrorHandler method handleError.

void handleError(Throwable e, HttpServerExchange exchange, RequestImpl request, ActionLog actionLog) {
    if (exchange.isResponseStarted()) {
        logger.error("response was sent, discard the current http transaction");
        return;
    }
    try {
        Response errorResponse = null;
        if (customErrorHandler != null) {
            Optional<Response> customErrorResponse = customErrorHandler.handle(request, e);
            if (customErrorResponse.isPresent())
                errorResponse = customErrorResponse.get();
        }
        if (errorResponse == null) {
            String accept = exchange.getRequestHeaders().getFirst(Headers.ACCEPT);
            errorResponse = defaultErrorResponse(e, accept, actionLog);
        }
        responseHandler.render((ResponseImpl) errorResponse, exchange, actionLog);
    } catch (Throwable error) {
        logger.error(error.getMessage(), e);
        if (exchange.isResponseStarted()) {
            logger.error("failed to render error page, response was sent, discard the current http transaction");
            return;
        }
        renderDefaultErrorPage(error, exchange, actionLog);
    }
}
Also used : ErrorResponse(core.framework.impl.web.service.ErrorResponse) Response(core.framework.web.Response)

Example 2 with ErrorResponse

use of core.framework.impl.web.service.ErrorResponse in project core-ng-project by neowu.

the class HTTPServerErrorHandlerTest method errorResponse.

@Test
void errorResponse() {
    ErrorResponse response = handler.errorResponse(new Error("test-message"), null);
    assertEquals("test-message", response.message);
    assertEquals("INTERNAL_ERROR", response.errorCode);
    assertEquals("ERROR", response.severity);
}
Also used : ErrorResponse(core.framework.impl.web.service.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 3 with ErrorResponse

use of core.framework.impl.web.service.ErrorResponse in project core-ng-project by neowu.

the class HTTPServerErrorHandlerTest method errorResponseWithErrorCodeException.

@Test
void errorResponseWithErrorCodeException() {
    ErrorResponse response = handler.errorResponse(new NotFoundException("test-message", "TEST_ERROR_CODE"), null);
    assertEquals("test-message", response.message);
    assertEquals("TEST_ERROR_CODE", response.errorCode);
    assertEquals("WARN", response.severity);
}
Also used : NotFoundException(core.framework.web.exception.NotFoundException) ErrorResponse(core.framework.impl.web.service.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 4 with ErrorResponse

use of core.framework.impl.web.service.ErrorResponse in project core-ng-project by neowu.

the class HTTPServerErrorHandler method errorResponse.

ErrorResponse errorResponse(Throwable e, String actionId) {
    ErrorResponse response = new ErrorResponse();
    response.id = actionId;
    response.message = e.getMessage();
    response.stackTrace = Exceptions.stackTrace(e);
    if (e instanceof ErrorCode) {
        ErrorCode errorCode = (ErrorCode) e;
        response.errorCode = errorCode.errorCode();
        response.severity = errorCode.severity().name();
    } else {
        response.errorCode = "INTERNAL_ERROR";
        response.severity = Severity.ERROR.name();
    }
    return response;
}
Also used : ErrorCode(core.framework.log.ErrorCode) ErrorResponse(core.framework.impl.web.service.ErrorResponse)

Aggregations

ErrorResponse (core.framework.impl.web.service.ErrorResponse)4 Test (org.junit.jupiter.api.Test)2 ErrorCode (core.framework.log.ErrorCode)1 Response (core.framework.web.Response)1 NotFoundException (core.framework.web.exception.NotFoundException)1