[WIP] Initial commit

This commit is contained in:
eleksir
2025-05-08 18:12:52 +03:00
commit 5379ae6eb3
1516 changed files with 648142 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
// 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.
//
// Detail is shown:
// - via `errors.GetSafeDetails()`
// - when formatting with `%+v`.
// - in Sentry reports.
// - via `errors.GetAllHints()` / `errors.FlattenHints()`
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.
//
// Detail is shown:
// - via `errors.GetSafeDetails()`
// - when formatting with `%+v`.
// - in Sentry reports.
// - via `errors.GetAllHints()` / `errors.FlattenHints()`
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
}

View File

@@ -0,0 +1,87 @@
// 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 (
"bytes"
"context"
"fmt"
"github.com/cockroachdb/errors/errbase"
"github.com/cockroachdb/redact"
"github.com/gogo/protobuf/proto"
)
type unimplementedError struct {
// For now, msg is non-reportable.
msg string
IssueLink
}
var _ error = (*unimplementedError)(nil)
var _ fmt.Formatter = (*unimplementedError)(nil)
var _ errbase.SafeFormatter = (*unimplementedError)(nil)
var _ errbase.SafeDetailer = (*unimplementedError)(nil)
func (w *unimplementedError) Error() string { return w.msg }
func (w *unimplementedError) SafeDetails() []string {
return []string{w.IssueURL, w.Detail}
}
// ErrorHint implements the hintdetail.ErrorHinter interface.
func (w *unimplementedError) ErrorHint() string {
var hintText bytes.Buffer
hintText.WriteString(UnimplementedErrorHint)
maybeAppendReferral(&hintText, w.IssueLink)
return hintText.String()
}
// UnimplementedErrorHint is the hint emitted upon unimplemented errors.
const UnimplementedErrorHint = `You have attempted to use a feature that is not yet implemented.`
func (w *unimplementedError) Format(s fmt.State, verb rune) { errbase.FormatError(w, s, verb) }
func (w *unimplementedError) SafeFormatError(p errbase.Printer) error {
// For now, msg is non-reportable.
p.Print(w.msg)
if p.Detail() {
// But the details are.
p.Printf("unimplemented")
if w.IssueURL != "" {
p.Printf("\nissue: %s", redact.Safe(w.IssueURL))
}
if w.Detail != "" {
p.Printf("\ndetail: %s", redact.Safe(w.Detail))
}
}
return nil
}
func decodeUnimplementedError(
_ context.Context, msg string, details []string, _ proto.Message,
) error {
var issueLink IssueLink
if len(details) > 0 {
issueLink.IssueURL = details[0]
}
if len(details) > 1 {
issueLink.Detail = details[1]
}
return &unimplementedError{msg: msg, IssueLink: issueLink}
}
func init() {
errbase.RegisterLeafDecoder(errbase.GetTypeKey((*unimplementedError)(nil)), decodeUnimplementedError)
}

View File

@@ -0,0 +1,97 @@
// 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 (
"bytes"
"context"
"fmt"
"github.com/cockroachdb/errors/errbase"
"github.com/cockroachdb/errors/stdstrings"
"github.com/cockroachdb/redact"
"github.com/gogo/protobuf/proto"
)
type withIssueLink struct {
cause error
IssueLink
}
var _ error = (*withIssueLink)(nil)
var _ errbase.SafeDetailer = (*withIssueLink)(nil)
var _ fmt.Formatter = (*withIssueLink)(nil)
var _ errbase.SafeFormatter = (*withIssueLink)(nil)
func (w *withIssueLink) Error() string { return w.cause.Error() }
func (w *withIssueLink) Cause() error { return w.cause }
func (w *withIssueLink) Unwrap() error { return w.cause }
func (w *withIssueLink) SafeDetails() []string {
return []string{w.IssueURL, w.Detail}
}
// ErrorHint implements the hintdetail.ErrorHinter interface.
func (w *withIssueLink) ErrorHint() string {
var hintText bytes.Buffer
maybeAppendReferral(&hintText, w.IssueLink)
return hintText.String()
}
func maybeAppendReferral(buf *bytes.Buffer, link IssueLink) {
if link.IssueURL != "" {
// If there is a URL, refer to that.
if buf.Len() > 0 {
buf.WriteByte('\n')
}
fmt.Fprintf(buf, "See: %s", link.IssueURL)
} else {
// No URL: tell the user to send details.
buf.WriteString(stdstrings.IssueReferral)
}
}
func (w *withIssueLink) Format(s fmt.State, verb rune) { errbase.FormatError(w, s, verb) }
func (w *withIssueLink) SafeFormatError(p errbase.Printer) error {
if p.Detail() {
sep := redact.SafeString("")
if w.IssueURL != "" {
p.Printf("issue: %s", redact.Safe(w.IssueURL))
sep = "\n"
}
if w.Detail != "" {
p.Printf("%sdetail: %s", sep, redact.Safe(w.Detail))
}
}
return w.cause
}
func decodeWithIssueLink(
_ context.Context, cause error, _ string, details []string, _ proto.Message,
) error {
var issueLink IssueLink
if len(details) > 0 {
issueLink.IssueURL = details[0]
}
if len(details) > 1 {
issueLink.Detail = details[1]
}
return &withIssueLink{cause: cause, IssueLink: issueLink}
}
func init() {
errbase.RegisterWrapperDecoder(errbase.GetTypeKey((*withIssueLink)(nil)), decodeWithIssueLink)
}