-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_error.go
54 lines (44 loc) · 1.24 KB
/
http_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
package httperror
import (
"net/http"
"github.com/TosinShada/stellar-core/clients/horizonclient"
"github.com/TosinShada/stellar-core/support/errors"
"github.com/TosinShada/stellar-core/support/render/httpjson"
)
type Error struct {
ErrorMessage string `json:"error"`
Status int `json:"-"`
}
func (h *Error) Error() string {
return h.ErrorMessage
}
func NewHTTPError(status int, errorMessage string) *Error {
return &Error{
ErrorMessage: errorMessage,
Status: status,
}
}
func (e *Error) Render(w http.ResponseWriter) {
httpjson.RenderStatus(w, e.Status, e, httpjson.JSON)
}
var InternalServer = &Error{
ErrorMessage: "An error occurred while processing this request.",
Status: http.StatusInternalServerError,
}
var BadRequest = &Error{
ErrorMessage: "The request was invalid in some way.",
Status: http.StatusBadRequest,
}
func ParseHorizonError(err error) error {
if err == nil {
return nil
}
rootErr := errors.Cause(err)
if hError := horizonclient.GetError(rootErr); hError != nil {
resultCode, _ := hError.ResultCodes()
err = errors.Wrapf(err, "error submitting transaction: %+v, %+v\n", hError.Problem, resultCode)
} else {
err = errors.Wrap(err, "error submitting transaction")
}
return err
}