MartinsOnuoha / countriesNowAPI

CountriesNow is an Open source API for retrieving geo-information for countries, including their states, cities, population, etc. 🌎
http://countriesnow.space
MIT License
185 stars 59 forks source link

"error" field in JSON response #11

Closed iambenkay closed 4 years ago

iambenkay commented 4 years ago

Good day. Nice work you're doing here. I was going through the documentation and I noticed you add the error field to every JSON response. I want to know why exactly. because I assume that errors can be conveyed with appropriate error codes (status codes greater than or equal to 400). I believe that is the semantic use of those status codes. Is there a special reason you set the error field?

MartinsOnuoha commented 4 years ago

@iambenkay Thanks for the observation. Yeah, there's a reason, simplicity really. Per the JSON:API specification, dealing with errors would require the using the Error Object property that would returns an array of error objects containing the status, code and detail fields, this would be the ideal design for REST apis like this one, however, for simplicity instead of having the entire Error array object, I decided to make it root level field and give it the value of it's child property status (true, false) - so it's readable enough.

so instead of having this:

{
   "error": [{ "status": false, "code":  401, "details": "unauthorized" }, {...}, {...}],
   "data": []
}

we have this:

{
  "error": false,
  "data": []
}

It's mostly a design for simplicity than architecture

iambenkay commented 4 years ago

Alright. thanks for clearing that up

iambenkay commented 4 years ago

I'll be closing this issue now