Question

I was just wondering how to correctly handle Salesforce error responses for an portal I'm developing. Or more generically, just how to handle a JSON error response. For instance, if I queried a database for information that didn't exist, if a user provided incorrect login credentials, etc. I'm looking for a widely accepted, pythonic solution to this problem.

Thank you.

Was it helpful?

Solution

Check the response, if there's an error, raise an Exception.

Ideally, the Exception should match the error message returned by the API, and include all the information that was returned by the API.

If you're writing a library, this will let the end-user decide how they want to proceed.


Here's an example of code I wrote for a Salesforce REST API wrapper:

The exception:

class SOQLException(SFDCException): 
    def __init__(self, errorCode, message):
        self.errorCode = errorCode
        self.message = message

And in the code making the request, after loading the JSON as data.

Maybe this changed, but Salesforce used to return the error dict inside an array:

    if len(data) == 1 and u"errorCode" in data[0]:
        error = data[0]
        raise SOQLException(**error)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top