...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package errors
17
18 import (
19 "encoding/json"
20 "fmt"
21 "reflect"
22
23 "golang.org/x/xerrors"
24 )
25
26 func toJsonOrFallbackToError(origin error) string {
27 out, err := json.Marshal(origin)
28 if err != nil {
29 if wrapper, ok := err.(xerrors.Wrapper); ok {
30 return fmt.Sprintf(
31 "failed to jsonify error on type %s, json error: %s; origin error message: %s",
32 reflect.TypeOf(origin).Name(),
33 err,
34 wrapper.Unwrap().Error(),
35 )
36 }
37 return fmt.Sprintf(
38 "failed to jsonify error on type %s, json error, %s; also failed to Unwrap() on it.",
39 reflect.TypeOf(origin).Name(),
40 err,
41 )
42
43 }
44 return string(out)
45 }
46