Search in sources :

Example 1 with RemoteServiceException

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

the class WebServiceClientTest method validateResponseWithErrorResponse.

@Test
void validateResponseWithErrorResponse() {
    ErrorResponse response = new ErrorResponse();
    response.severity = "WARN";
    response.errorCode = "NOT_FOUND";
    response.message = "not found";
    RemoteServiceException exception = assertThrows(RemoteServiceException.class, () -> webServiceClient.validateResponse(new HTTPResponse(HTTPStatus.NOT_FOUND, Maps.newHashMap(), Strings.bytes(JSON.toJSON(response)))));
    assertEquals(Severity.WARN, exception.severity());
    assertEquals(response.errorCode, exception.errorCode());
    assertEquals(response.message, exception.getMessage());
}
Also used : RemoteServiceException(core.framework.web.service.RemoteServiceException) HTTPResponse(core.framework.http.HTTPResponse) Test(org.junit.jupiter.api.Test)

Example 2 with RemoteServiceException

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

the class WebServiceClient method validateResponse.

void validateResponse(HTTPResponse response) {
    HTTPStatus status = response.status();
    if (status.code >= 200 && status.code < 300)
        return;
    byte[] responseBody = response.body();
    try {
        ErrorResponse error = JSONMapper.fromJSON(ErrorResponse.class, responseBody);
        logger.debug("failed to call remote service, id={}, severity={}, errorCode={}, remoteStackTrace={}", error.id, error.severity, error.errorCode, error.stackTrace);
        throw new RemoteServiceException(error.message, parseSeverity(error.severity), error.errorCode);
    } catch (RemoteServiceException e) {
        throw e;
    } catch (Throwable e) {
        String responseText = response.text();
        logger.warn("failed to decode response, statusCode={}, responseText={}", status.code, responseText, e);
        throw new RemoteServiceException(Strings.format("internal communication failed, status={}, responseText={}", status.code, responseText), Severity.ERROR, "REMOTE_SERVICE_ERROR", e);
    }
}
Also used : RemoteServiceException(core.framework.web.service.RemoteServiceException) HTTPStatus(core.framework.api.http.HTTPStatus)

Aggregations

RemoteServiceException (core.framework.web.service.RemoteServiceException)2 HTTPStatus (core.framework.api.http.HTTPStatus)1 HTTPResponse (core.framework.http.HTTPResponse)1 Test (org.junit.jupiter.api.Test)1