Newbie question related to golang REST API error handling practice or universal REST API practice ,5 minutes read ahead.
I am new to golang and I am creating REST apis. I have a question regarding error handling on how to handle error and send it to client.Suppose we get a database error (mongo) of unsuccessful connection to db or some error . My question is should I send that error with my response?
WHAT I AM DOING CURRENTLY?
I am sending status code ,message and data.
If there is successfulretrieval of data from DB => (200,"User found",{_id:"123",name:"johndoe"}). If there is error : for error like mongo error orunmarshalling error => (500,"internal server error",nil)
for error like user not found then like this => (404,"User not found",nil)
2024/05/17 16:20:42 encountered error while decoding json: json: cannot unmarshal number into Go struct field LoginUser.email of type string
{ "status": 500, "message": "internal server error", "data": null }
here I sent integer instead of string to generate error andofcourse i am logging it into console.
My question is should I send error like below with my response to client or should I not ? If yes then conditionally for dev environment or prod too? again I am logging error and info to console for dev env
2024/05/17 16:20:42 encountered error while decoding json: json: cannot unmarshal number into Go struct field LoginUser.email of type string
I have asked in discord community but didn't got the much clear response.Thanks for your time to read and reply, have a great day.