forked from cockroachdb/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
74 lines (64 loc) · 1.96 KB
/
message.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
// 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 errutil
import (
"context"
"fmt"
"github.com/cockroachdb/errors/errbase"
"github.com/gogo/protobuf/proto"
)
// WithMessage annotates err with a new message.
// If err is nil, WithMessage returns nil.
func WithMessage(err error, message string) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: message,
}
}
// WithMessagef annotates err with the format specifier.
// If err is nil, WithMessagef returns nil.
func WithMessagef(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
}
type withMessage struct {
cause error
msg string
}
func (w *withMessage) Error() string { return fmt.Sprintf("%s: %v", w.msg, w.cause) }
func (w *withMessage) Cause() error { return w.cause }
func (w *withMessage) Unwrap() error { return w.cause }
func (w *withMessage) Format(s fmt.State, verb rune) { errbase.FormatError(w, s, verb) }
func (w *withMessage) FormatError(p errbase.Printer) (next error) {
p.Print(w.msg)
return w.cause
}
// A message wrapper is decoded exactly.
func decodeMessage(
ctx context.Context, cause error, msg string, _ []string, _ proto.Message,
) error {
return &withMessage{cause: cause, msg: msg}
}
func init() {
tn := errbase.GetTypeKey((*withMessage)(nil))
errbase.RegisterWrapperDecoder(tn, decodeMessage)
}