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

The case for a MustWriteJson helper ? #176

Open
ant0ine opened this issue Jan 18, 2016 · 1 comment
Open

The case for a MustWriteJson helper ? #176

ant0ine opened this issue Jan 18, 2016 · 1 comment

Comments

@ant0ine
Copy link
Owner

ant0ine commented Jan 18, 2016

writer.WriteJson(...) returns an error. The JSON serialization can fail for many reasons (eg: NaN, channel, complex, ...).

If such error is a valid possibility in the program, if the JSON API deals with data that can contain sources of serialization failures, then, the regular error handling is appropriate:

                        err := w.WriteJson(...)
                        if err != nil {
                                // the following, or any other appropriate error handling
                                rest.Error(w, err.Error(), http.StatusInternalServerError)
                                return
                        }

But if a serialization error is considered a program bug, worth a stop in the processing of the request, then it could be a valid use of panic:

                        err := w.WriteJson(...)
                        if err != nil {
                             panic(err.Error())
                        }

This last piece of code could be provided by the framework like this: func MustWriteJson(w ResponseWriter, v interface{})

@wingyplus
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants