Re:How We Centralized and Structured Error Handling in Golang
by bvrmn on Wednesday December 18, 2024 @04:00AM
(#42449046)
> A 400 if you send mangled JSON, but a 200 if the request was valid but does not pass business validation rules.
What about empty required field in JSON? Is it still mangled or it's already BL?
Re:How We Centralized and Structured Error Handling in Golang
by LouisSayers on Wednesday December 18, 2024 @12:50PM
(#42452917)
As it's not to do with the http request and the body was able to be parsed, in my book that'd be classified as being at the application level, so results in a 200 status with a JSON response detailing the issue
200 OK {status: "failed", errors: ["field X is required"]}
How you deal with this on the application side, what JSON statuses you have etc is up to you.
Re:How We Centralized and Structured Error Handling in Golang
by bvrmn on Thursday December 19, 2024 @08:02AM
(#42461038)
It's an client error and it's highly beneficial to make it 400 for monitoring purposes. You want to see your FE or mobile devs deployed a faulty app.
Re:How We Centralized and Structured Error Handling in Golang
by LouisSayers on Thursday December 19, 2024 @11:23AM
(#42462976)
That depends on how you set up and do your monitoring. Not every failure needs to be indicated by an HTTP status code.
For example, on a server I'm working on there are helper functions that generate different types of responses. Responding in certain ways will produce a 200, but will also log a warning or error.
On the client side, you can create request helpers that all requests go through and that can resolve requests appropriately, rendering error messages to the user etc.
The main thing is to have a well defined, consistent approach.
I use http status codes to encode how the _request_ was handled, not necessarily the data within the request.
A 400 if you send mangled JSON, but a 200 if the request was valid but does not pass business validation rules.
Inside the 200 response is structured JSON that also has a status that is relevant at the application level.
Otherwise how can for example you tell if a 404 response is because the endpoint doesn't exist, or because the item requested at the endpoint doesn't exist?
I believe it's important to have a separation between what is happening at the API level vs Application, and this approach caters for both.