File size: 731 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package discord

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(strconv.Itoa(r.HTTPResponse.StatusCode))
	buf.WriteString(": ")
	buf.WriteString(http.StatusText(r.HTTPResponse.StatusCode))
	buf.WriteString(", Body: ")
	buf.Write(*r.Body)

	return buf.String()
}