vegamcache icon indicating copy to clipboard operation
vegamcache copied to clipboard

handle err on marshal instead of panic

Open poonai opened this issue 7 years ago • 4 comments

poonai avatar May 09 '18 09:05 poonai

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
 }

sidthekidder avatar May 28 '18 02:05 sidthekidder

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

poonai avatar May 28 '18 07:05 poonai

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?

sidthekidder avatar Jun 01 '18 21:06 sidthekidder

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

poonai avatar Jun 02 '18 13:06 poonai