Skip to content

Commit

Permalink
Update errors to use go1.13 semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
mtibben committed Sep 8, 2021
1 parent 8f179be commit a991e3e
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 74 deletions.
22 changes: 11 additions & 11 deletions api/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"fmt"
"syscall"

"github.com/99designs/gqlgen/codegen"
Expand All @@ -9,7 +10,6 @@ import (
"github.com/99designs/gqlgen/plugin/federation"
"github.com/99designs/gqlgen/plugin/modelgen"
"github.com/99designs/gqlgen/plugin/resolvergen"
"github.com/pkg/errors"
)

func Generate(cfg *config.Config, option ...Option) error {
Expand Down Expand Up @@ -40,7 +40,7 @@ func Generate(cfg *config.Config, option ...Option) error {
}

if err := cfg.LoadSchema(); err != nil {
return errors.Wrap(err, "failed to load schema")
return fmt.Errorf("failed to load schema: %w", err)
}

for _, p := range plugins {
Expand All @@ -53,50 +53,50 @@ func Generate(cfg *config.Config, option ...Option) error {

// LoadSchema again now we have everything
if err := cfg.LoadSchema(); err != nil {
return errors.Wrap(err, "failed to load schema")
return fmt.Errorf("failed to load schema: %w", err)
}

if err := cfg.Init(); err != nil {
return errors.Wrap(err, "generating core failed")
return fmt.Errorf("generating core failed: %w", err)
}

for _, p := range plugins {
if mut, ok := p.(plugin.ConfigMutator); ok {
err := mut.MutateConfig(cfg)
if err != nil {
return errors.Wrap(err, p.Name())
return fmt.Errorf("%s: %w", p.Name(), err)
}
}
}
// Merge again now that the generated models have been injected into the typemap
data, err := codegen.BuildData(cfg)
if err != nil {
return errors.Wrap(err, "merging type systems failed")
return fmt.Errorf("merging type systems failed: %w", err)
}

if err = codegen.GenerateCode(data); err != nil {
return errors.Wrap(err, "generating core failed")
return fmt.Errorf("generating core failed: %w", err)
}
if err = cfg.Packages.ModTidy(); err != nil {
return errors.Wrap(err, "tidy failed")
return fmt.Errorf("tidy failed: %w", err)
}

for _, p := range plugins {
if mut, ok := p.(plugin.CodeGenerator); ok {
err := mut.GenerateCode(data)
if err != nil {
return errors.Wrap(err, p.Name())
return fmt.Errorf("%s: %w", p.Name(), err)
}
}
}

if err = codegen.GenerateCode(data); err != nil {
return errors.Wrap(err, "generating core failed")
return fmt.Errorf("generating core failed: %w", err)
}

if !cfg.SkipValidation {
if err := validate(cfg); err != nil {
return errors.Wrap(err, "validation failed")
return fmt.Errorf("validation failed: %w", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/gen.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cmd

import (
"errors"
"os"

"github.com/99designs/gqlgen/api"
"github.com/99designs/gqlgen/codegen/config"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)

Expand All @@ -26,7 +26,7 @@ var genCmd = &cli.Command{
}
} else {
cfg, err = config.LoadConfigFromDefaultLocations()
if os.IsNotExist(errors.Cause(err)) {
if os.IsNotExist(errors.Unwrap(err)) {
cfg, err = config.LoadDefaultConfig()
}

Expand Down
3 changes: 1 addition & 2 deletions codegen/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/codegen/templates"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/v2/ast"
)

Expand Down Expand Up @@ -67,7 +66,7 @@ func (b *builder) buildArg(obj *Object, arg *ast.ArgumentDefinition) (*FieldArgu
if arg.DefaultValue != nil {
newArg.Default, err = arg.DefaultValue.Value(nil)
if err != nil {
return nil, errors.Errorf("default value is not valid: %s", err.Error())
return nil, fmt.Errorf("default value is not valid: %w", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package config

import (
"errors"
"fmt"
"go/token"
"go/types"

"github.com/99designs/gqlgen/codegen/templates"
"github.com/99designs/gqlgen/internal/code"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/v2/ast"
)

Expand Down Expand Up @@ -119,9 +119,9 @@ func (b *Binder) FindObject(pkgName string, typeName string) (types.Object, erro
if pkg == nil {
err := b.pkgs.Errors()
if err != nil {
return nil, errors.Wrapf(err, "package could not be loaded: %s", fullName)
return nil, fmt.Errorf("package could not be loaded: %s: %w", fullName, err)
}
return nil, errors.Errorf("required package was not loaded: %s", fullName)
return nil, fmt.Errorf("required package was not loaded: %s", fullName)
}

// function based marshalers take precedence
Expand All @@ -148,7 +148,7 @@ func (b *Binder) FindObject(pkgName string, typeName string) (types.Object, erro
}
}

return nil, errors.Errorf("unable to find type %s\n", fullName)
return nil, fmt.Errorf("unable to find type %s\n", fullName)
}

func (b *Binder) PointerTo(ref *TypeReference) *TypeReference {
Expand Down
27 changes: 13 additions & 14 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/99designs/gqlgen/internal/code"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -59,7 +58,7 @@ func LoadDefaultConfig() (*Config, error) {
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
if err != nil {
return nil, errors.Wrap(err, "unable to open schema")
return nil, fmt.Errorf("unable to open schema: %w", err)
}

config.Sources = append(config.Sources, &ast.Source{Name: filename, Input: string(schemaRaw)})
Expand All @@ -78,7 +77,7 @@ func LoadConfigFromDefaultLocations() (*Config, error) {

err = os.Chdir(filepath.Dir(cfgFile))
if err != nil {
return nil, errors.Wrap(err, "unable to enter config dir")
return nil, fmt.Errorf("unable to enter config dir: %w", err)
}
return LoadConfig(cfgFile)
}
Expand All @@ -96,11 +95,11 @@ func LoadConfig(filename string) (*Config, error) {

b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, errors.Wrap(err, "unable to read config")
return nil, fmt.Errorf("unable to read config: %w", err)
}

if err := yaml.UnmarshalStrict(b, config); err != nil {
return nil, errors.Wrap(err, "unable to parse config")
return nil, fmt.Errorf("unable to parse config: %w", err)
}

if err := CompleteConfig(config); err != nil {
Expand Down Expand Up @@ -150,13 +149,13 @@ func CompleteConfig(config *Config) error {

return nil
}); err != nil {
return errors.Wrapf(err, "failed to walk schema at root %s", pathParts[0])
return fmt.Errorf("failed to walk schema at root %s: %w", pathParts[0], err)
}
} else {
var err error
matches, err = filepath.Glob(f)
if err != nil {
return errors.Wrapf(err, "failed to glob schema filename %s", f)
return fmt.Errorf("failed to glob schema filename %s: %w", f, err)
}
}

Expand All @@ -174,7 +173,7 @@ func CompleteConfig(config *Config) error {
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
if err != nil {
return errors.Wrap(err, "unable to open schema")
return fmt.Errorf("unable to open schema: %w", err)
}

config.Sources = append(config.Sources, &ast.Source{Name: filename, Input: string(schemaRaw)})
Expand Down Expand Up @@ -343,10 +342,10 @@ func (c *Config) check() error {
fileList := map[string][]FilenamePackage{}

if err := c.Models.Check(); err != nil {
return errors.Wrap(err, "config.models")
return fmt.Errorf("config.models: %w", err)
}
if err := c.Exec.Check(); err != nil {
return errors.Wrap(err, "config.exec")
return fmt.Errorf("config.exec: %w", err)
}
fileList[c.Exec.ImportPath()] = append(fileList[c.Exec.ImportPath()], FilenamePackage{
Filename: c.Exec.Filename,
Expand All @@ -356,7 +355,7 @@ func (c *Config) check() error {

if c.Model.IsDefined() {
if err := c.Model.Check(); err != nil {
return errors.Wrap(err, "config.model")
return fmt.Errorf("config.model: %w", err)
}
fileList[c.Model.ImportPath()] = append(fileList[c.Model.ImportPath()], FilenamePackage{
Filename: c.Model.Filename,
Expand All @@ -366,7 +365,7 @@ func (c *Config) check() error {
}
if c.Resolver.IsDefined() {
if err := c.Resolver.Check(); err != nil {
return errors.Wrap(err, "config.resolver")
return fmt.Errorf("config.resolver: %w", err)
}
fileList[c.Resolver.ImportPath()] = append(fileList[c.Resolver.ImportPath()], FilenamePackage{
Filename: c.Resolver.Filename,
Expand All @@ -376,7 +375,7 @@ func (c *Config) check() error {
}
if c.Federation.IsDefined() {
if err := c.Federation.Check(); err != nil {
return errors.Wrap(err, "config.federation")
return fmt.Errorf("config.federation: %w", err)
}
fileList[c.Federation.ImportPath()] = append(fileList[c.Federation.ImportPath()], FilenamePackage{
Filename: c.Federation.Filename,
Expand Down Expand Up @@ -480,7 +479,7 @@ func inStrSlice(haystack []string, needle string) bool {
func findCfg() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", errors.Wrap(err, "unable to get working dir to findCfg")
return "", fmt.Errorf("unable to get working dir to findCfg: %w", err)
}

cfg := findCfgInDir(dir)
Expand Down
4 changes: 2 additions & 2 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package config

import (
"errors"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2"
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestLoadDefaultConfig(t *testing.T) {
require.NoError(t, err)

cfg, err = LoadDefaultConfig()
require.True(t, os.IsNotExist(errors.Cause(err)))
require.True(t, os.IsNotExist(errors.Unwrap(err)))
})
}

Expand Down
7 changes: 3 additions & 4 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"sort"

"github.com/pkg/errors"
"github.com/vektah/gqlparser/v2/ast"

"github.com/99designs/gqlgen/codegen/config"
Expand Down Expand Up @@ -67,22 +66,22 @@ func BuildData(cfg *config.Config) (*Data, error) {
case ast.Object:
obj, err := b.buildObject(schemaType)
if err != nil {
return nil, errors.Wrap(err, "unable to build object definition")
return nil, fmt.Errorf("unable to build object definition: %w", err)
}

s.Objects = append(s.Objects, obj)
case ast.InputObject:
input, err := b.buildObject(schemaType)
if err != nil {
return nil, errors.Wrap(err, "unable to build input definition")
return nil, fmt.Errorf("unable to build input definition: %w", err)
}

s.Inputs = append(s.Inputs, input)

case ast.Union, ast.Interface:
s.Interfaces[schemaType.Name], err = b.buildInterface(schemaType)
if err != nil {
return nil, errors.Wrap(err, "unable to bind to interface")
return nil, fmt.Errorf("unable to bind to interface: %w", err)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions codegen/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/99designs/gqlgen/codegen/templates"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/v2/ast"
)

Expand Down Expand Up @@ -52,7 +51,7 @@ func (b *builder) buildDirectives() (map[string]*Directive, error) {

for name, dir := range b.Schema.Directives {
if _, ok := directives[name]; ok {
return nil, errors.Errorf("directive with name %s already exists", name)
return nil, fmt.Errorf("directive with name %s already exists", name)
}

var args []*FieldArgument
Expand All @@ -72,7 +71,7 @@ func (b *builder) buildDirectives() (map[string]*Directive, error) {
var err error
newArg.Default, err = arg.DefaultValue.Value(nil)
if err != nil {
return nil, errors.Errorf("default value for directive argument %s(%s) is not valid: %s", dir.Name, arg.Name, err.Error())
return nil, fmt.Errorf("default value for directive argument %s(%s) is not valid: %w", dir.Name, arg.Name, err)
}
}
args = append(args, newArg)
Expand Down
Loading

0 comments on commit a991e3e

Please sign in to comment.