github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/graphql/handler/transport/error.go (about)

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