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