Skip to content

Commit

Permalink
return spec correct gql errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Sep 19, 2024
1 parent 5b58c19 commit f112cfa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions http/handler_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,12 @@ type GraphQLResponse struct {
}

func (res GraphQLResponse) MarshalJSON() ([]byte, error) {
var errors []string
for _, err := range res.Errors {
errors = append(errors, err.Error())
errors := make([]map[string]any, len(res.Errors))
for i, err := range res.Errors {
errors[i] = map[string]any{"message": err.Error()}
}
if len(errors) == 0 {
return json.Marshal(map[string]any{"data": res.Data})
}
return json.Marshal(map[string]any{"data": res.Data, "errors": errors})
}
Expand Down
6 changes: 5 additions & 1 deletion http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func responseJSON(rw http.ResponseWriter, status int, data any) {
}

func parseError(msg any) error {
switch msg {
err, ok := msg.(map[string]any)
if !ok {
return fmt.Errorf("%v", msg)
}
switch err["message"] {
case client.ErrDocumentNotFoundOrNotAuthorized.Error():
return client.ErrDocumentNotFoundOrNotAuthorized
case datastore.ErrTxnConflict.Error():
Expand Down

0 comments on commit f112cfa

Please sign in to comment.