File: errors.go

package info (click to toggle)
golang-github-koofr-go-httpclient 0.0~git20190818.e0dc8fd-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 160 kB
  • sloc: makefile: 2
file content (38 lines) | stat: -rw-r--r-- 853 bytes parent folder | download
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 httpclient

import (
	"errors"
	"fmt"
	"net/http"
)

type InvalidStatusError struct {
	Expected []int
	Got      int
	Headers  http.Header
	Content  string
}

func (e InvalidStatusError) Error() string {
	return fmt.Sprintf("Invalid response status! Got %d, expected %d; headers: %s, content: %s", e.Got, e.Expected, e.Headers, e.Content)
}

func IsInvalidStatusError(err error) (invalidStatusError *InvalidStatusError, ok bool) {
	if ise, ok := err.(InvalidStatusError); ok {
		return &ise, true
	} else if ise, ok := err.(*InvalidStatusError); ok {
		return ise, true
	} else {
		return nil, false
	}
}

func IsInvalidStatusCode(err error, statusCode int) bool {
	if ise, ok := IsInvalidStatusError(err); ok {
		return ise.Got == statusCode
	} else {
		return false
	}
}

var RateLimitTimeoutError = errors.New("HTTPClient rate limit timeout")