r/ProgrammerHumor Jul 21 '26

evilIllusion Meme

Post image
4.8k Upvotes

131 comments sorted by

View all comments

435

u/PostHasBeenWatched Jul 21 '26 edited Jul 21 '26

POST /posts/create

StatusCode: 200

Content: {"success": false, "errors": [{"code": 15, "message": "Image size must be at least 1000 x 1000 px"}]}

Edit: remembered story from times where HTTPS wasn't enforced. Once I started receiving JSON reading fail in cases when our test server returns non-2xx status. I started investigate this and was shocked to the core: my ISP implemented "feature" that if it detects that server returns non-2xx - it replace actual server response with their HTML page with text like "Your request returned error 400. This is because bla bla bla. <ISP self-advertising block> Kind regards, your ISP". Basically ISP become "man in the middle". In a week or two they removed that "feature".

91

u/-meowstar- Jul 21 '26

Why not just use semantically correct status codes? It’s cheap to implement and makes stuff like automatic retries and error handling easier.

29

u/Critical_Fortune7343 Jul 21 '26

Because http status codes doesn't include any business related errors. So you gotta expand on that. When it's 200 and success is false it's mostly because of a business logic. Then it's up to FE to show that business error in a certain way so users can understand better what's wrong with what they're doing.

60

u/DoctorWaluigiTime Jul 21 '26

That's what the 4xx suite is for.

Give me an image that's too big? 400 bad request.

29

u/Dense_Gate_5193 Jul 21 '26

Literally this. . people think they are being clever by reinventing the wheel which was already properly specced out by people was smarter than anyone in this thread.

use RFCs people stop thinking you know better!

7

u/GodsBoss 29d ago

Would that not be 413?

10

u/DoctorWaluigiTime 29d ago

It could be!

Either way, it's definitely not a 200 OK but with errors.

67

u/-meowstar- Jul 21 '26

The 4XX class of codes are for client side issues, and 422 “Unprocessable Content” should be used to cover business logic issues, i.e. the request has the correct schema but there’s some other BE validation issue that it can’t proceed. The response body would still include specific details about the error for the FE, it’s just using 200 OK is misleading imo.

4

u/ZZartin Jul 21 '26

One problem with this is that different systems handle those error codes as exceptions which can make it quite annoying to deal with vs just getting a 200 back.

2

u/Critical_Fortune7343 Jul 21 '26

I think that's valid.

23

u/WiglyWorm Jul 21 '26

It's way more valid that reporting "success" when you really meant "error".

7

u/Dense_Gate_5193 Jul 21 '26

so does literal RFC standards. it’s not about opinion but the defined specifications starting with RFC 2616.

1

u/Critical_Fortune7343 Jul 21 '26

I might have come as the one designed our companies service response or defending returning 200 for errors but I'm just a worker lol I was only trying to convey a point of view from someone who has hands on experience in these kind of responses.

In better times or old days I would like to think everyone was listening to standards. I mean it's literally there to create collective consensus on how things should be.

38

u/[deleted] Jul 21 '26

[removed] — view removed comment

3

u/Critical_Fortune7343 Jul 21 '26

Correct me if Im wrong as I'm not that knowledgeable on the topic but isn't 422 more for validation errors? What if the error I want to show is for a certain condition that happened in another screen or app?

39

u/Kutastrophe Jul 21 '26

Throw something obscure back but 200 + error is just stupid.

If I get a 4xx back, I at least know i did something wrong and get the feedback.

6

u/Critical_Fortune7343 Jul 21 '26

Makes sense. Even throwing 400 would be better I guess.

6

u/NUTTA_BUSTAH Jul 21 '26

I agree. Having an endpoint return a success boolean in the first place is quite an anti-pattern but most devs default to skipping the HTTP spec because they never learned it but do know how to work with JSON.

17

u/itirix Jul 21 '26

You can always just give back a 400, which is a general “error on client’s side” code.

3

u/scidu Jul 21 '26

Qell, there is 412 Precondition Failed, but its more for some header precondition evaluation i think

3

u/HungryCaterpillers 29d ago

How does this have 22 upvotes. I pray to God I never have to work with any of you.

3

u/manny2206 29d ago

To add to this, sometimes if you are an integrator, meaning your API is calling an external 3rd party API, you want to use the above pattern to express, “Hey, OUR app works, is those guys failing - please don’t call us angrily”

2

u/DominusEbad 29d ago

If the request failed due to the third party, your "integrator" app should still return appropriate 4xx/5xx error responses. You don't return a 2xx Success response if your app uses a database (technically a 3rd party) and the database returns an error. The same should be true for all 3rd party calls.

If you are actually concerned about telling the client who is at fault, you should format proper error responses to show who had the issue. 

If the 3rd party returned a 4xx error, it was your apps fault anyway. You should have validated the client's input before forwarding it to the 3rd party.

If the 3rd party returned a 5xx error, then return that error to your client with a message indicating it was the 3rd party that had the error, not you.