Generic REST error detection

I have an automation utility that among other things can issue requests to any REST service. An Admin level User provides the endpoint and the JSON request in a file and my utility issues the request on a defined schedule… saving the result into another file where it can get additional processing. (So for example a newspaper might get the weather forecast from a service each night.)

My problem is that there seems to be no standard for REST errors… which I need to detect.

I do check the HTTP Status and if it is not 200 then flag an error. But some services will return 200 even though there is an error. Instead the resulting JSON will include an item named “error” or an array named “errors” that include items like “error_message” or “error_code”. One service includes a boolean item named “Success”.

So I do I create a generic way to determine if there is an error or not?

I considered treating the JSON response as a string and requiring the user to enter a RegEx search expression that would match that particular’s service error message… That idea does not excite me and am looking for alternatives. Any ideas are appreciated.

your issue upload should get a (defined) success response as json or xml or just a plain string “OK”
everything else is a error.

1 Like

Unfortunately, not every REST API developer returns status 2xx only when “successful” and 4xx or other status when it is rejecting the request for some reason. So i don’t think there will be a fool-proof way of making it work generically without regard to the REST interface. I suspect the most generic thing you can do is:

  • Let them optionally define a property name/tag and expected value
  • If none is defined, treat any 2xx status as good and anything else like 4xx or 5xx as failure
  • If they define a property name or tag, examine the return value to see if it starts with { or < character
  • If starts with {, then parse into JSON and check for requested property and compare the value.
  • If starts with <, then parse into XML doc and do the same.

While I personally like regex, they are not intuitive to the common user but just being able to name a property/tag and expected value should let them quickly define a request and test if it works right (at least when it succeeds).

You could even treat the expected value as a regex pattern for those who know how to define them.