github.com/apipluspower/gqlgen@v0.15.2/graphql/error.go (about)

     1  package graphql
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/vektah/gqlparser/v2/gqlerror"
     8  )
     9  
    10  type ErrorPresenterFunc func(ctx context.Context, err error) *gqlerror.Error
    11  
    12  func DefaultErrorPresenter(ctx context.Context, err error) *gqlerror.Error {
    13  	var gqlErr *gqlerror.Error
    14  	if errors.As(err, &gqlErr) {
    15  		return gqlErr
    16  	}
    17  	return gqlerror.WrapPath(GetPath(ctx), err)
    18  }
    19  
    20  func ErrorOnPath(ctx context.Context, err error) error {
    21  	if err == nil {
    22  		return nil
    23  	}
    24  	var gqlErr *gqlerror.Error
    25  	if errors.As(err, &gqlErr) {
    26  		if gqlErr.Path == nil {
    27  			gqlErr.Path = GetPath(ctx)
    28  		}
    29  		return gqlErr
    30  	}
    31  	return gqlerror.WrapPath(GetPath(ctx), err)
    32  }