generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.go
38 lines (33 loc) · 858 Bytes
/
response.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
package mtnmomo
import (
"bytes"
"errors"
"net/http"
"strconv"
)
// Response captures the http response
type Response struct {
HTTPResponse *http.Response
Body *[]byte
}
// Error ensures that the response can be decoded into a string inc ase it's an error response
func (r *Response) Error() error {
switch r.HTTPResponse.StatusCode {
case 200, 201, 202, 204, 205:
return nil
default:
return errors.New(r.errorMessage())
}
}
func (r *Response) errorMessage() string {
var buf bytes.Buffer
buf.WriteString("URL: ")
buf.WriteString(r.HTTPResponse.Request.URL.String())
buf.WriteString(", StatusCode: ")
buf.WriteString(strconv.Itoa(r.HTTPResponse.StatusCode))
buf.WriteString(", StatusText: ")
buf.WriteString(http.StatusText(r.HTTPResponse.StatusCode))
buf.WriteString(", Body: ")
buf.Write(*r.Body)
return buf.String()
}