Skip to content

Commit

Permalink
Merge pull request #115 from PaulSonOfLars/diffErrorCodes
Browse files Browse the repository at this point in the history
Change exit code behaviour to return debuggable values
  • Loading branch information
yoheimuta authored Nov 30, 2019
2 parents ce0afbb + 750fffb commit 8624b6d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
10 changes: 5 additions & 5 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Do(
switch {
case len(args) == 0:
_, _ = fmt.Fprint(stderr, help)
return osutil.ExitFailure
return osutil.ExitInternalFailure
default:
return doSub(
args,
Expand Down Expand Up @@ -78,18 +78,18 @@ func doLint(
if len(args) < 1 {
_, _ = fmt.Fprintln(stderr, "protolint lint requires at least one argument. See Usage.")
_, _ = fmt.Fprint(stderr, help)
return osutil.ExitFailure
return osutil.ExitInternalFailure
}

flags, err := lint.NewFlags(args)
if err != nil {
_, _ = fmt.Fprint(stderr, err)
return osutil.ExitFailure
return osutil.ExitInternalFailure
}
if len(flags.Args()) < 1 {
_, _ = fmt.Fprintln(stderr, "protolint lint requires at least one argument. See Usage.")
_, _ = fmt.Fprint(stderr, help)
return osutil.ExitFailure
return osutil.ExitInternalFailure
}

subCmd, err := lint.NewCmdLint(
Expand All @@ -99,7 +99,7 @@ func doLint(
)
if err != nil {
_, _ = fmt.Fprint(stderr, err)
return osutil.ExitFailure
return osutil.ExitInternalFailure
}
return subCmd.Run()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/protocgenprotolint/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Do(
subCmd, err := newSubCmd(stdin, stdout, stderr)
if err != nil {
_, _ = fmt.Fprintln(stderr, err)
return osutil.ExitFailure
return osutil.ExitInternalFailure
}
return subCmd.Run()
}
Expand Down
19 changes: 14 additions & 5 deletions internal/cmd/subcmds/lint/cmdLint.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ func (c *CmdLint) Run() osutil.ExitCode {
failures, err := c.run()
if err != nil {
_, _ = fmt.Fprintln(c.stderr, err)
return osutil.ExitFailure
return osutil.ExitInternalFailure
}

err = c.config.reporter.Report(c.output, failures)
if err != nil {
_, _ = fmt.Fprintln(c.stderr, err)
return osutil.ExitFailure
return osutil.ExitInternalFailure
}

if 0 < len(failures) {
return osutil.ExitFailure
return osutil.ExitLintFailure
}

return osutil.ExitSuccess
Expand All @@ -95,6 +95,15 @@ func (c *CmdLint) run() ([]report.Failure, error) {
return allFailures, nil
}

// ParseError represents the error returned through a parsing exception.
type ParseError struct {
Message string
}

func (p ParseError) Error() string {
return p.Message
}

func (c *CmdLint) runOneFile(
f file.ProtoFile,
) ([]report.Failure, error) {
Expand All @@ -111,9 +120,9 @@ func (c *CmdLint) runOneFile(
proto, err := f.Parse(c.config.verbose)
if err != nil {
if c.config.verbose {
return nil, err
return nil, ParseError{Message: err.Error()}
}
return nil, fmt.Errorf("%s. Use -v for more details", err)
return nil, ParseError{Message: fmt.Sprintf("%s. Use -v for more details", err)}
}

return c.l.Run(proto, rs)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/subcmds/list/cmdList.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewCmdList(
func (c *CmdList) Run() osutil.ExitCode {
err := c.run()
if err != nil {
return osutil.ExitFailure
return osutil.ExitInternalFailure
}
return osutil.ExitSuccess
}
Expand Down
5 changes: 3 additions & 2 deletions internal/osutil/exitCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type ExitCode int

// ExitCode constants.
const (
ExitSuccess ExitCode = iota
ExitFailure
ExitSuccess ExitCode = iota
ExitLintFailure // Lint errors, exclusively.
ExitInternalFailure // All other errors: parsing, internal, runtime errors.
)

0 comments on commit 8624b6d

Please sign in to comment.