Debian Codegraph @

errs/errs.go

raw symbols SCIP
1 package errs
2
3 import (
4 "fmt"
5 "os"
6 "strings"
7
8 "github.com/pkg/errors"
9 "github.com/urfave/cli"
10 )
11
12 // NewError returns a new Error for the given format and arguments
13 func NewError(format string, args ...interface{}) error {
14 return errors.Errorf(format, args...)
15 }
16
17 // NewExitError returns an error that the urfave/cli package will handle and
18 // will show the given error and exit with the given code.
19 func NewExitError(err error, exitCode int) error {
20 return cli.NewExitError(err, exitCode)
21 }
22
23 // Wrap returns a new error wrapped by the given error with the given message.
24 // If the given error implements the errors.Cause interface, the base error is
25 // used. If the given error is wrapped by a package name, the error wrapped
26 // will be the string after the last colon.
27 func Wrap(err error, format string, args ...interface{}) error {
28 if err == nil {
29 return nil
30 }
31 cause := errors.Cause(err)
32 if cause == err {
33 str := err.Error()
34 if i := strings.LastIndexByte(str, ':'); i >= 0 {
35 str = strings.TrimSpace(str[i:])
36 return errors.Wrapf(fmt.Errorf(str), format, args...)
37 }
38 }
39 return errors.Wrapf(cause, format, args...)
40 }
41
42 // InsecureCommand returns an error with a message saying that the current
43 // command requires the insecure flag.
44 func InsecureCommand(ctx *cli.Context) error {
45 return errors.Errorf("'%s %s' requires the '--insecure' flag", ctx.App.Name, ctx.Command.Name)
46 }
47
48 // EqualArguments returns an error saying that the given positional arguments
49 // cannot be equal.
50 func EqualArguments(ctx *cli.Context, arg1, arg2 string) error {
51 return errors.Errorf("positional arguments <%s> and <%s> cannot be equal in '%s'", arg1, arg2, usage(ctx))
52 }
53
54 // MissingArguments returns an error with a missing arguments message for the
55 // given positional argument names.
56 func MissingArguments(ctx *cli.Context, argNames ...string) error {
57 switch len(argNames) {
58 case 0:
59 return errors.Errorf("missing positional arguments in '%s'", usage(ctx))
60 case 1:
61 return errors.Errorf("missing positional argument <%s> in '%s'", argNames[0], usage(ctx))
62 default:
63 args := make([]string, len(argNames))
64 for i, name := range argNames {
65 args[i] = "<" + name + ">"
66 }
67 return errors.Errorf("missing positional argument %s in '%s'", strings.Join(args, " "), usage(ctx))
68 }
69 }
70
71 // NumberOfArguments returns nil if the number of positional arguments is
72 // equal to the required one. It will return an appropriate error if they are
73 // not.
74 func NumberOfArguments(ctx *cli.Context, required int) error {
75 n := ctx.NArg()
76 switch {
77 case n < required:
78 return TooFewArguments(ctx)
79 case n > required:
80 return TooManyArguments(ctx)
81 default:
82 return nil
83 }
84 }
85
86 // MinMaxNumberOfArguments returns nil if the number of positional arguments
87 // between the min/max range. It will return an appropriate error if they are
88 // not.
89 func MinMaxNumberOfArguments(ctx *cli.Context, min, max int) error {
90 n := ctx.NArg()
91 switch {
92 case n < min:
93 return TooFewArguments(ctx)
94 case n > max:
95 return TooManyArguments(ctx)
96 default:
97 return nil
98 }
99 }
100
101 // TooFewArguments returns an error with a few arguments were provided message.
102 func TooFewArguments(ctx *cli.Context) error {
103 return errors.Errorf("not enough positional arguments were provided in '%s'", usage(ctx))
104 }
105
106 // TooManyArguments returns an error with a too many arguments were provided
107 // message.
108 func TooManyArguments(ctx *cli.Context) error {
109 return errors.Errorf("too many positional arguments were provided in '%s'", usage(ctx))
110 }
111
112 // InsecureArgument returns an error with the given argument requiring the
113 // --insecure flag.
114 func InsecureArgument(ctx *cli.Context, name string) error {
115 return errors.Errorf("positional argument <%s> requires the '--insecure' flag", name)
116 }
117
118 // FlagValueInsecure returns an error with the given flag and value requiring
119 // the --insecure flag.
120 func FlagValueInsecure(ctx *cli.Context, flag string, value string) error {
121 return errors.Errorf("flag '--%s %s' requires the '--insecure' flag", flag, value)
122 }
123
124 // InvalidFlagValue returns an error with the given value being missing or
125 // invalid for the given flag. Optionally it lists the given formatted options
126 // at the end.
127 func InvalidFlagValue(ctx *cli.Context, flag string, value string, options string) error {
128 var format string
129 if len(value) == 0 {
130 format = fmt.Sprintf("missing value for flag '--%s'", flag)
131 } else {
132 format = fmt.Sprintf("invalid value '%s' for flag '--%s'", value, flag)
133 }
134
135 if len(options) == 0 {
136 return errors.New(format)
137 }
138
139 return errors.New(format + "; options are " + options)
140 }
141
142 // InvalidFlagValueMsg returns an error with the given value being missing or
143 // invalid for the given flag. Optionally it returns an error message to aid
144 // in debugging.
145 func InvalidFlagValueMsg(ctx *cli.Context, flag string, value string, msg string) error {
146 var format string
147 if len(value) == 0 {
148 format = fmt.Sprintf("missing value for flag '--%s'", flag)
149 } else {
150 format = fmt.Sprintf("invalid value '%s' for flag '--%s'", value, flag)
151 }
152
153 if len(msg) == 0 {
154 return errors.New(format)
155 }
156
157 return errors.New(format + "; " + msg)
158 }
159
160 // IncompatibleFlag returns an error with the flag being incompatible with the
161 // given value.
162 func IncompatibleFlag(ctx *cli.Context, flag string, value string) error {
163 return errors.Errorf("flag '--%s' is incompatible with '%s'", flag, value)
164 }
165
166 // IncompatibleFlagWithFlag returns an error with the flag being incompatible with the
167 // given value.
168 func IncompatibleFlagWithFlag(ctx *cli.Context, flag string, withFlag string) error {
169 return errors.Errorf("flag '--%s' is incompatible with '--%s'", flag, withFlag)
170 }
171
172 // IncompatibleFlagValue returns an error with the flag being incompatible with the
173 // given value.
174 func IncompatibleFlagValue(ctx *cli.Context, flag, incompatibleWith,
175 incompatibleWithValue string) error {
176 return errors.Errorf("flag '--%s' is incompatible with flag '--%s %s'",
177 flag, incompatibleWith, incompatibleWithValue)
178 }
179
180 // IncompatibleFlagValues returns an error with the flag being incompatible with the
181 // given value.
182 func IncompatibleFlagValues(ctx *cli.Context, flag, value, incompatibleWith,
183 incompatibleWithValue string) error {
184 return errors.Errorf("flag '--%s %s' is incompatible with flag '--%s %s'",
185 flag, value, incompatibleWith, incompatibleWithValue)
186 }
187
188 // IncompatibleFlagValueWithFlagValue returns an error with the given value
189 // being missing or invalid for the given flag. Optionally it lists the given
190 // formatted options at the end.
191 func IncompatibleFlagValueWithFlagValue(ctx *cli.Context, flag string, value string,
192 withFlag string, withValue, options string) error {
193 format := fmt.Sprintf("flag '--%s %s' is incompatible with flag '--%s %s'",
194 flag, value, withFlag, withValue)
195
196 if len(options) == 0 {
197 return errors.New(format)
198 }
199
200 return errors.Errorf("%s\n\n Option(s): --%s %s", format, withFlag, options)
201 }
202
203 // RequiredFlag returns an error with the required flag message.
204 func RequiredFlag(ctx *cli.Context, flag string) error {
205 return errors.Errorf("'%s %s' requires the '--%s' flag", ctx.App.HelpName,
206 ctx.Command.Name, flag)
207 }
208
209 // RequiredWithFlag returns an error with the required flag message with another flag.
210 func RequiredWithFlag(ctx *cli.Context, flag, required string) error {
211 return errors.Errorf("flag '--%s' requires the '--%s' flag", flag, required)
212 }
213
214 // RequiredWithFlagValue returns an error with the required flag message.
215 func RequiredWithFlagValue(ctx *cli.Context, flag, value, required string) error {
216 return errors.Errorf("'--%s %s' requires the '--%s' flag", flag, value, required)
217 }
218
219 // RequiredWithProvisionerTypeFlag returns an error with the required flag message.
220 func RequiredWithProvisionerTypeFlag(ctx *cli.Context, provisionerType, required string) error {
221 return errors.Errorf("provisioner type '%s' requires the '--%s' flag", provisionerType, required)
222 }
223
224 // RequiredInsecureFlag returns an error with the given flag requiring the
225 // insecure flag message.
226 func RequiredInsecureFlag(ctx *cli.Context, flag string) error {
227 return errors.Errorf("flag '--%s' requires the '--insecure' flag", flag)
228 }
229
230 // RequiredSubtleFlag returns an error with the given flag requiring the
231 // subtle flag message..
232 func RequiredSubtleFlag(ctx *cli.Context, flag string) error {
233 return errors.Errorf("flag '--%s' requires the '--subtle' flag", flag)
234 }
235
236 // RequiredUnlessInsecureFlag returns an error with the required flag message unless
237 // the insecure flag is used.
238 func RequiredUnlessInsecureFlag(ctx *cli.Context, flag string) error {
239 return errors.Errorf("flag '--%s' is required unless the '--insecure' flag is provided", flag)
240 }
241
242 // RequiredUnlessFlag returns an error with the required flag message unless
243 // the specified flag is used.
244 func RequiredUnlessFlag(ctx *cli.Context, flag, unlessFlag string) error {
245 return errors.Errorf("flag '--%s' is required unless the '--%s' flag is provided", flag, unlessFlag)
246 }
247
248 // RequiredUnlessSubtleFlag returns an error with the required flag message unless
249 // the subtle flag is used.
250 func RequiredUnlessSubtleFlag(ctx *cli.Context, flag string) error {
251 return errors.Errorf("flag '--%s' is required unless the '--subtle' flag is provided", flag)
252 }
253
254 // RequiredOrFlag returns an error with a list of flags being required messages.
255 func RequiredOrFlag(ctx *cli.Context, flags ...string) error {
256 params := make([]string, len(flags))
257 for i, flag := range flags {
258 params[i] = "--" + flag
259 }
260 return errors.Errorf("one of flag %s is required", strings.Join(params, " or "))
261 }
262
263 // RequiredWithOrFlag returns an error with a list of flags at least one of which
264 // is required in conjunction with the last flag in the list.
265 func RequiredWithOrFlag(ctx *cli.Context, withFlag string, flags ...string) error {
266 params := make([]string, len(flags))
267 for i := 0; i < len(flags); i++ {
268 params[i] = "--" + flags[i]
269 }
270 return errors.Errorf("one of flag %s is required with flag --%s", strings.Join(params, " or "), withFlag)
271 }
272
273 // MinSizeFlag returns an error with a greater or equal message message for
274 // the given flag and size.
275 func MinSizeFlag(ctx *cli.Context, flag string, size string) error {
276 return errors.Errorf("flag '--%s' must be greater or equal than %s", flag, size)
277 }
278
279 // MinSizeInsecureFlag returns an error with a requiring --insecure flag
280 // message with the given flag an size.
281 func MinSizeInsecureFlag(ctx *cli.Context, flag, size string) error {
282 return errors.Errorf("flag '--%s' requires at least %s unless '--insecure' flag is provided", flag, size)
283 }
284
285 // MutuallyExclusiveFlags returns an error with mutually exclusive message for
286 // the given flags.
287 func MutuallyExclusiveFlags(ctx *cli.Context, flag1, flag2 string) error {
288 return errors.Errorf("flag '--%s' and flag '--%s' are mutually exclusive", flag1, flag2)
289 }
290
291 // UnsupportedFlag returns an error with a message saying that the given flag is
292 // not yet supported.
293 func UnsupportedFlag(ctx *cli.Context, flag string) error {
294 return errors.Errorf("flag '--%s' is not yet supported", flag)
295 }
296
297 // usage returns the command usage text if set or a default usage string.
298 func usage(ctx *cli.Context) string {
299 if len(ctx.Command.UsageText) == 0 {
300 return fmt.Sprintf("%s %s [command options]", ctx.App.HelpName, ctx.Command.Name)
301 }
302 // keep just the first line and remove markdown
303 lines := strings.Split(ctx.Command.UsageText, "\n")
304 return strings.Replace(lines[0], "**", "", -1)
305 }
306
307 // FileError is a wrapper for errors of the os package.
308 func FileError(err error, filename string) error {
309 if err == nil {
310 return nil
311 }
312 switch e := err.(type) {
313 case *os.PathError:
314 return errors.Errorf("%s %s failed: %v", e.Op, e.Path, e.Err)
315 case *os.LinkError:
316 return errors.Errorf("%s %s %s failed: %v", e.Op, e.Old, e.New, e.Err)
317 case *os.SyscallError:
318 return errors.Errorf("%s failed: %v", e.Syscall, e.Err)
319 default:
320 return Wrap(err, "unexpected error on %s", filename)
321 }
322 }
323
324 // FriendlyError is an interface for returning friendly error messages to the user.
325 type FriendlyError interface {
326 Message() string
327 }