Skip to content

Commit a16c3fc

Browse files
committed
complete some reviews
1 parent 04e2509 commit a16c3fc

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

error.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ var (
1212
ErrGracefulTimeout = stdErrors.New("shutdown: graceful timeout has been reached, exiting")
1313
)
1414

15+
// Fiber redirection errors
16+
var (
17+
ErrRedirectBackNoFallback = NewError(StatusInternalServerError, "Referer not found, you have to enter fallback URL for redirection.")
18+
)
19+
1520
// Range errors
1621
var (
1722
ErrRangeMalformed = stdErrors.New("range: malformed range header string")

helpers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,6 @@ const (
675675
ConstraintDatetime = "datetime"
676676
ConstraintRegex = "regex"
677677
)
678+
679+
// Cookie name to send flash messages when to use redirection.
680+
const FlashCookieName = "fiber_flash"

redirect.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,19 @@ func (r *Redirect) Route(name string, config ...RedirectConfig) error {
184184
}
185185

186186
// Redirect back to the URL to referer.
187-
// TODO: Should fallback be optional?
188-
func (r *Redirect) Back(fallback string) error {
187+
func (r *Redirect) Back(fallback ...string) error {
189188
location := r.c.Get(HeaderReferer)
190189
if location == "" {
191-
location = fallback
190+
// Check fallback URL
191+
if len(fallback) == 0 {
192+
err := ErrRedirectBackNoFallback
193+
r.c.Status(err.Code)
194+
195+
return err
196+
}
197+
location = fallback[0]
192198
}
199+
193200
return r.To(location)
194201
}
195202

redirect_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ func Test_Redirect_Back(t *testing.T) {
134134
c.Redirect().Back("/")
135135
require.Equal(t, 302, c.Response().StatusCode())
136136
require.Equal(t, "/", string(c.Response().Header.Peek(HeaderLocation)))
137+
138+
err := c.Redirect().Back()
139+
require.Equal(t, 500, c.Response().StatusCode())
140+
require.ErrorAs(t, ErrRedirectBackNoFallback, &err)
137141
}
138142

139143
// go test -run Test_Redirect_Back_WithReferer

0 commit comments

Comments
 (0)