forked from mikhailv/graphql-transport-ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
37 lines (31 loc) · 979 Bytes
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package transport
import (
"encoding/json"
"fmt"
"net/http"
"github.com/vektah/gqlparser/v2/gqlerror"
)
type gqlResponse struct {
Errors gqlerror.List `json:"errors,omitempty"`
Data json.RawMessage `json:"data"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
// SendError sends a best effort error to a raw response writer. It assumes the client can understand the standard
// json error response
func SendError(w http.ResponseWriter, code int, errors ...*gqlerror.Error) {
w.WriteHeader(code)
b, err := jsonEncode(&gqlResponse{Errors: errors})
if err != nil {
panic(err)
}
_, _ = w.Write(b)
}
// SendErrorf wraps SendError to add formatted messages
func SendErrorf(w http.ResponseWriter, code int, format string, args ...interface{}) {
SendError(w, code, &gqlerror.Error{Message: fmt.Sprintf(format, args...)})
}
func toGQLError(err error) *gqlerror.Error {
return &gqlerror.Error{
Message: err.Error(),
}
}