pokeapi icon indicating copy to clipboard operation
pokeapi copied to clipboard

Error formating

Open zaourzag opened this issue 2 years ago • 8 comments

Hello! As you might know, pokemon legends are out. Wich can cause not found errors in the db. But instead of formating it in json you format it in plain html. Why is this? As this can break alot of code . My request is if you guys could kindly change up that to json. Thank you! Zakaria Aourzag

zaourzag avatar Apr 15 '22 07:04 zaourzag

Can you post an example?

Naramsim avatar Apr 17 '22 13:04 Naramsim

If you go to https://pokeapi.co/api/v2/pokemon/900 which is from legends archeus, it'll return: "Not Found". In a non json format. Which gives you an error if you use most like code requests it'll error out.

zaourzag avatar Apr 17 '22 14:04 zaourzag

PokeAPI retrieves 404s when no resources are found. Why not verify the http status code before parsing the response?

Kronopt avatar Apr 17 '22 20:04 Kronopt

It might be because of the json parser I use, but it just goes SyntaxError: Unexpected token N in JSON at position 0

zaourzag avatar Apr 18 '22 10:04 zaourzag

Hi zaourzag,

I think what Kronopt is saying is that you should verify the http status first. If the status is 200, only then try to parse the json response.

The logic would be something like this:

if http_status_code == 200:  
    parse_json(json_data)   
else:  
   return pokemon_doesnt_exist error  

aicampbell avatar Apr 18 '22 13:04 aicampbell

Hi yes, all the proposed solutions are valid. But I like the idea of returning JSON formatted error codes if the resource is missing.

In the meantime, check the status code before attempting to read the returned data.

https://jasonwatmore.com/post/2021/10/09/fetch-error-handling-for-failed-http-responses-and-network-errors#:~:text=The%20fetch()%20function%20will,ourselves%20by%20calling%20return%20Promise.

Naramsim avatar Apr 18 '22 19:04 Naramsim

Will do, but it was just more of an inconsistency I found lol. (For the record, I use a node http lib based off of centra and undici)

zaourzag avatar Apr 18 '22 20:04 zaourzag

I mean I tried to find where the 404 error code is. I'm familiar with python but I'm not a master in it lol. Nodejs and Express are more my jam :P

zaourzag avatar Apr 19 '22 11:04 zaourzag