craftql icon indicating copy to clipboard operation
craftql copied to clipboard

Insert entries with failed validation is returning status 500 as http code

Open rodrigosaraiva opened this issue 5 years ago • 6 comments

When we are using the mutation for insert an entry to the craft database, everything is ok when the fields are right. But if some of the fields failed in validation, instead to return 400 that is the most appropriated code for validation errors. It is returning 500. screenshot from 2019-03-06 16-43-00

screenshot from 2019-03-06 16-43-37

screenshot from 2019-03-06 16-43-26

rodrigosaraiva avatar Mar 06 '19 16:03 rodrigosaraiva

I'm not sure I agree. 400 would be a not found or not authorized class error. 500 indicates that something went wrong.

What status code would you expect and do you have any examples of other services that do something similarly?

markhuot avatar Mar 06 '19 21:03 markhuot

Hey Mark. I think it is wrong. The 500 indicates some error at the server (internal server error), something that couldn't be processed and the webserver shows this error. It is related to not treated situation, some programming error, etc. A validation error message is not a server/webserver error response. It is a proper response that the data cannot be saved at database because not respect the format, etc. The 400 and related 4xx is the http code specific for this cases. It is a bad request, and it was identified and the response indicates what is wrong for the user.

rodrigosaraiva avatar Mar 06 '19 21:03 rodrigosaraiva

@markhuot kind of agree with Rodrigo's point here, Mark. I've been surprised also by 500s coming from CraftQL's disagreement about queries. And these are not 'server' failures -- they are information-not-acceptable-to-application problems, suspect you'd agree if put that way.

Looking over the codes in usage, how about 422?

According to Wikpedia's summaries, this means '422 Unprocessable Entity (WebDAV; RFC 4918) The request was well-formed but was unable to be followed due to semantic errors.[17]'

narration-sd avatar Mar 06 '19 21:03 narration-sd

...and the other advantage of 422 is probably that it's seldom seen, thus a nice flag that can be doc'd, to let know that something pretty hidden, the CraftQL query, is the root of a problem...

narration-sd avatar Mar 07 '19 05:03 narration-sd

422 is what Laravel uses for validation errors, I think it's a good choice

riasvdv avatar Mar 08 '19 11:03 riasvdv

Surprised to reference this and find it closed -- @markhuot , any reason not to return informative codes, either case?

narration-sd avatar Mar 26 '19 18:03 narration-sd