Search in sources :

Example 1 with ErrorDetails

use of com.linkedin.restli.common.ErrorDetails in project rest.li by linkedin.

the class ErrorResponseBuilder method buildErrorResponse.

private ErrorResponse buildErrorResponse(RestLiServiceException result, ErrorResponseFormat errorResponseFormat) {
    ErrorResponse er = new ErrorResponse();
    if (errorResponseFormat.showStatusCodeInBody()) {
        er.setStatus(result.getStatus().getCode());
    }
    if (errorResponseFormat.showMessage() && result.getMessage() != null) {
        er.setMessage(result.getMessage());
    }
    if (errorResponseFormat.showServiceErrorCode() && result.hasServiceErrorCode()) {
        er.setServiceErrorCode(result.getServiceErrorCode());
    }
    if (errorResponseFormat.showDetails() && result.hasErrorDetails()) {
        er.setErrorDetails(new ErrorDetails(result.getErrorDetails()));
    }
    if (errorResponseFormat.showStacktrace()) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        result.printStackTrace(pw);
        er.setStackTrace(sw.toString());
        er.setExceptionClass(result.getClass().getName());
    }
    if (errorResponseFormat.showExceptionClass()) {
        er.setExceptionClass(result.getClass().getName());
    }
    return er;
}
Also used : StringWriter(java.io.StringWriter) ErrorDetails(com.linkedin.restli.common.ErrorDetails) ErrorResponse(com.linkedin.restli.common.ErrorResponse) PrintWriter(java.io.PrintWriter)

Example 2 with ErrorDetails

use of com.linkedin.restli.common.ErrorDetails in project rest.li by linkedin.

the class ParSeqRestClientTest method mockClient.

/**
   * @return a mock ParSeqRestClient that gives an error
   */
private ParSeqRestClient mockClient(final String errKey, final String errValue, final String errMsg, final int httpCode, final int appCode, final ProtocolVersion protocolVersion, final String errorResponseHeaderName) {
    final ErrorResponse er = new ErrorResponse();
    final DataMap errMap = new DataMap();
    errMap.put(errKey, errValue);
    er.setErrorDetails(new ErrorDetails(errMap));
    er.setStatus(httpCode);
    er.setMessage(errMsg);
    er.setServiceErrorCode(appCode);
    final byte[] mapBytes;
    try {
        mapBytes = new JacksonDataCodec().mapToBytes(er.data());
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    final Map<String, String> headers = new HashMap<>();
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    headers.put(errorResponseHeaderName, RestConstants.HEADER_VALUE_ERROR);
    return new ParSeqRestClient(new RestClient(new MockClient(httpCode, headers, mapBytes), "http://localhost"), RequestConfigProvider.build(new ParSeqRestliClientConfigBuilder().build(), () -> Optional.empty()));
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) HashMap(java.util.HashMap) ErrorDetails(com.linkedin.restli.common.ErrorDetails) IOException(java.io.IOException) ErrorResponse(com.linkedin.restli.common.ErrorResponse) DataMap(com.linkedin.data.DataMap)

Example 3 with ErrorDetails

use of com.linkedin.restli.common.ErrorDetails in project rest.li by linkedin.

the class RestClientTest method mockClient.

private RestClient mockClient(String errKey, String errValue, String errMsg, int httpCode, int appCode, ProtocolVersion protocolVersion, String errorResponseHeaderName) {
    ErrorResponse er = new ErrorResponse();
    DataMap errMap = new DataMap();
    errMap.put(errKey, errValue);
    er.setErrorDetails(new ErrorDetails(errMap));
    er.setStatus(httpCode);
    er.setMessage(errMsg);
    er.setServiceErrorCode(appCode);
    byte[] mapBytes;
    try {
        mapBytes = new JacksonDataCodec().mapToBytes(er.data());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Map<String, String> headers = new HashMap<String, String>();
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    headers.put(errorResponseHeaderName, RestConstants.HEADER_VALUE_ERROR);
    return new RestClient(new MyMockClient(httpCode, headers, mapBytes), "http://localhost");
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) HashMap(java.util.HashMap) ErrorDetails(com.linkedin.restli.common.ErrorDetails) IOException(java.io.IOException) ErrorResponse(com.linkedin.restli.common.ErrorResponse) DataMap(com.linkedin.data.DataMap)

Aggregations

ErrorDetails (com.linkedin.restli.common.ErrorDetails)3 ErrorResponse (com.linkedin.restli.common.ErrorResponse)3 DataMap (com.linkedin.data.DataMap)2 JacksonDataCodec (com.linkedin.data.codec.JacksonDataCodec)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1