This package was developed by Silicon Ally while working on a project for Adventure Scientists. Many thanks to Adventure Scientists for supporting our open source mission!
gqlerr
gqlerr
is a package for handling errors in a Go/gqlgen-based GraphQL server.
It integrates logging via *zap.Logger so that all errors are logged. Codes are
modeled after
the gRPC error codes.
Usage
To use this package, set the gqlerr.ErrorPresenter
as your
server.SetErrorPresenter
with your already-configured *zap.Logger
instance.
See the gqlgen error docs for more
info.
From there, start replacing errors in handlers with calls to gqlerr
. For
example, if you have code like:
func (r *Resolver) SomeResolver(ctx context.Context, req model.Request) (*model.Response, error) {
if err := validate(req); err != nil {
return nil, fmt.Errorf("failed to validate request: %w", err)
}
return &model.Response{}, nil
}
You'd update it to the following:
func (r *Resolver) SomeResolver(ctx context.Context, req model.Request) (*model.Response, error) {
if err := validate(req); err != nil {
return nil, gqlerr.
InvalidArgument("request failed validation", zap.Error(err)).
WithMessage("invalid request")
}
return &model.Response{}, nil
}
This error will get transformed into a standard GraphQL {"errors": [ ... ]}
,
with the message given by WithMessage
. By default, a generic message similar
to net/http
's StatusText(...)
function will be returned. A log message will
also be written to the logger, at an appropriate log level for the error code
if one is not explicitly supplied with At{Debug,Info,Warn,Error}Level()
.