Skip to content
File

Blob: spec/rpc/error.go

go41 lines
1package rpc
2 
3import (
4 "fmt"
5 
6 "go.miragespace.co/specter/spec/chord"
7 
8 "github.com/twitchtv/twirp"
9)
10 
11func GetErrorMeta(err twirp.Error) (cause, kv string) {
12 cause = err.Meta("cause")
13 kv = err.Meta("kv")
14 return
15}
16 
17func WrapError(err error) error {
18 var code twirp.ErrorCode
19 if chord.ErrorIsRetryable(err) {
20 code = twirp.FailedPrecondition
21 } else {
22 code = twirp.Internal
23 }
24 twerr := twirp.NewError(code, err.Error())
25 twerr = twerr.WithMeta("cause", fmt.Sprintf("%T", err)) // to easily tell apart wrapped internal errors from explicit ones
26 return twirp.WrapError(twerr, err)
27}
28 
29func WrapErrorKV(key string, err error) error {
30 var code twirp.ErrorCode
31 if chord.ErrorIsRetryable(err) {
32 code = twirp.FailedPrecondition
33 } else {
34 code = twirp.Internal
35 }
36 twerr := twirp.NewError(code, err.Error())
37 twerr = twerr.WithMeta("cause", fmt.Sprintf("%T", err)) // to easily tell apart wrapped internal errors from explicit ones
38 twerr = twerr.WithMeta("kv", key)
39 return twirp.WrapError(twerr, err)
40}