github.com/spread-ai/gqlgen@v0.0.0-20221124102857-a6c8ef538a1d/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 the original error to avoid losing any attached annotation
    30  		return err
    31  	}
    32  	return gqlerror.WrapPath(GetPath(ctx), err)
    33  }