PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/error.go

https://github.com/szaydel/go-flags
Go | 95 lines | 47 code | 24 blank | 24 comment | 1 complexity | fee9df84c37f46325ee7a3bb0979b74f MD5 | raw file
 1package flags
 2
 3import (
 4	"fmt"
 5	"reflect"
 6)
 7
 8// ErrorType represents the type of error.
 9type ErrorType uint
10
11const (
12	// ErrUnknown indicates a generic error.
13	ErrUnknown ErrorType = iota
14
15	// ErrExpectedArgument indicates that an argument was expected.
16	ErrExpectedArgument
17
18	// ErrUnknownFlag indicates an unknown flag.
19	ErrUnknownFlag
20
21	// ErrUnknownGroup indicates an unknown group.
22	ErrUnknownGroup
23
24	// ErrMarshal indicates a marshalling error while converting values.
25	ErrMarshal
26
27	// ErrHelp indicates that the built-in help was shown (the error
28	// contains the help message).
29	ErrHelp
30
31	// ErrNoArgumentForBool indicates that an argument was given for a
32	// boolean flag (which don't not take any arguments).
33	ErrNoArgumentForBool
34
35	// ErrRequired indicates that a required flag was not provided.
36	ErrRequired
37
38	// ErrShortNameTooLong indicates that a short flag name was specified,
39	// longer than one character.
40	ErrShortNameTooLong
41
42	// ErrDuplicatedFlag indicates that a short or long flag has been
43	// defined more than once
44	ErrDuplicatedFlag
45
46	// ErrTag indicates an error while parsing flag tags.
47	ErrTag
48
49	// ErrCommandRequired indicates that a command was required but not
50	// specified
51	ErrCommandRequired
52
53	// ErrUnknownCommand indicates that an unknown command was specified.
54	ErrUnknownCommand
55)
56
57func (e ErrorType) String() string {
58	return reflect.TypeOf(e).Name()
59}
60
61// Error represents a parser error. The error returned from Parse is of this
62// type. The error contains both a Type and Message.
63type Error struct {
64	// The type of error
65	Type ErrorType
66
67	// The error message
68	Message string
69}
70
71// Error returns the error's message
72func (e *Error) Error() string {
73	return e.Message
74}
75
76func newError(tp ErrorType, message string) *Error {
77	return &Error{
78		Type:    tp,
79		Message: message,
80	}
81}
82
83func newErrorf(tp ErrorType, format string, args ...interface{}) *Error {
84	return newError(tp, fmt.Sprintf(format, args...))
85}
86
87func wrapError(err error) *Error {
88	ret, ok := err.(*Error)
89
90	if !ok {
91		return newError(ErrUnknown, err.Error())
92	}
93
94	return ret
95}