DEV Community

Discussion on: Best practice for Python returns

Collapse
 
pavelloz profile image
Paweł Kowalski • Edited

I dont know much about python, but when building graphql api, after long trial and error we went with structure like this:

{
  "errors": {},
  "results": {}
}

Errors is empty when there are no errors, and populated when something went wrong. We found out that it is easier to handle both cases when return values are consistent.

Collapse
 
belfinwe profile image
joakimRN

Hi, and thanks for the reply :)
Yeah, I was thinking of using a dictionary in a similar fashion, where one key-value pair is used to indicate if there was any trouble or not.

Having a key error seems like a nice way to create the necessary feed back in a standardized way.

Collapse
 
rfletchr profile image
Rob Fletcher

this is preferable

try:
   data = get_one({"name":"joe"})

except ConnectionError:
   # handle a ConnectionError

else:
   # if no exceptions were raised then the data is presumed to be good
   do_something_with(data)