-
Notifications
You must be signed in to change notification settings - Fork 66
/
issuelink.go
107 lines (94 loc) · 3.13 KB
/
issuelink.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
100
101
102
103
104
105
106
107
// Copyright 2019 The Cockroach Authors.
//
// 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.
package issuelink
import (
"fmt"
"github.com/cockroachdb/errors/errbase"
"github.com/cockroachdb/errors/markers"
)
// WithIssueLink adds an annotation to a know issue
// on a web issue tracker.
//
// The url and detail strings may contain PII and will
// be considered reportable.
func WithIssueLink(err error, issue IssueLink) error {
if err == nil {
return nil
}
return &withIssueLink{cause: err, IssueLink: issue}
}
// HasIssueLink returns true iff the error or one of its
// causes has a linked issue payload.
func HasIssueLink(err error) bool {
_, ok := markers.If(err, func(err error) (v interface{}, ok bool) {
v, ok = err.(*withIssueLink)
return
})
return ok
}
// IsIssueLink returns true iff the error (not its
// causes) has a linked issue payload.
func IsIssueLink(err error) bool {
_, ok := err.(*withIssueLink)
return ok
}
// GetAllIssueLinks retrieves the linked issue carried
// by the error or its direct causes.
func GetAllIssueLinks(err error) (issues []IssueLink) {
for ; err != nil; err = errbase.UnwrapOnce(err) {
if issue, ok := GetIssueLink(err); ok {
issues = append(issues, issue)
}
}
return
}
// UnimplementedError creates a new leaf error that indicates that
// some feature was not (yet) implemented.
func UnimplementedError(issueLink IssueLink, msg string) error {
return &unimplementedError{IssueLink: issueLink, msg: msg}
}
// UnimplementedErrorf creates a new leaf error that indicates that
// some feature was not (yet) implemented. The message is formatted.
func UnimplementedErrorf(issueLink IssueLink, format string, args ...interface{}) error {
return &unimplementedError{IssueLink: issueLink, msg: fmt.Sprintf(format, args...)}
}
// IsUnimplementedError returns iff if err is an unimplemented error.
func IsUnimplementedError(err error) bool {
_, ok := err.(*unimplementedError)
return ok
}
// HasUnimplementedError returns iff if err or its cause is an
// unimplemented error.
func HasUnimplementedError(err error) bool {
_, ok := errbase.UnwrapAll(err).(*unimplementedError)
return ok
}
// GetIssueLink retrieves the linked issue annotation carried
// by the error, or false if there is no such annotation.
func GetIssueLink(err error) (IssueLink, bool) {
switch w := err.(type) {
case *withIssueLink:
return w.IssueLink, true
case *unimplementedError:
return w.IssueLink, true
}
return IssueLink{}, false
}
// IssueLink is the payload for a linked issue annotation.
type IssueLink struct {
// URL to the issue on a tracker.
IssueURL string
// Annotation that characterizes a sub-issue.
Detail string
}