Search in sources :

Example 1 with SalesforceMultipleChoicesException

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

the class RestApiIntegrationTest method testStatus300.

@Test
public void testStatus300() throws Exception {
    // get test merchandise
    // note that the header value overrides sObjectFields in endpoint
    final Merchandise__c merchandise = template().requestBodyAndHeader("direct:getSObject", testId, "sObjectFields", "Name,Description__c,Price__c,Total_Inventory__c", Merchandise__c.class);
    assertNotNull(merchandise);
    assertNotNull(merchandise.getName());
    assertNotNull(merchandise.getPrice__c());
    assertNotNull(merchandise.getTotal_Inventory__c());
    CreateSObjectResult result = null;
    try {
        merchandise.clearBaseFields();
        result = template().requestBody("direct:createSObject", merchandise, CreateSObjectResult.class);
        assertNotNull(result);
        assertNotNull(result.getId());
        // note that the request SObject overrides settings on the endpoint for LineItem__c
        try {
            template().requestBody("direct:getSObjectWithId", merchandise, Merchandise__c.class);
            fail("Expected SalesforceException with statusCode 300");
        } catch (final CamelExecutionException e) {
            assertTrue(e.getCause() instanceof SalesforceException);
            assertTrue(e.getCause().getCause() instanceof SalesforceMultipleChoicesException);
            final SalesforceMultipleChoicesException cause = (SalesforceMultipleChoicesException) e.getCause().getCause();
            assertEquals(300, cause.getStatusCode());
            final List<String> choices = cause.getChoices();
            assertNotNull(choices);
            assertFalse(choices.isEmpty());
        }
    } finally {
        // delete the test clone
        if (result != null) {
            template().requestBody("direct:deleteSObject", result.getId());
        }
    }
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) CreateSObjectResult(org.apache.camel.component.salesforce.api.dto.CreateSObjectResult) Merchandise__c(org.apache.camel.component.salesforce.dto.generated.Merchandise__c) SalesforceMultipleChoicesException(org.apache.camel.component.salesforce.api.SalesforceMultipleChoicesException) List(java.util.List) Test(org.junit.Test)

Example 2 with SalesforceMultipleChoicesException

use of org.apache.camel.component.salesforce.api.SalesforceMultipleChoicesException 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

SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)2 SalesforceMultipleChoicesException (org.apache.camel.component.salesforce.api.SalesforceMultipleChoicesException)2 IOException (java.io.IOException)1 List (java.util.List)1 CamelExecutionException (org.apache.camel.CamelExecutionException)1 CreateSObjectResult (org.apache.camel.component.salesforce.api.dto.CreateSObjectResult)1 RestError (org.apache.camel.component.salesforce.api.dto.RestError)1 Merchandise__c (org.apache.camel.component.salesforce.dto.generated.Merchandise__c)1 RestChoices (org.apache.camel.component.salesforce.internal.dto.RestChoices)1 RestErrors (org.apache.camel.component.salesforce.internal.dto.RestErrors)1 Test (org.junit.Test)1