Happy Business Starts Here

Valued Scholar

REST API Error Handling

I'm working on implementing a client for the REST APIs using the provided Swagger specifications. One thing I'm struggling a little with is proper error handling. For cases where the API returns something other than a 200 response, I can use the response code and message details to learn more about the error, but there are some APIs that will return a 200 response and set a 'success' field to 'false', as is specified in the Responses and Errors article. This response isn't documented in the specificaitons, so the code I have generated from the Swagger specification loses the 'reasons' array. All I know is that succes = false. Are there any suggestions or best practices for handling errors in this format with clients generated from the provided Swagger specification?

 

Here's an example call that will returnn a 200 response with success = false for the Create Payment call in the Payments API (https://www.zuora.com/developer/api-reference/#operation/POST_CreatePayment):

 

 

POST /v1/payments HTTP/1.1
Host: rest.apisandbox.zuora.com
apiAccessKeyid: ***
apiSecretAccessKey: ***
Content-Type: application/json
Cache-Control: no-cache

{
	
			"accountId": "***",
			"amount" : 100,
			"currency" : "USD",
			"effectiveDate" : "2017-06-23",
			"type" : "Electronic",
			"paymentMethodId" : "***",
			"invoices" : [
				{
					"amount" : 100,
					"invoiceId" :"***"
				}
			]
		
}

And the example response:

 

{
    "success": false,
    "processId": "9C5137166309351D",
    "reasons": [
        {
            "code": 53810000,
            "message": "gatewayId must be specified in order to create an electronic payment."
        }
    ]
}

 

2 REPLIES 2
Highlighted
Valued Scholar

Re: REST API Error Handling

I believe I've answered my own question. Error handling needs to occur at a higher level than parsing the response into a model object. I've implemented code to check for the existence of a "success" property and whether it is false right after receiving the response for a call. I'm implementing this C# using the the code generated from Swagger Codegen. This includes a default ExceptionFactory delegate, which I'm overriding with this error handling logic.

Community Manager

Re: REST API Error Handling

Funny how that happens :-)  but thanks for letting us know about your solution, @kconnor! Keep us posted if you run into any other problems and we can look into it further.


Lana Lee | Senior Community Manager and Strategist
"A little consideration, a little thought for others, makes all the difference." —A. A. Milne