Skip to content

Instantly share code, notes, and snippets.

@elimisteve elimisteve/json.go
Created Dec 1, 2018

Embed
What would you like to do?
Go JSON handlers
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
You can’t perform that action at this time.