-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_codes.go
82 lines (73 loc) · 2.39 KB
/
http_codes.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
71
72
73
74
75
76
77
78
79
80
81
82
package networkfile
import (
"errors"
"fmt"
"io"
"net/http"
)
const (
HTTPCodeEOF = 480
HTTPCodeUnexpectedEOF = 481
HTTPCodeShortBuffer = 482
HTTPCodeShortWrite = 483
HTTPCodeClosedPipe = 484
HTTPCodeNoProgress = 486
HTTPCodeUnknownError = 490
HTTPCodeUnsupportedOperation = 491
)
var (
ErrUnsupportedOperation = errors.New("unsupported operation")
ErrUnauthorized = errors.New("unauthorized: wrong secret key")
ErrUnknownFile = errors.New("not found: unknown file")
HTTPCodeToErr = map[int]error{
http.StatusUnauthorized: ErrUnauthorized,
http.StatusNotFound: ErrUnknownFile,
HTTPCodeEOF: io.EOF,
HTTPCodeUnexpectedEOF: io.ErrUnexpectedEOF,
HTTPCodeShortBuffer: io.ErrShortBuffer,
HTTPCodeShortWrite: io.ErrShortWrite,
HTTPCodeClosedPipe: io.ErrClosedPipe,
HTTPCodeNoProgress: io.ErrNoProgress,
HTTPCodeUnsupportedOperation: ErrUnsupportedOperation,
}
errToHTTPCode = map[error]int{
ErrUnauthorized: http.StatusUnauthorized,
ErrUnknownFile: http.StatusNotFound,
io.EOF: HTTPCodeEOF,
io.ErrUnexpectedEOF: HTTPCodeUnexpectedEOF,
io.ErrShortBuffer: HTTPCodeShortBuffer,
io.ErrShortWrite: HTTPCodeShortWrite,
io.ErrClosedPipe: HTTPCodeClosedPipe,
io.ErrNoProgress: HTTPCodeNoProgress,
ErrUnsupportedOperation: HTTPCodeUnsupportedOperation,
}
)
// writeErrorToResponseWriter writes the given error with appropriate status code to the HTTP writer.
func writeErrorToResponseWriter(rw http.ResponseWriter, err error) {
code, ok := errToHTTPCode[err]
if !ok {
rw.WriteHeader(HTTPCodeUnknownError)
_, _ = rw.Write([]byte(err.Error()))
return
}
rw.WriteHeader(code)
}
// responseCodeToError returns the right error from the given response
func responseCodeToError(resp *http.Response, expected int) error {
if resp.StatusCode == expected {
return nil
}
err, ok := HTTPCodeToErr[resp.StatusCode]
if ok {
return err
}
buf := make([]byte, 512)
n, err := resp.Body.Read(buf)
if err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf(
"%d: an unknown error occurred but the error could not be determined because: %w",
resp.StatusCode, err,
)
}
return fmt.Errorf("%d: an unknown error occurred: %s", resp.StatusCode, string(buf[:n]))
}