From efc3fc0990782b27b6d569a6203456c40c84b1bb Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 10 Dec 2024 21:32:12 -0500 Subject: [PATCH] http: Add an HTTP redirect senteniel error --- http/handler.go | 6 ++++++ http/http.go | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/http/handler.go b/http/handler.go index 2256080..d5bb723 100644 --- a/http/handler.go +++ b/http/handler.go @@ -179,6 +179,12 @@ func (this *Handler) serveDocument ( this.serveError ( res, req, httpError.Status, httpError.Message, false) + return + } + var httpRedirect Redirect + if errors.As(err, &httpRedirect) { + http.Redirect(res, req, httpRedirect.Location, httpRedirect.Status) + return } if err != nil { this.serveError ( diff --git a/http/http.go b/http/http.go index b3b7ab6..f3438f6 100644 --- a/http/http.go +++ b/http/http.go @@ -22,6 +22,19 @@ func (err Error) Error () string { } } +// Redirect, if returned from a template, will cause the server to redirect the +// client to the given location. +type Redirect struct { + Status int + Location string +} + +func (err Redirect) Error () string { + return fmt.Sprintf ( + "%d: %s: %s", + err.Status, http.StatusText(err.Status), err.Location) +} + // HTTPData represents information about an ongoing HTTP request that is made // available to templates as they are being executed. type HTTPData struct {