gqlerr

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 10 Imported by: 2

README

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.

GoDoc CI Workflow

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().

Documentation

Overview

Package gqlerr provides helpers for handling errors that occur in a Go GraphQL server. It integrates logging via *zap.Logger so that all errors are logged. Codes are modeled after: https://pkg.go.dev/google.golang.org/grpc/codes

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorPresenter

func ErrorPresenter(logger *zap.Logger) func(context.Context, error) *gqlerror.Error

func RecoverFunc added in v1.1.0

func RecoverFunc(ctx context.Context, v any) error

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func AlreadyExists

func AlreadyExists(ctx context.Context, msg string, fields ...zap.Field) *Error

func FailedPrecondition

func FailedPrecondition(ctx context.Context, msg string, fields ...zap.Field) *Error

func Internal

func Internal(ctx context.Context, msg string, fields ...zap.Field) *Error

func InvalidArgument

func InvalidArgument(ctx context.Context, msg string, fields ...zap.Field) *Error

func New

func New(ctx context.Context, code codes.Code, msg string, fields ...zap.Field) *Error

New returns an initialize error with the given code. The message and fields are used for logging, and won't be visible to clients. For setting client- visible response parameters, see WithErrorID and WithMessage

func NotFound

func NotFound(ctx context.Context, msg string, fields ...zap.Field) *Error

func PermissionDenied

func PermissionDenied(ctx context.Context, msg string, fields ...zap.Field) *Error

func ResourceExhausted

func ResourceExhausted(ctx context.Context, msg string, fields ...zap.Field) *Error

func Unauthenticated

func Unauthenticated(ctx context.Context, msg string, fields ...zap.Field) *Error

func Unimplemented

func Unimplemented(ctx context.Context, msg string, fields ...zap.Field) *Error

func (*Error) AtDebug

func (e *Error) AtDebug() *Error

AtDebug overrides the default level for the error and logs at DEBUG level.

func (*Error) AtError

func (e *Error) AtError() *Error

AtError overrides the default level for the error and logs at ERROR level.

func (*Error) AtInfo

func (e *Error) AtInfo() *Error

AtInfo overrides the default level for the error and logs at INFO level.

func (*Error) AtPanic added in v1.1.0

func (e *Error) AtPanic() *Error

AtPanic overrides the default level for the error and logs at PANIC level. Note: This doesn't actually cause a panic when using a production zap logger, since we use DPanic.

func (*Error) AtWarn

func (e *Error) AtWarn() *Error

AtWarn overrides the default level for the error and logs at WARN level.

func (*Error) Error

func (e *Error) Error() string

func (*Error) WithErrorID

func (e *Error) WithErrorID(errID ErrorID) *Error

WithErrorID adds an error intended for client apps to use, and returns the error for chaining purposes. It'll appear in the GraphQL response "extensions" field, see: https://spec.graphql.org/October2021/#sec-Response-Format

func (*Error) WithMessage

func (e *Error) WithMessage(msg string) *Error

WithMessage adds an error intended for clients to see, and returns the error for chaining purposes. It'll appear in the GraphQL response "errors" field, see: https://spec.graphql.org/October2021/#sec-Errors

type ErrorID

type ErrorID string

ErrorID represents a type of error specific to the domain of the caller of this package, like admin_only or too_many_muffins.

Directories

Path Synopsis
Package codes contains error codes for working with the gqlerr package.
Package codes contains error codes for working with the gqlerr package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL