Skip to content

Commit

Permalink
error types
Browse files Browse the repository at this point in the history
  • Loading branch information
jrperritt committed Jun 5, 2015
1 parent f30ee85 commit cd1597f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions util/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package util

import (
"fmt"
"os"

"github.com/codegangsta/cli"
)

// PrintError is used for printing information when an error is encountered.
func PrintError(c *cli.Context, err error) {
w := c.App.Writer
switch err.(type) {
case ErrMissingFlag, ErrFlagFormatting:
fmt.Fprintf(w, "%s", err.Error())
fmt.Fprintf(w, "Usage: %s\n", c.Command.Usage)
}
os.Exit(1)
}

// ErrMissingFlag is used when a user doesn't provide a required flag.
type ErrMissingFlag struct {
Msg string
}

func (e ErrMissingFlag) Error() string {
return fmt.Sprintf("Missing flag: %s\n", e.Msg)
}

// ErrFlagFormatting is used when a flag's format is invalid.
type ErrFlagFormatting struct {
Msg string
}

func (e ErrFlagFormatting) Error() string {
return fmt.Sprintf("Invalid flag formatting: %s\n", e.Msg)
}

0 comments on commit cd1597f

Please sign in to comment.