forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
146 lines (123 loc) · 4.76 KB
/
error_test.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package horizonclient
import (
"testing"
"github.com/stellar/go/support/render/problem"
"github.com/stretchr/testify/assert"
)
func TestError_Error(t *testing.T) {
var herr Error
// transaction failed happy path: with the appropriate extra fields
herr = Error{
Problem: problem.P{
Title: "Transaction Failed",
Type: "transaction_failed",
Extras: map[string]interface{}{
"result_codes": map[string]interface{}{
"transaction": "tx_failed",
"operations": []string{"op_underfunded", "op_already_exists"},
},
},
},
}
assert.Equal(t, `horizon error: "Transaction Failed" (tx_failed, op_underfunded, op_already_exists) - check horizon.Error.Problem for more information`, herr.Error())
// transaction failed sad path: missing result_codes extra
herr = Error{
Problem: problem.P{
Title: "Transaction Failed",
Type: "transaction_failed",
Extras: map[string]interface{}{},
},
}
assert.Equal(t, `horizon error: "Transaction Failed" - check horizon.Error.Problem for more information`, herr.Error())
// transaction failed sad path: unparseable result_codes extra
herr = Error{
Problem: problem.P{
Title: "Transaction Failed",
Type: "transaction_failed",
Extras: map[string]interface{}{
"result_codes": "kaboom",
},
},
}
assert.Equal(t, `horizon error: "Transaction Failed" - check horizon.Error.Problem for more information`, herr.Error())
// non-transaction errors
herr = Error{
Problem: problem.P{
Type: "https://stellar.org/horizon-errors/not_found",
Title: "Resource Missing",
Status: 404,
},
}
assert.Equal(t, `horizon error: "Resource Missing" - check horizon.Error.Problem for more information`, herr.Error())
}
func TestError_ResultCodes(t *testing.T) {
var herr Error
// happy path: transaction_failed with the appropriate extra fields
herr.Problem.Type = "transaction_failed"
herr.Problem.Extras = make(map[string]interface{})
herr.Problem.Extras["result_codes"] = map[string]interface{}{
"transaction": "tx_failed",
"operations": []string{"op_underfunded", "op_already_exists"},
}
trc, err := herr.ResultCodes()
if assert.NoError(t, err) {
assert.Equal(t, "tx_failed", trc.TransactionCode)
if assert.Len(t, trc.OperationCodes, 2) {
assert.Equal(t, "op_underfunded", trc.OperationCodes[0])
assert.Equal(t, "op_already_exists", trc.OperationCodes[1])
}
}
// sad path: missing result_codes extra
herr.Problem.Type = "transaction_failed"
herr.Problem.Extras = make(map[string]interface{})
_, err = herr.ResultCodes()
assert.Equal(t, ErrResultCodesNotPopulated, err)
// sad path: unparseable result_codes extra
herr.Problem.Type = "transaction_failed"
herr.Problem.Extras = make(map[string]interface{})
herr.Problem.Extras["result_codes"] = "kaboom"
_, err = herr.ResultCodes()
assert.Error(t, err)
}
func TestError_ResultString(t *testing.T) {
var herr Error
// happy path: transaction_failed with the appropriate extra fields
herr.Problem.Type = "transaction_failed"
herr.Problem.Extras = make(map[string]interface{})
herr.Problem.Extras["result_xdr"] = "AAAAAAAAAMj/////AAAAAgAAAAAAAAAA/////wAAAAAAAAAAAAAAAAAAAAA="
trs, err := herr.ResultString()
if assert.NoError(t, err) {
assert.Equal(t, "AAAAAAAAAMj/////AAAAAgAAAAAAAAAA/////wAAAAAAAAAAAAAAAAAAAAA=", trs)
}
// sad path: missing result_xdr extra
herr.Problem.Type = "transaction_failed"
herr.Problem.Extras = make(map[string]interface{})
_, err = herr.ResultString()
assert.Equal(t, ErrResultNotPopulated, err)
// sad path: unparseable result_xdr extra
herr.Problem.Type = "transaction_failed"
herr.Problem.Extras = make(map[string]interface{})
herr.Problem.Extras["result_xdr"] = 1234
_, err = herr.ResultString()
assert.Error(t, err)
}
func TestError_Envelope(t *testing.T) {
var herr Error
// happy path: transaction_failed with the appropriate extra fields
herr.Problem.Type = "transaction_failed"
herr.Problem.Extras = make(map[string]interface{})
herr.Problem.Extras["envelope_xdr"] = `AAAAADSMMRmQGDH6EJzkgi/7PoKhphMHyNGQgDp2tlS/dhGXAAAAZAAT3TUAAAAwAAAAAAAAAAAAAAABAAAAAAAAAAMAAAABSU5SAAAAAAA0jDEZkBgx+hCc5IIv+z6CoaYTB8jRkIA6drZUv3YRlwAAAAFVU0QAAAAAADSMMRmQGDH6EJzkgi/7PoKhphMHyNGQgDp2tlS/dhGXAAAAAAX14QAAAAAKAAAAAQAAAAAAAAAAAAAAAAAAAAG/dhGXAAAAQLuStfImg0OeeGAQmvLkJSZ1MPSkCzCYNbGqX5oYNuuOqZ5SmWhEsC7uOD9ha4V7KengiwNlc0oMNqBVo22S7gk=`
_, err := herr.Envelope()
assert.NoError(t, err)
// sad path: missing envelope_xdr extra
herr.Problem.Extras = make(map[string]interface{})
_, err = herr.Envelope()
assert.Equal(t, ErrEnvelopeNotPopulated, err)
// sad path: unparseable envelope_xdr extra
herr.Problem.Extras = make(map[string]interface{})
herr.Problem.Extras["envelope_xdr"] = "AAAAADSMMRmQGDH6EJzkgi"
_, err = herr.Envelope()
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "xdr decode")
}
}