Created
December 1, 2018 04:29
-
-
Save elimisteve/2780dfcb10618001b0c25e005999281c to your computer and use it in GitHub Desktop.
Go JSON handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
log "github.com/Sirupsen/logrus" | |
"github.com/gorilla/websocket" | |
) | |
const contentTypeJSON = "application/json; charset=utf-8" | |
func WriteError(w http.ResponseWriter, errStr string, secretErr error) error { | |
return WriteErrorStatus(w, errStr, secretErr, http.StatusInternalServerError) | |
} | |
func WriteErrorStatus(w http.ResponseWriter, errStr string, secretErr error, status int) error { | |
log.Debugf("Real error: %v", secretErr) | |
log.Debugf("Returning HTTP %d w/error: %q", status, errStr) | |
w.Header().Set("Content-Type", contentTypeJSON) | |
w.WriteHeader(status) | |
_, err := fmt.Fprintf(w, `{"error":%q}`, errStr) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment