Skip to content

Commit 4c817fa

Browse files
author
Artur Kh
committed
Move error handling to util.go
1 parent 9b18b9b commit 4c817fa

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

authorizations.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"io/ioutil"
98
"net/http"
109
"strings"
11-
"time"
1210
)
1311

1412
type AuthorizationRequest struct {
@@ -18,15 +16,13 @@ type AuthorizationRequest struct {
1816
}
1917

2018
type AuthorizationResponse struct {
21-
Id int
22-
URL string
2319
Token string
2420
}
2521

2622
func Authorize(username, password string) (string, error) {
2723
payload := AuthorizationRequest{
2824
[]string{"gist"},
29-
fmt.Sprintf("go-gist (%d)", time.Now().Unix()),
25+
"go-gist", //fmt.Sprintf("go-gist (%d)", time.Now().Unix()),
3026
"https://github.com/khrt/go-gist",
3127
}
3228

@@ -70,22 +66,7 @@ func Authorize(username, password string) (string, error) {
7066
}
7167

7268
if resp.StatusCode != 201 {
73-
var e map[string]json.RawMessage
74-
if err := json.Unmarshal(body, &e); err != nil {
75-
return "", err
76-
}
77-
78-
message := string(e["message"])
79-
80-
if e["errors"] != nil {
81-
var ee []map[string]string
82-
if err := json.Unmarshal(e["errors"], &ee); err != nil {
83-
return "", err
84-
}
85-
message += fmt.Sprintf(" (%s %s)", ee[0]["resource"], ee[0]["code"])
86-
}
87-
88-
return "", errors.New(message)
69+
return "", GistParseError(body)
8970
}
9071

9172
var auth AuthorizationResponse

gist.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
7-
"fmt"
86
"io/ioutil"
97
"net/http"
108
)
@@ -134,11 +132,7 @@ func doRequest(req *http.Request) ([]byte, error) {
134132
}
135133

136134
if resp.StatusCode != 200 && resp.StatusCode != 201 {
137-
var f interface{}
138-
if err := json.Unmarshal(body, &f); err != nil {
139-
return nil, err
140-
}
141-
return nil, errors.New(fmt.Sprintf("%v", f.(map[string]interface{})["message"]))
135+
return nil, GistParseError(body)
142136
}
143137

144138
return body, nil

go-gist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func main() {
3939
}
4040

4141
if err != nil {
42-
fmt.Fprintf(os.Stderr, "error: %v\n", err)
42+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
4343
os.Exit(1)
4444
}
4545
}

util.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"strings"
8+
)
9+
10+
func GistParseError(body []byte) error {
11+
var err map[string]json.RawMessage
12+
if err := json.Unmarshal(body, &err); err != nil {
13+
return err
14+
}
15+
16+
message := string(err["message"])
17+
18+
if err["errors"] != nil {
19+
var errs []map[string]string
20+
if err := json.Unmarshal(err["errors"], &errs); err != nil {
21+
return err
22+
}
23+
24+
var messages []string
25+
for _, m := range errs {
26+
messages = append(messages, fmt.Sprintf("%s %s", m["resource"], m["code"]))
27+
}
28+
message += " (" + strings.Join(messages, ",") + ")"
29+
}
30+
31+
return errors.New(message)
32+
}

0 commit comments

Comments
 (0)