-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
84 lines (68 loc) · 2.71 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
package xtrace
/*
Copyright 2019 Nicholas Krichevsky
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"strings"
"golang.org/x/xerrors"
)
// formatSprinter's pointer type implements the xerrors.Printer interface, but wraps fmt.Sprintf, and will store the
// last errors' error type internally.
type formatSprinter struct {
// The outputs generated by the print methods
messages []string
// Whether or not to get detailed output
detail bool
traceFormatter TraceFormatter
}
// Print takes the output of fmt.Sprint and stores it in output.
func (sprinter *formatSprinter) Print(args ...interface{}) {
message := fmt.Sprint(args...)
sprinter.insertMessage(message)
}
// Print takes the output of fmt.Sprintf and stores it in output.
func (sprinter *formatSprinter) Printf(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
sprinter.insertMessage(message)
}
// Detail will give detailed output as requested by the creator of this type.
func (sprinter *formatSprinter) Detail() bool {
return sprinter.detail
}
// insertMessage inserts the given message with a normalized format.
func (sprinter *formatSprinter) insertMessage(message string) {
formattedMessage := sprinter.traceFormatter.FormatTrace(sprinter.messages, message)
sprinter.messages = append(sprinter.messages, formattedMessage)
}
// output generates the output of a sprinter as a string. If Detail is disabled, only the first message is returned.
func (sprinter *formatSprinter) output() string {
// Just return the message without joining
if len(sprinter.messages) == 1 {
return sprinter.messages[0]
}
return strings.Join(sprinter.messages, "")
}
// generateErrorString will produce the result of the given xerrors.Formatter with/without detail, as requested.
// If the given error does not implement xerrors.Formatter, will return err.Error() instead
func generateErrorString(err error, traceFormatter TraceFormatter, detail bool) string {
formatter, isFormatter := err.(xerrors.Formatter)
if !isFormatter {
return traceFormatter.FormatTrace(nil, err.Error())
}
sprinter := &formatSprinter{
detail: detail,
traceFormatter: traceFormatter,
}
formatter.FormatError(sprinter)
return sprinter.output()
}