We can customize the handling of status codes specified by WebServer, for example, for common errors like 404/403/500, we can display custom error messages, page content, or redirect to a specific page.

The related methods are as follows:

  1. func (s *Server) BindStatusHandler(status int, handler HandlerFunc)
  2. func (s *Server) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc)
  3. func (d *Domain) BindStatusHandler(status int, handler HandlerFunc)
  4. func (d *Domain) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc)

As we can see, we can use BindStatusHandler or BindStatusHandlerByMap to implement custom callback handling for specified status codes, and this feature also supports binding to specific domain names. Let’s look at a few simple examples.

Basic Usage

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/", func(r *ghttp.Request){
  9. r.Response.Writeln("halo world!")
  10. })
  11. s.BindStatusHandler(404, func(r *ghttp.Request){
  12. r.Response.Writeln("This is customized 404 page")
  13. })
  14. s.SetPort(8199)
  15. s.Run()
  16. }

After execution, when we visit an unbound route page, such as http://127.0.0.1:8199/test, we can see the page shows our expected return result: This is customized 404 page.

Moreover, the common way of handling web page request error status codes is to guide users to a specific error page. Therefore, in the status code callback handling function, we can use the r.RedirectTo method to perform page redirection, as shown in the following example:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/status/:status", func(r *ghttp.Request) {
  9. r.Response.Write("woops, status ", r.Get("status"), " found")
  10. })
  11. s.BindStatusHandler(404, func(r *ghttp.Request){
  12. r.Response.RedirectTo("/status/404")
  13. })
  14. s.SetPort(8199)
  15. s.Run()
  16. }

After execution, when we manually access a non-existent page through the browser, such as http://127.0.0.1:8199/test, we can see the page is redirected to http://127.0.0.1:8199/status/404, and the page returns the content: woops, status 404 found

Batch Settings

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindStatusHandlerByMap(map[int]ghttp.HandlerFunc {
  9. 403 : func(r *ghttp.Request){r.Response.Writeln("403")},
  10. 404 : func(r *ghttp.Request){r.Response.Writeln("404")},
  11. 500 : func(r *ghttp.Request){r.Response.Writeln("500")},
  12. })
  13. s.SetPort(8199)
  14. s.Run()
  15. }

As we can see, we can use the BindStatusHandlerByMap method for batch settings of custom status codes. After the example program is executed, when the service API returns status codes 403/404/500, the API will return the corresponding status code number.

Precautions

If content output is involved in custom status code handling methods, it’s often necessary to use r.Response.ClearBuffer() to clear the original buffered output content.