Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle err on marshal instead of panic #8

Open
poonai opened this issue May 9, 2018 · 4 comments
Open

handle err on marshal instead of panic #8

poonai opened this issue May 9, 2018 · 4 comments
Labels
bug Something isn't working

Comments

@poonai
Copy link
Owner

poonai commented May 9, 2018

No description provided.

@poonai poonai added the bug Something isn't working label May 9, 2018
@sidthekidder
Copy link

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
Copy link
Owner Author

poonai commented May 28, 2018

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

@sidthekidder
Copy link

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
Copy link
Owner Author

poonai commented Jun 2, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants