-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_pattern.go
More file actions
57 lines (52 loc) · 1.33 KB
/
error_pattern.go
File metadata and controls
57 lines (52 loc) · 1.33 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
package pages
import (
"errors"
"net/http"
)
func ErrorPattern(authorizer PageAuthorizer, decoratorStrategy PageDecoratorStrategy) ErrorPatternFunc {
if authorizer == nil {
panic("error pattern: authorizer is required")
}
if decoratorStrategy == nil {
panic("error pattern: decorator strategy is required")
}
return func(r *http.Request, status int, err error) string {
e := err
// Walk wrapped errors to honor custom pattern providers at any depth.
// Keep the original err for Pattern(*http.Request, int, error).
Loop:
for {
switch t := e.(type) {
case interface{ Pattern() string }:
return t.Pattern()
case interface {
Pattern(*http.Request, int, error) string
}:
return t.Pattern(r, status, err)
case interface{ Unwrap() error }:
e = t.Unwrap()
continue
default:
break Loop
}
}
if errors.Is(err, ErrPageNotFound) &&
decoratorStrategy.IsURIDecorable(r.Context(), r.URL.Path) &&
authorizer.Authorize(r.Context(), CreatePage) == Allow {
return PageInternalCreate
}
switch status {
case http.StatusUnauthorized:
return PageErrorUnauthorized
case http.StatusForbidden:
return PageErrorForbidden
case http.StatusNotFound:
return PageErrorNotFound
default:
if status >= 400 && status < 500 {
return PageError4xx
}
return PageError5xx
}
}
}