package org.springframework.social.salesforce.api;

import java.util.List;
import java.util.Map;

import org.springframework.social.ApiException;

/**
 * Encapsulates the error response sent by salesforce.
 * <p/>
 * 
 * @see <a
 *      href="http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_concepts_core_data_objects.htm">
 *      Salesforce API Core Datatypes</a>
 * 
 * @author Umut Utkan
 */
public class SalesforceRequestException extends ApiException {

    private static final long serialVersionUID = 7047374539651371668L;

    private final List<String> fields;
    private final String code;

    public SalesforceRequestException(String message) {
        super(message);
        this.code = null;
        this.fields = null;
    }

    @SuppressWarnings("unchecked")
    public SalesforceRequestException(Map<String, Object> errorDetails) {
        super(errorDetails.get("message").toString());

        this.code = errorDetails.get("errorCode").toString();
        this.fields = (List<String>) errorDetails.get("fields");
    }

    public List<String> getFields() {
        return fields;
    }

    public String getCode() {
        return code;
    }

}

@@ -91,16 +91,61 @@
 l.Map;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -930,16 +930,24 @@
   super(
+(String)
 errorDet
@@ -965,27 +965,16 @@
 essage%22)
-.toString()
 );%0A%0A    
@@ -989,16 +989,50 @@
 .code = 
+StringUtils.defaultString((String)
 errorDet
@@ -1056,18 +1056,19 @@
 de%22)
-.toString(
+, %22UNKNOWN%22
 );%0A 

change error creation to be a little bit better about null values