-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherror.go
51 lines (43 loc) · 1002 Bytes
/
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
package typeform
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const (
CodeAuthenticationFailed = "AUTHENTICATION_FAILED"
CodeFormNotFound = "FORM_NOT_FOUND"
CodeImageNotFound = "IMAGE_NOT_FOUND"
CodeThemeNotFound = "THEME_NOT_FOUND"
CodeWorkspaceNotFound = "WORKSPACE_NOT_FOUND"
)
type Error struct {
URL string
Method string
StatusCode int
Code string `json:"code"`
Description string `json:"description"`
}
func newAPIError(req *http.Request, res *http.Response) error {
apiError := Error{
URL: req.URL.String(),
Method: req.Method,
StatusCode: res.StatusCode,
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
err = json.Unmarshal(b, &apiError)
if err != nil {
return err
}
return apiError
}
func (e Error) Error() string {
return fmt.Sprintf(
"url: %s, method: %s, statusCode: %d, code: %s, description: %s",
e.URL, e.Method, e.StatusCode, e.Code, e.Description,
)
}