github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/panics_test.go (about)

     1  package followschema
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/operandinc/gqlgen/graphql"
     9  
    10  	"github.com/operandinc/gqlgen/client"
    11  	"github.com/operandinc/gqlgen/graphql/handler"
    12  	"github.com/stretchr/testify/require"
    13  	"github.com/vektah/gqlparser/v2/gqlerror"
    14  )
    15  
    16  func TestPanics(t *testing.T) {
    17  	resolvers := &Stub{}
    18  	resolvers.QueryResolver.Panics = func(ctx context.Context) (panics *Panics, e error) {
    19  		return &Panics{}, nil
    20  	}
    21  	resolvers.PanicsResolver.ArgUnmarshal = func(ctx context.Context, obj *Panics, u []MarshalPanic) (b bool, e error) {
    22  		return true, nil
    23  	}
    24  	resolvers.PanicsResolver.FieldScalarMarshal = func(ctx context.Context, obj *Panics) (marshalPanic []MarshalPanic, e error) {
    25  		return []MarshalPanic{MarshalPanic("aa"), MarshalPanic("bb")}, nil
    26  	}
    27  
    28  	srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))
    29  	srv.SetRecoverFunc(func(ctx context.Context, err interface{}) (userMessage error) {
    30  		return fmt.Errorf("panic: %v", err)
    31  	})
    32  
    33  	srv.SetErrorPresenter(func(ctx context.Context, err error) *gqlerror.Error {
    34  		return &gqlerror.Error{
    35  			Message: "presented: " + err.Error(),
    36  			Path:    graphql.GetPath(ctx),
    37  		}
    38  	})
    39  
    40  	c := client.New(srv)
    41  
    42  	t.Run("panics in marshallers will not kill server", func(t *testing.T) {
    43  		var resp interface{}
    44  		err := c.Post(`query { panics { fieldScalarMarshal } }`, &resp)
    45  
    46  		require.EqualError(t, err, "http 422: {\"errors\":[{\"message\":\"presented: panic: BOOM\"}],\"data\":null}")
    47  	})
    48  
    49  	t.Run("panics in unmarshalers will not kill server", func(t *testing.T) {
    50  		var resp interface{}
    51  		err := c.Post(`query { panics { argUnmarshal(u: ["aa", "bb"]) } }`, &resp)
    52  
    53  		require.EqualError(t, err, "[{\"message\":\"presented: input: panics.argUnmarshal panic: BOOM\",\"path\":[\"panics\",\"argUnmarshal\"]}]")
    54  	})
    55  
    56  	t.Run("panics in funcs unmarshal return errors", func(t *testing.T) {
    57  		var resp interface{}
    58  		err := c.Post(`query { panics { fieldFuncMarshal(u: ["aa", "bb"]) } }`, &resp)
    59  
    60  		require.EqualError(t, err, "[{\"message\":\"presented: input: panics.fieldFuncMarshal panic: BOOM\",\"path\":[\"panics\",\"fieldFuncMarshal\"]}]")
    61  	})
    62  
    63  	t.Run("panics in funcs marshal return errors", func(t *testing.T) {
    64  		var resp interface{}
    65  		err := c.Post(`query { panics { fieldFuncMarshal(u: []) } }`, &resp)
    66  
    67  		require.EqualError(t, err, "http 422: {\"errors\":[{\"message\":\"presented: panic: BOOM\"}],\"data\":null}")
    68  	})
    69  }