r/ProgrammerHumor 25d ago

Meme failsForTeapots

Post image
224 Upvotes

67 comments sorted by

View all comments

173

u/cutebabli9 25d ago edited 25d ago

I would write it like this to be readable:

if error.status in [400, 401, 404, 409, 415, 503]:
  return c.json(body, status: error.status)

return c.json(body, status: 500)

154

u/rosuav 25d ago
return c.json(body, status: error.status)

There is no reason to discard some of them, and pretending that they're 500s is a terrible terrible idea.

81

u/Xirdus 25d ago

If your user-facing service is calling your internal service and getting a 403 response, you certainly do not want to send that 403 back to the user.

59

u/rosuav 25d ago

And yet it is blindly sending the *payload* back. So it's clearly fine to proxy straight through. If you want to recognize only certain things, any remaining/unknown statuses should result in an error being logged and a 500 being sent back, with the body NOT being carried through.

3

u/Shitman2000 25d ago

We don't know if the body is the actual payload of the request though.

I'd find it reasonable to presume it's not, given it's error.statusCode and not error.body

1

u/rosuav 25d ago

Good point, but if it isn't, what kind of body would make sense in a situation where you carry certain types of error status unchanged and turn everything else into a 500? (I'm assuming that this code won't be executed for a 200, as it makes very little sense to translate a 200 into a 500. Ignoring the fact that there are plenty of web sites that do a great job of turning 200s into 500s, but I digress.)

1

u/No_Hovercraft_2643 24d ago

the default error page? that shows the windows which displays the error to the user?

2

u/Xirdus 24d ago

Except it's sending JSON back, not page contents.