Search in sources :

Example 1 with RestError

use of org.apache.camel.component.salesforce.api.dto.RestError in project camel by apache.

the class SalesforceException method appendFields.

protected void appendFields(StringBuilder builder) {
    // append message
    builder.append("message:'");
    builder.append(getMessage());
    builder.append("',");
    // check for error
    if (errors != null) {
        builder.append("errors:[");
        for (RestError error : errors) {
            builder.append(error.toString());
        }
        builder.append("],");
    }
    // append statusCode
    builder.append("statusCode:");
    builder.append(statusCode);
}
Also used : RestError(org.apache.camel.component.salesforce.api.dto.RestError)

Example 2 with RestError

use of org.apache.camel.component.salesforce.api.dto.RestError in project camel by apache.

the class SalesforceException method toErrorMessage.

private static String toErrorMessage(List<RestError> errors, int statusCode) {
    StringBuilder builder = new StringBuilder("{");
    if (errors != null) {
        builder.append("errors:[");
        for (RestError error : errors) {
            builder.append(error.toString());
        }
        builder.append("],");
    }
    builder.append("statusCode:");
    builder.append(statusCode);
    builder.append("}");
    return builder.toString();
}
Also used : RestError(org.apache.camel.component.salesforce.api.dto.RestError)

Example 3 with RestError

use of org.apache.camel.component.salesforce.api.dto.RestError in project camel by apache.

the class SalesforceSession method parseLoginResponse.

/**
     * Parses login response, allows SalesforceSecurityHandler to parse a login request for a failed authentication
     * conversation.
     */
public synchronized void parseLoginResponse(ContentResponse loginResponse, String responseContent) throws SalesforceException {
    final int responseStatus = loginResponse.getStatus();
    try {
        switch(responseStatus) {
            case HttpStatus.OK_200:
                // parse the response to get token
                LoginToken token = objectMapper.readValue(responseContent, LoginToken.class);
                // don't log token or instance URL for security reasons
                LOG.info("Login successful");
                accessToken = token.getAccessToken();
                instanceUrl = token.getInstanceUrl();
                // notify all session listeners
                for (SalesforceSessionListener listener : listeners) {
                    try {
                        listener.onLogin(accessToken, instanceUrl);
                    } catch (Throwable t) {
                        LOG.warn("Unexpected error from listener {}: {}", listener, t.getMessage());
                    }
                }
                break;
            case HttpStatus.BAD_REQUEST_400:
                // parse the response to get error
                final LoginError error = objectMapper.readValue(responseContent, LoginError.class);
                final String msg = String.format("Login error code:[%s] description:[%s]", error.getError(), error.getErrorDescription());
                final List<RestError> errors = new ArrayList<RestError>();
                errors.add(new RestError(msg, error.getErrorDescription()));
                throw new SalesforceException(errors, HttpStatus.BAD_REQUEST_400);
            default:
                throw new SalesforceException(String.format("Login error status:[%s] reason:[%s]", responseStatus, loginResponse.getReason()), responseStatus);
        }
    } catch (IOException e) {
        String msg = "Login error: response parse exception " + e.getMessage();
        throw new SalesforceException(msg, e);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) RestError(org.apache.camel.component.salesforce.api.dto.RestError) LoginError(org.apache.camel.component.salesforce.internal.dto.LoginError) ArrayList(java.util.ArrayList) LoginToken(org.apache.camel.component.salesforce.internal.dto.LoginToken) IOException(java.io.IOException)

Example 4 with RestError

use of org.apache.camel.component.salesforce.api.dto.RestError in project camel by apache.

the class DefaultBulkApiClient method createRestException.

@Override
protected SalesforceException createRestException(Response response, InputStream responseContent) {
    // this must be of type Error
    try {
        final Error error = unmarshalResponse(responseContent, response.getRequest(), Error.class);
        final RestError restError = new RestError();
        restError.setErrorCode(error.getExceptionCode());
        restError.setMessage(error.getExceptionMessage());
        return new SalesforceException(Arrays.asList(restError), response.getStatus());
    } catch (SalesforceException e) {
        String msg = "Error un-marshaling Salesforce Error: " + e.getMessage();
        return new SalesforceException(msg, e);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) RestError(org.apache.camel.component.salesforce.api.dto.RestError) RestError(org.apache.camel.component.salesforce.api.dto.RestError) Error(org.apache.camel.component.salesforce.api.dto.bulk.Error)

Example 5 with RestError

use of org.apache.camel.component.salesforce.api.dto.RestError in project camel by apache.

the class DefaultRestClient method createRestException.

@Override
protected SalesforceException createRestException(Response response, InputStream responseContent) {
    // get status code and reason phrase
    final int statusCode = response.getStatus();
    String reason = response.getReason();
    if (reason == null || reason.isEmpty()) {
        reason = HttpStatus.getMessage(statusCode);
    }
    // try parsing response according to format
    try {
        if (responseContent != null && responseContent.available() > 0) {
            final List<String> choices;
            // return list of choices as error message for 300
            if (statusCode == HttpStatus.MULTIPLE_CHOICES_300) {
                if (PayloadFormat.JSON.equals(format)) {
                    choices = objectMapper.readValue(responseContent, TypeReferences.STRING_LIST_TYPE);
                } else {
                    RestChoices restChoices = new RestChoices();
                    xStream.fromXML(responseContent, restChoices);
                    choices = restChoices.getUrls();
                }
                return new SalesforceMultipleChoicesException(reason, statusCode, choices);
            } else {
                final List<RestError> restErrors;
                if (PayloadFormat.JSON.equals(format)) {
                    restErrors = objectMapper.readValue(responseContent, TypeReferences.REST_ERROR_LIST_TYPE);
                } else {
                    RestErrors errors = new RestErrors();
                    xStream.fromXML(responseContent, errors);
                    restErrors = errors.getErrors();
                }
                return new SalesforceException(restErrors, statusCode);
            }
        }
    } catch (IOException e) {
        // log and ignore
        String msg = "Unexpected Error parsing " + format + " error response body + [" + responseContent + "] : " + e.getMessage();
        log.warn(msg, e);
    } catch (RuntimeException e) {
        // log and ignore
        String msg = "Unexpected Error parsing " + format + " error response body + [" + responseContent + "] : " + e.getMessage();
        log.warn(msg, e);
    }
    // just report HTTP status info
    return new SalesforceException("Unexpected error: " + reason + ", with content: " + responseContent, statusCode);
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) RestErrors(org.apache.camel.component.salesforce.internal.dto.RestErrors) RestChoices(org.apache.camel.component.salesforce.internal.dto.RestChoices) RestError(org.apache.camel.component.salesforce.api.dto.RestError) SalesforceMultipleChoicesException(org.apache.camel.component.salesforce.api.SalesforceMultipleChoicesException) IOException(java.io.IOException)

Aggregations

RestError (org.apache.camel.component.salesforce.api.dto.RestError)7 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 XStream (com.thoughtworks.xstream.XStream)1 ArrayList (java.util.ArrayList)1 SalesforceMultipleChoicesException (org.apache.camel.component.salesforce.api.SalesforceMultipleChoicesException)1 Error (org.apache.camel.component.salesforce.api.dto.bulk.Error)1 LoginError (org.apache.camel.component.salesforce.internal.dto.LoginError)1 LoginToken (org.apache.camel.component.salesforce.internal.dto.LoginToken)1 RestChoices (org.apache.camel.component.salesforce.internal.dto.RestChoices)1 RestErrors (org.apache.camel.component.salesforce.internal.dto.RestErrors)1