Skip to content

Commit 7ec2bcf

Browse files
authored
Merge pull request #613 from k1LoW/change-errors
Use github.com/k1LoW/errors
2 parents b7b6535 + ed003a9 commit 7ec2bcf

File tree

31 files changed

+202
-118
lines changed

31 files changed

+202
-118
lines changed

cmd/completion.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"os"
2727
"path/filepath"
2828

29-
"github.com/pkg/errors"
29+
"github.com/k1LoW/errors"
3030
"github.com/spf13/cobra"
3131
)
3232

@@ -51,7 +51,7 @@ tbls completion fish ~/.config/fish/completions/tbls.fish
5151
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
5252
Args: func(cmd *cobra.Command, args []string) error {
5353
if len(args) != 1 {
54-
return errors.Errorf("accepts 1 arg, received %d", len(args))
54+
return fmt.Errorf("accepts 1 arg, received %d", len(args))
5555
}
5656
if err := cobra.OnlyValidArgs(cmd, args); err != nil {
5757
return err

cmd/coverage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import (
2626
"fmt"
2727
"os"
2828

29+
"github.com/k1LoW/errors"
2930
"github.com/k1LoW/tbls/cmdutil"
3031
"github.com/k1LoW/tbls/config"
3132
"github.com/k1LoW/tbls/coverage"
3233
"github.com/labstack/gommon/color"
3334
"github.com/mattn/go-runewidth"
34-
"github.com/pkg/errors"
3535
"github.com/spf13/cobra"
3636
)
3737

cmd/doc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import (
2626
"path/filepath"
2727
"strings"
2828

29+
"github.com/k1LoW/errors"
2930
"github.com/k1LoW/tbls/cmdutil"
3031
"github.com/k1LoW/tbls/config"
3132
"github.com/k1LoW/tbls/datasource"
3233
"github.com/k1LoW/tbls/output/gviz"
3334
"github.com/k1LoW/tbls/output/json"
3435
"github.com/k1LoW/tbls/output/md"
3536
"github.com/k1LoW/tbls/schema"
36-
"github.com/pkg/errors"
3737
"github.com/spf13/cobra"
3838
)
3939

cmd/lint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import (
2525
"os"
2626
"reflect"
2727

28+
"github.com/k1LoW/errors"
2829
"github.com/k1LoW/tbls/cmdutil"
2930
"github.com/k1LoW/tbls/config"
3031
"github.com/k1LoW/tbls/datasource"
3132
"github.com/labstack/gommon/color"
32-
"github.com/pkg/errors"
3333
"github.com/spf13/cobra"
3434
)
3535

cmd/out.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
package cmd
2222

2323
import (
24+
"fmt"
2425
"io"
2526
"os"
2627

28+
"github.com/k1LoW/errors"
2729
"github.com/k1LoW/tbls/cmdutil"
2830
"github.com/k1LoW/tbls/config"
2931
"github.com/k1LoW/tbls/output"
@@ -36,7 +38,6 @@ import (
3638
"github.com/k1LoW/tbls/output/plantuml"
3739
"github.com/k1LoW/tbls/output/xlsx"
3840
"github.com/k1LoW/tbls/output/yaml"
39-
"github.com/pkg/errors"
4041
"github.com/spf13/cobra"
4142
)
4243

@@ -102,7 +103,7 @@ var outCmd = &cobra.Command{
102103
case "config":
103104
o = tbls_config.New(c)
104105
default:
105-
return errors.Errorf("unsupported format '%s'", format)
106+
return fmt.Errorf("unsupported format '%s'", format)
106107
}
107108

108109
var wr io.Writer

cmd/root.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"strconv"
3030
"strings"
3131

32+
"github.com/k1LoW/errors"
3233
"github.com/k1LoW/tbls/cmdutil"
3334
"github.com/k1LoW/tbls/config"
3435
"github.com/k1LoW/tbls/output/json"
@@ -271,7 +272,7 @@ func printError(err error) {
271272
env := os.Getenv("DEBUG")
272273
debug, _ := strconv.ParseBool(env)
273274
if env != "" && debug {
274-
fmt.Printf("%+v\n", err)
275+
fmt.Println(err, errors.StackTraces(err))
275276
} else {
276277
fmt.Println(err)
277278
}

cmdutil/when.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/expr-lang/expr"
88
"github.com/expr-lang/expr/ast"
9-
"github.com/pkg/errors"
9+
"github.com/k1LoW/errors"
1010
)
1111

1212
// AST walker which replaces `$IDENTIFIER` with `Env.IDENTIFIER` member lookup expressions.

config/config.go

+36-25
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99

1010
"github.com/aquasecurity/go-version/pkg/version"
1111
"github.com/goccy/go-yaml"
12+
"github.com/k1LoW/errors"
1213
"github.com/k1LoW/expand"
1314
"github.com/k1LoW/tbls/dict"
1415
"github.com/k1LoW/tbls/schema"
1516
ver "github.com/k1LoW/tbls/version"
1617
"github.com/minio/pkg/wildcard"
17-
"github.com/pkg/errors"
1818
"github.com/samber/lo"
1919
)
2020

@@ -356,12 +356,15 @@ func (c *Config) LoadEnviron() error {
356356
}
357357

358358
// LoadConfigFile load config file
359-
func (c *Config) LoadConfigFile(path string) error {
359+
func (c *Config) LoadConfigFile(path string) (err error) {
360+
defer func() {
361+
err = errors.WithStack(err)
362+
}()
360363
if path == "" && os.Getenv("TBLS_DSN") == "" {
361364
for _, p := range DefaultConfigFilePaths {
362365
if f, err := os.Stat(filepath.Join(c.root, p)); err == nil && !f.IsDir() {
363366
if path != "" {
364-
return errors.Errorf("duplicate config file [%s, %s]", path, p)
367+
return fmt.Errorf("duplicate config file [%s, %s]", path, p)
365368
}
366369
path = p
367370
}
@@ -373,23 +376,25 @@ func (c *Config) LoadConfigFile(path string) error {
373376

374377
fullPath, err := filepath.Abs(path)
375378
if err != nil {
376-
return errors.Wrap(errors.WithStack(err), "failed to load config file")
379+
return fmt.Errorf("failed to load config file: %w", err)
377380
}
378381

379382
buf, err := os.ReadFile(filepath.Clean(fullPath))
380383
if err != nil {
381-
return errors.Wrap(errors.WithStack(err), "failed to load config file")
384+
return fmt.Errorf("failed to load config file: %w", err)
382385
}
383386
c.Path = filepath.Clean(fullPath)
384387

385388
return c.LoadConfig(buf)
386389
}
387390

388391
// LoadConfig load config from []byte
389-
func (c *Config) LoadConfig(in []byte) error {
390-
err := yaml.Unmarshal(expand.ExpandenvYAMLBytes(in), c)
391-
if err != nil {
392-
return errors.Wrap(errors.WithStack(err), "failed to load config file")
392+
func (c *Config) LoadConfig(in []byte) (err error) {
393+
defer func() {
394+
err = errors.WithStack(err)
395+
}()
396+
if err := yaml.Unmarshal(expand.ExpandenvYAMLBytes(in), c); err != nil {
397+
return fmt.Errorf("failed to load config file: %w", err)
393398
}
394399
c.MergedDict.Merge(c.Dict.Dump())
395400
return nil
@@ -611,15 +616,18 @@ func (c *Config) detectShowColumnsForER(s *schema.Schema) error {
611616
return nil
612617
}
613618

614-
func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) error {
619+
func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) (err error) {
620+
defer func() {
621+
err = errors.WithStack(err)
622+
}()
615623
for _, r := range relations {
616624
c, err := schema.ToCardinality(r.Cardinality)
617625
if err != nil {
618-
return errors.Wrap(err, "failed to add relation")
626+
return fmt.Errorf("failed to add relation: %w", err)
619627
}
620628
pc, err := schema.ToCardinality(r.ParentCardinality)
621629
if err != nil {
622-
return errors.Wrap(err, "failed to add relation")
630+
return fmt.Errorf("failed to add relation: %w", err)
623631
}
624632
relation := &schema.Relation{
625633
Cardinality: c,
@@ -633,24 +641,24 @@ func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation)
633641
}
634642
relation.Table, err = s.FindTableByName(r.Table)
635643
if err != nil {
636-
return errors.Wrap(err, "failed to add relation")
644+
return fmt.Errorf("failed to add relation: %w", err)
637645
}
638646
for _, c := range r.Columns {
639647
column, err := relation.Table.FindColumnByName(c)
640648
if err != nil {
641-
return errors.Wrap(err, "failed to add relation")
649+
return fmt.Errorf("failed to add relation: %w", err)
642650
}
643651
relation.Columns = append(relation.Columns, column)
644652
column.ParentRelations = append(column.ParentRelations, relation)
645653
}
646654
relation.ParentTable, err = s.FindTableByName(r.ParentTable)
647655
if err != nil {
648-
return errors.Wrap(err, "failed to add relation")
656+
return fmt.Errorf("failed to add relation: %w", err)
649657
}
650658
for _, c := range r.ParentColumns {
651659
column, err := relation.ParentTable.FindColumnByName(c)
652660
if err != nil {
653-
return errors.Wrap(err, "failed to add relation")
661+
return fmt.Errorf("failed to add relation: %w", err)
654662
}
655663
relation.ParentColumns = append(relation.ParentColumns, column)
656664
column.ChildRelations = append(column.ChildRelations, relation)
@@ -665,11 +673,11 @@ func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation)
665673
cr.Def = r.Def
666674
cr.Cardinality, err = schema.ToCardinality(r.Cardinality)
667675
if err != nil {
668-
return errors.Wrap(err, "failed to add relation")
676+
return fmt.Errorf("failed to add relation: %w", err)
669677
}
670678
cr.ParentCardinality, err = schema.ToCardinality(r.ParentCardinality)
671679
if err != nil {
672-
return errors.Wrap(err, "failed to add relation")
680+
return fmt.Errorf("failed to add relation: %w", err)
673681
}
674682
}
675683
} else {
@@ -679,11 +687,14 @@ func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation)
679687
return nil
680688
}
681689

682-
func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) error {
690+
func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) (err error) {
691+
defer func() {
692+
err = errors.WithStack(err)
693+
}()
683694
for _, c := range comments {
684695
table, err := s.FindTableByName(c.Table)
685696
if err != nil {
686-
return errors.Wrap(err, "failed to add table comment")
697+
return fmt.Errorf("failed to add table comment: %w", err)
687698
}
688699
if c.TableComment != "" {
689700
table.Comment = c.TableComment
@@ -696,14 +707,14 @@ func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) err
696707
for c, comment := range c.ColumnComments {
697708
column, err := table.FindColumnByName(c)
698709
if err != nil {
699-
return errors.Wrap(err, "failed to add column comment")
710+
return fmt.Errorf("failed to add column comment: %w", err)
700711
}
701712
column.Comment = comment
702713
}
703714
for c, labels := range c.ColumnLabels {
704715
column, err := table.FindColumnByName(c)
705716
if err != nil {
706-
return errors.Wrap(err, "failed to add column comment")
717+
return fmt.Errorf("failed to add column comment: %w", err)
707718
}
708719
for _, l := range labels {
709720
column.Labels = column.Labels.Merge(l)
@@ -712,21 +723,21 @@ func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) err
712723
for i, comment := range c.IndexComments {
713724
index, err := table.FindIndexByName(i)
714725
if err != nil {
715-
return errors.Wrap(err, "failed to add index comment")
726+
return fmt.Errorf("failed to add index comment: %w", err)
716727
}
717728
index.Comment = comment
718729
}
719730
for c, comment := range c.ConstraintComments {
720731
constraint, err := table.FindConstraintByName(c)
721732
if err != nil {
722-
return errors.Wrap(err, "failed to add constraint comment")
733+
return fmt.Errorf("failed to add constraint comment: %w", err)
723734
}
724735
constraint.Comment = comment
725736
}
726737
for t, comment := range c.TriggerComments {
727738
trigger, err := table.FindTriggerByName(t)
728739
if err != nil {
729-
return errors.Wrap(err, "failed to add trigger comment")
740+
return fmt.Errorf("failed to add trigger comment: %w", err)
730741
}
731742
trigger.Comment = comment
732743
}

0 commit comments

Comments
 (0)