Debian Codegraph @

redirects.go

raw symbols SCIP
1 package errors
2
3 import (
4 "fmt"
5 "net/http"
6 )
7
8 func SeeOther(u string) *redirect {
9 return &redirect{s: http.StatusSeeOther, u: u}
10 }
11 func NewSeeOther(e error, u string) *redirect {
12 return &redirect{c: e, s: http.StatusSeeOther, u: u}
13 }
14 func Found(u string) *redirect {
15 return &redirect{s: http.StatusFound, u: u}
16 }
17 func NewFound(e error, u string) *redirect {
18 return &redirect{c: e, s: http.StatusFound, u: u}
19 }
20 func MovedPermanently(u string) *redirect {
21 return &redirect{s: http.StatusMovedPermanently, u: u}
22 }
23 func NewMovedPermanently(e error, u string) *redirect {
24 return &redirect{c: e, s: http.StatusMovedPermanently, u: u}
25 }
26 func NotModified(u string) *redirect {
27 return &redirect{s: http.StatusNotModified, u: u}
28 }
29 func NewNotModified(e error, u string) *redirect {
30 return &redirect{c: e, s: http.StatusNotModified, u: u}
31 }
32 func TemporaryRedirect(u string) *redirect {
33 return &redirect{s: http.StatusTemporaryRedirect, u: u}
34 }
35 func NewTemporaryRedirect(e error, u string) *redirect {
36 return &redirect{c: e, s: http.StatusTemporaryRedirect, u: u}
37 }
38 func PermanentRedirect(u string) *redirect {
39 return &redirect{s: http.StatusPermanentRedirect, u: u}
40 }
41 func NewPermanentRedirect(e error, u string) *redirect {
42 return &redirect{c: e, s: http.StatusPermanentRedirect, u: u}
43 }
44
45 type redirect struct {
46 c error
47 u string
48 s int
49 }
50
51 func (r redirect) Error() string {
52 if r.c == nil {
53 return fmt.Sprintf("Redirect %d to %s", r.s, r.u)
54 }
55 return fmt.Sprintf("Redirect %d to %s: %s", r.s, r.u, r.c)
56 }
57
58 // As is used by the errors.As() function to coerce the method's parameter to the one of the receiver
59 //
60 // if the underlying logic of the receiver's type can understand it.
61 //
62 // In this case we're converting a forbidden to its underlying type Err.
63 func (r *redirect) As(err interface{}) bool {
64 switch x := err.(type) {
65 case **redirect:
66 *x = r
67 case *redirect:
68 *x = *r
69 default:
70 return false
71 }
72 return true
73 }
74
75 func (r redirect) Is(e error) bool {
76 rr := redirect{}
77 return As(e, &rr) && r.s == rr.s
78 }
79
80 func IsRedirect(e error) bool {
81 _, okp := e.(*redirect)
82 _, oks := e.(redirect)
83 return okp || oks || As(e, &redirect{})
84 }
85
86 func IsNotModified(e error) bool {
87 ep, okp := e.(*redirect)
88 es, oks := e.(redirect)
89
90 ae := redirect{}
91 return (okp && ep.s == http.StatusNotModified) ||
92 (oks && es.s == http.StatusNotModified) ||
93 (As(e, &ae) && ae.s == http.StatusNotModified)
94 }
95
96 func (r redirect) Unwrap() error {
97 return r.c
98 }