forked from stellar/go
/
error.go
88 lines (70 loc) · 1.95 KB
/
error.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
package horizon
import (
"encoding/json"
"github.com/andskur/go/support/errors"
"github.com/andskur/go/support/render/problem"
"github.com/andskur/go/xdr"
)
func (herr Error) Error() string {
return `Horizon error: "` + herr.Problem.Title + `". Check horizon.Error.Problem for more information.`
}
// ToProblem converts the Prolem to a problem.P
func (prob Problem) ToProblem() problem.P {
extras := make(map[string]interface{})
for k, v := range prob.Extras {
extras[k] = v
}
return problem.P{
Type: prob.Type,
Title: prob.Title,
Status: prob.Status,
Detail: prob.Detail,
Instance: prob.Instance,
Extras: extras,
}
}
// Envelope extracts the transaction envelope that triggered this error from the
// extra fields.
func (herr *Error) Envelope() (*xdr.TransactionEnvelope, error) {
raw, ok := herr.Problem.Extras["envelope_xdr"]
if !ok {
return nil, ErrEnvelopeNotPopulated
}
var b64 string
var result xdr.TransactionEnvelope
err := json.Unmarshal(raw, &b64)
if err != nil {
return nil, errors.Wrap(err, "json decode failed")
}
err = xdr.SafeUnmarshalBase64(b64, &result)
if err != nil {
return nil, errors.Wrap(err, "xdr decode failed")
}
return &result, nil
}
// ResultString extracts the transaction result as a string.
func (herr *Error) ResultString() (string, error) {
raw, ok := herr.Problem.Extras["result_xdr"]
if !ok {
return "", ErrResultNotPopulated
}
var b64 string
err := json.Unmarshal(raw, &b64)
if err != nil {
return "", errors.Wrap(err, "json decode failed")
}
return b64, nil
}
// ResultCodes extracts a result code summary from the error, if possible.
func (herr *Error) ResultCodes() (*TransactionResultCodes, error) {
raw, ok := herr.Problem.Extras["result_codes"]
if !ok {
return nil, ErrResultCodesNotPopulated
}
var result TransactionResultCodes
err := json.Unmarshal(raw, &result)
if err != nil {
return nil, errors.Wrap(err, "json decode failed")
}
return &result, nil
}