API Error Handling

Is there a consistent error object? I haven’t been able to find much documentation on Error Handling and what errors I’ve come across use different responses. For example, if you attempt to create a customer and the admin email is a duplicate, the exception is a ValidationError and you can access the response messages as follow:

except dwollav2.ValidationError as e:
    return HttpResponse(status=e.status, reason=e.body['_embedded']['errors'][0]['message'])

If a user were to attempt to add a new bank account, and this bank account has already been added, the exception is just a generic ‘Error’, which is fine, but the entire body of the response is different from the previous example.

try:
    customer = app_token.post('%s/funding-sources' % customer_url, request_body)
except dwollav2.error.Error as e:
    # the captured error response is as follow:
    # e.status = 400
    # e.body:
          {'code' : 'DuplicateResource', 
           'message': 'Bank already exists: id=...',
           '_links': {'about': {'href': '..., 'type': 'application/vnd.dwolla.v1.hal+json', 'resource-type': 'funding-source'}}}

Why are the error object attributes different from error to error?

Is there any guidance or updates on this?

Hi @jmtichell!

Yes – errors should be consistent all across the endpoints and resources. I can help clarify those different error messages above.

All 4xx level errors would have a response like the following:

{
    "code": "DuplicateResource",
    "message": "Bank already exists: id=f326dbc1-6ffa-4474-843b-f776bc2a5063",
    // additional information if it exists, like links
}

However, if it’s a 400 error with a top-level error code of ValidationError then it’ll have an embedded list of errors like the following:

{
    "code": "ValidationError",
    "message": "Validation error(s) present. See embedded errors list for more details.",
    "_embedded": {
        "errors": [
            {
                "code": "Required",
                "message": "FirstName required.",
                "path": "/firstName",
                "_links": {}
            }
        ]
    }
} 

Hope that helps! Let us know if you come across any questions! :slight_smile:

Thank you @shreya. Just to confirm, the only case I need to handle this differently will be the 400 error for ValidationError?

Hi @jmtichell – that would be correct!