github.com/99designs/gqlgen@v0.17.45/graphql/handler/transport/error.go (about) 1 package transport 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 8 "github.com/vektah/gqlparser/v2/gqlerror" 9 10 "github.com/99designs/gqlgen/graphql" 11 ) 12 13 // SendError sends a best effort error to a raw response writer. It assumes the client can understand the standard 14 // json error response 15 func SendError(w http.ResponseWriter, code int, errors ...*gqlerror.Error) { 16 w.WriteHeader(code) 17 b, err := json.Marshal(&graphql.Response{Errors: errors}) 18 if err != nil { 19 panic(err) 20 } 21 w.Write(b) 22 } 23 24 // SendErrorf wraps SendError to add formatted messages 25 func SendErrorf(w http.ResponseWriter, code int, format string, args ...interface{}) { 26 SendError(w, code, &gqlerror.Error{Message: fmt.Sprintf(format, args...)}) 27 }