Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
64 lines (53 loc) · 1.5 KB
/
Copy patherrors.go
File metadata and controls
64 lines (53 loc) · 1.5 KB
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
// Copyright © Paul Tötterman <paul.totterman@gmail.com>. All rights reserved.
package main
import (
"log/slog"
"net/http"
)
// Error is a constant error type. Nice to compare and wrap.
type Error string
// Error implements the error interface.
func (e Error) Error() string {
return string(e)
}
// Sentinel errors.
const (
ErrFailedRollback Error = "failed rollback"
ErrInvalidIP Error = "invalid IP"
ErrInvalidURL Error = "invalid URL"
ErrMissingName Error = "missing name"
ErrMissingURL Error = "missing URL"
ErrMissingUser Error = "missing user"
ErrNoTx Error = "no tx"
ErrUnknown Error = "unknown error"
ErrURLNameConflict Error = "URL name conflict"
)
// HTTPError is an error returned over the network.
type HTTPError struct {
Code int
Err error
Message string
}
// Error implements the error interface.
func (e *HTTPError) Error() string {
return e.Message
}
// ServeHTTP implements http.Handler.
func (e *HTTPError) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.polish()
//nolint:gosec // Structured slog fields; request data is logged as values, not interpolated log lines.
slog.Error("error",
slog.String("method", r.Method),
slog.String("url", r.RequestURI),
slog.Int("status", e.Code),
slog.String("remote", r.RemoteAddr),
slog.String("message", e.Message),
slog.Any("err", e.Err),
)
http.Error(w, e.Message, e.Code)
}
func (e *HTTPError) polish() {
if e.Message == "" {
e.Message = http.StatusText(e.Code)
}
}