-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhttp.go
More file actions
57 lines (46 loc) · 1.19 KB
/
http.go
File metadata and controls
57 lines (46 loc) · 1.19 KB
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
package create
import (
"encoding/json"
"errors"
"fmt"
"io"
"github.com/google/go-github/v68/github"
)
func handleResponseError(response *github.Response) error {
if err := response.Body.Close(); err != nil {
return err
}
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
var errorResponse github.ErrorResponse
err = json.Unmarshal(body, &errorResponse)
if err != nil {
return err
}
if response.StatusCode == 400 {
return errors.New(errorResponse.Message)
}
if response.StatusCode == 422 {
for _, e := range errorResponse.Errors {
switch e.Code {
case "missing":
return errors.New("the requested milestone does not exist")
case "missing_field":
return fmt.Errorf("the required field %q has not been set", e.Field)
case "invalid":
return fmt.Errorf("the content set in the field %q is invalid", e.Field)
case "already_exists":
return errors.New("the milestone with the same title already exists")
case "unprocessable":
return errors.New("the data sent was unprocessable")
case "custom":
return errors.New(e.Message)
default:
return errors.New("unknown error")
}
}
}
return errors.New(errorResponse.Message)
}