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)