-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ysugimoto/strict-graphql-error
Strict graphql error
- Loading branch information
Showing
8 changed files
with
188 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# CHANGELOG | ||
|
||
## v0.12.0 | ||
|
||
### Define MiddlewareError and respond with error code | ||
|
||
We defined `MiddleWareError` which can return in Middleware function. If middleware function returns that pointer instead of common error, | ||
The runtime responds error with that field data. | ||
|
||
The definition is: | ||
|
||
```go | ||
type MiddlewareError struct { | ||
Code string | ||
Message string | ||
} | ||
|
||
// generate error | ||
return runtime.NewMiddlewareError("CODE", "MESSAGE") | ||
``` | ||
|
||
If middleware returns common error, the runtime error will be: | ||
|
||
``` | ||
{"data": null, "errors": [{"message": "error message of err.Error()", "extensions": {"code": "MIDDLEWARE_ERROR"}]} | ||
``` | ||
|
||
If middleware returns `MiddlewareError`, the runtime error will be: | ||
|
||
``` | ||
{"data": null, "errors": [{"message": "Message field value of MiddlewareError", "extensions": {"code": "Code field value of MiddlewareError"}]} | ||
``` | ||
|
||
|
||
## v0.9.1 | ||
|
||
### Changed middleware fucntion type | ||
|
||
On MiddlewareFunc, you need to return `context.Context` as first return value. this is because we need to make custom metadata to gRPC on middleware process. | ||
If you are already using your onw middleware, plase change its interface. see https://github.com/ysugimoto/grpc-graphql-gateway/pull/10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package runtime | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
"github.com/graphql-go/graphql" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMiddlewareError(t *testing.T) { | ||
t.Run("Set message field on common error", func(t *testing.T) { | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
mux := NewServeMux() | ||
mux.Use(func(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, error) { | ||
return ctx, errors.New("error") | ||
}) | ||
mux.ServeHTTP(w, r) | ||
})) | ||
defer srv.Close() | ||
resp, err := http.Get(srv.URL) | ||
assert.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
var r graphql.Result | ||
err = json.NewDecoder(resp.Body).Decode(&r) | ||
assert.NoError(t, err) | ||
assert.Nil(t, r.Data) | ||
if assert.Len(t, r.Errors, 1) { | ||
e := r.Errors[0] | ||
assert.Equal(t, "error", e.Message) | ||
if assert.Len(t, e.Extensions, 1) { | ||
assert.Equal(t, "MIDDLEWARE_ERROR", e.Extensions["code"]) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Set message field on MiddewareError", func(t *testing.T) { | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
mux := NewServeMux() | ||
mux.Use(func(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, error) { | ||
return ctx, NewMiddlewareError("CUSTOM_CODE", "CUSTOM_MESSAGE") | ||
}) | ||
mux.ServeHTTP(w, r) | ||
})) | ||
defer srv.Close() | ||
resp, err := http.Get(srv.URL) | ||
assert.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
var r graphql.Result | ||
err = json.NewDecoder(resp.Body).Decode(&r) | ||
assert.NoError(t, err) | ||
assert.Nil(t, r.Data) | ||
if assert.Len(t, r.Errors, 1) { | ||
e := r.Errors[0] | ||
assert.Equal(t, "CUSTOM_MESSAGE", e.Message) | ||
if assert.Len(t, e.Extensions, 1) { | ||
assert.Equal(t, "CUSTOM_CODE", e.Extensions["code"]) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters