-
Notifications
You must be signed in to change notification settings - Fork 4
/
error.go
99 lines (78 loc) · 1.81 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package libcmd
import (
"fmt"
)
// parsing: generic parser error
type parserError struct {
arg string
err error
}
func (e parserError) Error() string {
if e.arg != "" {
return fmt.Sprintf("error parsing argument '%s': %v", e.arg, e.err)
}
return fmt.Sprintf("parsing error: %v", e.err)
}
// parsing: unknown argument
type unknownArgErr struct {
arg string
}
func (e unknownArgErr) Error() string {
return fmt.Sprintf("unknown argument: %s", e.arg)
}
// parsing: no value for argument
type noValueErr struct {
arg string
}
func (e noValueErr) Error() string {
return fmt.Sprintf("no value for argument: %s", e.arg)
}
// parsing: conversion error
type conversionErr struct {
value interface{}
typeName string
}
func (e conversionErr) Error() string {
return fmt.Sprintf("'%v' is not a valid %s value", e.value, e.typeName)
}
// parsing: unsupported type
type unsupportedErr struct {
value interface{}
typeName string
}
func (e unsupportedErr) Error() string {
return fmt.Sprintf("unsupported type '%s' for value '%s'", e.typeName, e.value)
}
// parsing: wrong number of operands
type operandRequiredErr struct {
required int
got int
exact bool
}
func (e operandRequiredErr) Error() string {
if e.exact {
return fmt.Sprintf("wrong number of operands, exactly %d required (got %d)", e.required, e.got)
}
return fmt.Sprintf("wrong number of operands, at least %d required (got %d)", e.required, e.got)
}
// IsParserErr returns true is the error is an error
// generated by the parsing process itself.
func IsParserErr(err error) bool {
if err == nil {
return false
}
switch err.(type) {
case parserError:
return true
case unknownArgErr:
return true
case noValueErr:
return true
case conversionErr:
return true
case operandRequiredErr:
return true
default:
return false
}
}