Skip to content

Commit d5de75a

Browse files
committed
Added ctx.NotFound
1 parent dfd1f2b commit d5de75a

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

web.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@ func (ctx *Context) Abort(status int, body string) {
5555
}
5656

5757
func (ctx *Context) Redirect(status int, url string) {
58-
//note := "<a href=\"%v\">" + statusText[code] + "</a>.\n"
59-
6058
ctx.SetHeader("Location", url, true)
6159
ctx.StartResponse(status)
62-
ctx.WriteString("")
60+
ctx.WriteString("Redirecting to: " + url)
61+
}
62+
63+
func (ctx *Context) NotFound(message string) {
64+
ctx.StartResponse(404)
65+
ctx.WriteString(message)
6366
}
67+
6468
//Sets a cookie -- duration is the amount of time in seconds. 0 = forever
6569
func (ctx *Context) SetCookie(name string, value string, age int64) {
6670
if age == 0 {

web_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ func init() {
111111
return ""
112112
})
113113

114+
Get("/error/notfound/(.*)", func(ctx *Context, message string) { ctx.NotFound(message) })
115+
114116
Post("/posterror/code/(.*)/(.*)", func(ctx *Context, code string, message string) string {
115117
n, _ := strconv.Atoi(code)
116118
ctx.Abort(n, message)
@@ -143,6 +145,7 @@ var tests = []Test{
143145
Test{"GET", "/echo/" + strings.Repeat("0123456789", 100), "", 200, strings.Repeat("0123456789", 100)},
144146

145147
Test{"GET", "/writetest", "", 200, "hello"},
148+
Test{"GET", "/error/notfound/notfound", "", 404, "notfound"},
146149
Test{"GET", "/doesnotexist", "", 404, "Page not found"},
147150
Test{"POST", "/doesnotexist", "", 404, "Page not found"},
148151
Test{"GET", "/error/code/500", "", 500, statusText[500]},

0 commit comments

Comments
 (0)