File: redirects.go

package info (click to toggle)
golang-github-go-ap-errors 0.0~git20250501.cd50c6a-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 172 kB
  • sloc: makefile: 18
file content (98 lines) | stat: -rw-r--r-- 2,460 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package errors

import (
	"fmt"
	"net/http"
)

func SeeOther(u string) *redirect {
	return &redirect{s: http.StatusSeeOther, u: u}
}
func NewSeeOther(e error, u string) *redirect {
	return &redirect{c: e, s: http.StatusSeeOther, u: u}
}
func Found(u string) *redirect {
	return &redirect{s: http.StatusFound, u: u}
}
func NewFound(e error, u string) *redirect {
	return &redirect{c: e, s: http.StatusFound, u: u}
}
func MovedPermanently(u string) *redirect {
	return &redirect{s: http.StatusMovedPermanently, u: u}
}
func NewMovedPermanently(e error, u string) *redirect {
	return &redirect{c: e, s: http.StatusMovedPermanently, u: u}
}
func NotModified(u string) *redirect {
	return &redirect{s: http.StatusNotModified, u: u}
}
func NewNotModified(e error, u string) *redirect {
	return &redirect{c: e, s: http.StatusNotModified, u: u}
}
func TemporaryRedirect(u string) *redirect {
	return &redirect{s: http.StatusTemporaryRedirect, u: u}
}
func NewTemporaryRedirect(e error, u string) *redirect {
	return &redirect{c: e, s: http.StatusTemporaryRedirect, u: u}
}
func PermanentRedirect(u string) *redirect {
	return &redirect{s: http.StatusPermanentRedirect, u: u}
}
func NewPermanentRedirect(e error, u string) *redirect {
	return &redirect{c: e, s: http.StatusPermanentRedirect, u: u}
}

type redirect struct {
	c error
	u string
	s int
}

func (r redirect) Error() string {
	if r.c == nil {
		return fmt.Sprintf("Redirect %d to %s", r.s, r.u)
	}
	return fmt.Sprintf("Redirect %d to %s: %s", r.s, r.u, r.c)
}

// As is used by the errors.As() function to coerce the method's parameter to the one of the receiver
//
//	if the underlying logic of the receiver's type can understand it.
//
// In this case we're converting a forbidden to its underlying type Err.
func (r *redirect) As(err interface{}) bool {
	switch x := err.(type) {
	case **redirect:
		*x = r
	case *redirect:
		*x = *r
	default:
		return false
	}
	return true
}

func (r redirect) Is(e error) bool {
	rr := redirect{}
	return As(e, &rr) && r.s == rr.s
}

func IsRedirect(e error) bool {
	_, okp := e.(*redirect)
	_, oks := e.(redirect)
	return okp || oks || As(e, &redirect{})
}

func IsNotModified(e error) bool {
	ep, okp := e.(*redirect)
	es, oks := e.(redirect)

	ae := redirect{}
	return (okp && ep.s == http.StatusNotModified) ||
		(oks && es.s == http.StatusNotModified) ||
		(As(e, &ae) && ae.s == http.StatusNotModified)
}

func (r redirect) Unwrap() error {
	return r.Unwrap()
}