cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
cmd/cloudflared/cliutil/errors.go
37lines · modecode
| 1 | package cliutil |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/urfave/cli/v2" |
| 6 | ) |
| 7 | |
| 8 | type usageError string |
| 9 | |
| 10 | func (ue usageError) Error() string { |
| 11 | return string(ue) |
| 12 | } |
| 13 | |
| 14 | func UsageError(format string, args ...interface{}) error { |
| 15 | if len(args) == 0 { |
| 16 | return usageError(format) |
| 17 | } else { |
| 18 | msg := fmt.Sprintf(format, args...) |
| 19 | return usageError(msg) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // Ensures exit with error code if actionFunc returns an error |
| 24 | func ErrorHandler(actionFunc cli.ActionFunc) cli.ActionFunc { |
| 25 | return func(ctx *cli.Context) error { |
| 26 | err := actionFunc(ctx) |
| 27 | if err != nil { |
| 28 | if _, ok := err.(usageError); ok { |
| 29 | msg := fmt.Sprintf("%s\nSee 'cloudflared %s --help'.", err.Error(), ctx.Command.FullName()) |
| 30 | err = cli.Exit(msg, -1) |
| 31 | } else if _, ok := err.(cli.ExitCoder); !ok { |
| 32 | err = cli.Exit(err.Error(), 1) |
| 33 | } |
| 34 | } |
| 35 | return err |
| 36 | } |
| 37 | } |
| 38 | |