This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
forked from rpip/upvest-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
67 lines (57 loc) · 1.92 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
package upvest
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
// ErrorType is represents the allowed values for the error's type.
type ErrorType string
// List of values that ErrorType can take.
const (
ErrInvalidRequest ErrorType = "invalid_request_error"
ErrAuthorization = "authorization_error"
ErrAuthentication = "authentication_error"
ErrDuplicateUser = "duplicate_user"
ErrServer = "server_error"
)
var errorTypes = map[int]ErrorType{
http.StatusBadRequest: ErrInvalidRequest,
http.StatusUnauthorized: ErrAuthorization,
http.StatusForbidden: ErrAuthentication,
http.StatusConflict: ErrDuplicateUser,
http.StatusInternalServerError: ErrServer,
}
// Error represents an error response from the Upvest API server
type Error struct {
Type ErrorType `json:"type,omitempty"`
Message string `json:"message,omitempty"`
StatusCode int `json:"code,omitempty"`
Details map[string]interface{} `json:"details,omitempty"`
URL *url.URL `json:"url,omitempty"`
Header http.Header `json:"header,omitempty"`
}
// httpError supports the error interface
func (aerr *Error) Error() string {
ret, _ := json.Marshal(aerr)
return string(ret)
}
// NewError parses http response and returns Upvest error type
func NewError(resp *http.Response) *Error {
p, _ := ioutil.ReadAll(resp.Body)
var upvestErrorResp map[string]interface{}
_ = json.Unmarshal(p, &upvestErrorResp)
var errorType ErrorType = "server_error"
if err, ok := errorTypes[resp.StatusCode]; ok {
errorType = err
}
// uperror := upvestErrorResp["error"].(map[string]interface{})
return &Error{
Type: errorType,
StatusCode: resp.StatusCode,
Header: resp.Header,
Details: upvestErrorResp,
URL: resp.Request.URL,
//Message: uperror["Message"].(string),
}
}