/
op_error.go
71 lines (63 loc) · 1.67 KB
/
op_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
// Copyright (c) 2021 Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package ctxerr
import (
"bytes"
"fmt"
"reflect"
)
// OpBadUsage returns a string to notice the user he passed a bad
// parameter to an operator constructor.
func OpBadUsage(op, usage string, param interface{}, pos int, kind bool) *Error {
var b bytes.Buffer
fmt.Fprintf(&b, "usage: %s%s, but received ", op, usage)
if param == nil {
b.WriteString("nil")
} else {
t := reflect.TypeOf(param)
if kind && t.String() != t.Kind().String() {
fmt.Fprintf(&b, "%s (%s)", t, t.Kind())
} else {
b.WriteString(t.String())
}
}
b.WriteString(" as ")
switch pos {
case 1:
b.WriteString("1st")
case 2:
b.WriteString("2nd")
case 3:
b.WriteString("3rd")
default:
fmt.Fprintf(&b, "%dth", pos)
}
b.WriteString(" parameter")
return &Error{
Message: "bad usage of " + op + " operator",
Summary: NewSummary(b.String()),
}
}
// OpTooManyParams returns an *Error to notice the user he called a
// variadic operator constructor with too many parameters.
func OpTooManyParams(op, usage string) *Error {
return &Error{
Message: "bad usage of " + op + " operator",
Summary: NewSummary("usage: " + op + usage + ", too many parameters"),
}
}
// OpBad returns an *Error to notice the user a bad operator
// constructor usage. If len(args) is > 0, s and args are given to
// fmt.Sprintf.
func OpBad(op, s string, args ...interface{}) *Error {
if len(args) > 0 {
s = fmt.Sprintf(s, args...)
}
return &Error{
Message: "bad usage of " + op + " operator",
Summary: NewSummary(s),
}
}