poonai/vegamcache

handle err on marshal instead of panic

poonai opened this issue · 4 comments

handle err on marshal instead of panic

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
 }

marshal should return marshaledData and err. if err, response writer should write "502 Server Error"

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?

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