-
Notifications
You must be signed in to change notification settings - Fork 9
/
rpcerrors.go
93 lines (76 loc) · 1.88 KB
/
rpcerrors.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package rpcerrors
import (
"fmt"
"runtime"
"github.com/go-errors/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
type Error struct {
Err error
s *status.Status
stack []uintptr
frames []errors.StackFrame
}
func Wrap(code codes.Code, err error) *Error {
return WrapWithSkip(code, err, 1)
}
func WrapWithSkip(code codes.Code, err error, skip int) *Error {
stack := make([]uintptr, errors.MaxStackDepth)
length := runtime.Callers(1+skip, stack[:])
return &Error{
Err: err,
s: status.New(code, err.Error()),
stack: stack[:length],
}
}
func Errorf(code codes.Code, format string, args ...interface{}) *Error {
stack := make([]uintptr, errors.MaxStackDepth)
length := runtime.Callers(2, stack[:])
err := fmt.Errorf(format, args...)
return &Error{
Err: err,
s: status.New(code, err.Error()),
stack: stack[:length],
}
}
func (e *Error) Error() string {
return e.Err.Error()
}
func (e *Error) Unwrap() error {
return e.Err
}
func (e *Error) GRPCStatus() *status.Status {
return e.s
}
func (e *Error) WithDetails(details ...proto.Message) *Error {
if e.s.Code() == codes.OK {
return e
}
p := e.s.Proto()
for _, detail := range details {
any, _ := anypb.New(detail)
if any != nil {
p.Details = append(p.Details, any)
}
}
return &Error{
Err: e.Err,
s: status.FromProto(p),
stack: e.stack,
}
}
func (err *Error) StackFrames() []errors.StackFrame {
if err.frames == nil {
err.frames = make([]errors.StackFrame, len(err.stack))
for i, pc := range err.stack {
err.frames[i] = errors.NewStackFrame(pc)
}
}
return err.frames
}