/
wrap.go
92 lines (78 loc) · 2.68 KB
/
wrap.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
// Copyright 2019 dfuse Platform Inc.
//
// 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 derr
import (
"fmt"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Wrap is a shortcut for `pkgErrors.Wrap` (where `pkgErrors` is `github.com/pkg/errors`)
func Wrap(err error, message string) error {
if se, ok := err.(interface{ GRPCStatus() *status.Status }); ok {
sts := se.GRPCStatus().Proto()
newSts := &spb.Status{
Code: sts.Code,
Message: fmt.Sprintf("%s: %s", message, sts.Message),
Details: sts.Details,
}
return status.ErrorProto(newSts)
}
if err == nil {
return nil
}
return fmt.Errorf(message+": %w", err)
}
// Wrapf is a shortcut for `pkgErrors.Wrapf` (where `pkgErrors` is `github.com/pkg/errors`)
func Wrapf(err error, format string, args ...interface{}) error {
if se, ok := err.(interface{ GRPCStatus() *status.Status }); ok {
sts := se.GRPCStatus().Proto()
newSts := &spb.Status{
Code: sts.Code,
Message: fmt.Sprintf("%s: %s", fmt.Sprintf(format, args...), sts.Message),
Details: sts.Details,
}
return status.ErrorProto(newSts)
}
if err == nil {
return nil
}
return fmt.Errorf(fmt.Sprintf(format, args...)+": %w", err)
}
func WrapCode(code codes.Code, err error, message string) error {
// ici on ajouterait le `Code` précédent dans le `err`qui est un `status.Status` en
// [PreviousCode].
panic("unimplemented")
}
func WrapfCode(code codes.Code, err error, format string, args ...interface{}) error {
panic("unimplemented")
}
func Status(code codes.Code, message string) error {
s := status.New(code, message)
addDebugInfo(s)
return s.Err()
}
func Statusf(code codes.Code, format string, args ...interface{}) error {
s := status.Newf(code, format, args...)
addDebugInfo(s)
return s.Err()
}
func addDebugInfo(s *status.Status) {
// Eventually, use https://godoc.org/github.com/go-stack/stack#Trace
// to stuff in the current call stack (minus 2 levels) into a `errdetails.DebugInfo`
// debug stack.. which will be passed
// In the `DebugInfo.Detail`, we can put the pod name, and other contextual info, like the
// binary program name or something.
return
}