handle err on marshal instead of panic
poonai opened this issue · 4 comments
poonai commented
handle err on marshal instead of panic
sidthekidder commented
Do you mean something like this?
func marshal(data interface{}) []byte {
marshaledData, err := json.Marshal(data)
if err != nil {
- panic(err)
+ return []byte("502 Server Error")
}
return marshaledData
}
poonai commented
marshal should return marshaledData and err. if err, response writer should write "502 Server Error"
sidthekidder commented
Yes that's what will happen right, we are shifting the err check into marshal() func. If its successful marshaledData will get returned and written, else "server error" bytes will get written. Otherwise the err check will get replicated 3 times.
How do I test this error?
poonai commented
to get err, pass a something which not a json.
err check will get replicated 3 times
It looks idiomatic, that's why to return err