user1870035
user1870035

Reputation:

How to handle Salesforce REST error response in JSON (Python)

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.

Upvotes: 0

Views: 1677

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55197

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)

Upvotes: 1

Related Questions