-
Notifications
You must be signed in to change notification settings - Fork 86
/
errorhandling.go
70 lines (65 loc) · 1.83 KB
/
errorhandling.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
68
69
70
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// JSONRequest do a http request with json content-type
func JSONRequest(url string, structure interface{}) (err error) {
// initialize a http client
client := &http.Client{}
// create a GET request but don't perform yet
var req *http.Request
req, err = http.NewRequest(http.MethodGet, url, nil)
// errors can occurs when create a request
// for example the url protocol can be wrong
if err != nil {
return err
}
// Added content type json
req.Header.Add("Content-Type", "application/json; charset=utf-8")
// perform request and retrieve a response
var resp *http.Response
resp, err = client.Do(req)
if err != nil {
return err
}
// don't forget to close the body
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(structure)
if err != nil {
return err
}
return nil
}
func main() {
// map json response into struct
s := struct {
Origin string `json:"origin"`
URL string `json:"url"`
Args map[string]string `json:"args"`
Headers map[string]string `json:"headers"`
}{}
// Call JsonRequest and verify raised error
switch err := JSONRequest("http://httpbin.org/get", &s).(type) {
// no errors, print Origin(your ip)
case nil:
fmt.Printf("Origin: %s\n", s.Origin)
// there's something wrong
case *url.Error:
fmt.Println("Error on request")
// error while parse json
case *json.SyntaxError:
fmt.Printf("Error while parse json: %q", err)
// possible error on structure mapping
case *json.InvalidUnmarshalError:
fmt.Println("Problem while map json to structure")
// there are many errors that can occurs
default:
fmt.Printf("Unexpected error %T = %+v\n", err, err)
}
// this approach prevent parse error messages.
// Even if the error messages change,
// you continue to capture them correctly
}