Home > Backend Development > Golang > How to Customize 404 Error Handling in httprouter with Custom Handlers?

How to Customize 404 Error Handling in httprouter with Custom Handlers?

Susan Sarandon
Release: 2024-10-25 19:02:02
Original
693 people have browsed it

How to Customize 404 Error Handling in httprouter with Custom Handlers?

Handling 404s in httprouter with Custom Handlers

httprouter is a popular routing middleware for Golang, which allows you to register custom handlers for specific routes or patterns. In cases where a route or resource is not found, you may want to handle these 404 Not Found responses yourself.

Understanding the NotFound Field

The httprouter.Router type has a field called NotFound, which is an http.Handler. This means that you can assign a custom handler to this field to handle 404 responses.

Creating a Custom NotFound Handler

To create a custom NotFound handler, you need to define a function with the signature:

func(http.ResponseWriter, *http.Request)
Copy after login

You can then convert this function to an http.Handler using the http.HandlerFunc helper function.

Example Usage

Here is an example of how you can set a custom NotFound handler:

<code class="go">func MyNotFound(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    w.WriteHeader(http.StatusNotFound)
    w.Write([]byte("My own Not Found handler."))
    w.Write([]byte(" The page you requested could not be found."))
}

var router *httprouter.Router = ... // Your router value
router.NotFound = http.HandlerFunc(MyNotFound)</code>
Copy after login

This custom handler will be automatically invoked by httprouter when a 404 Not Found response is encountered.

Manual Invocation of NotFound Handler

In some cases, you may want to manually invoke the NotFound handler from within another handler. You can achieve this by passing a ResponseWriter and a *Request to the MyNotFound function or directly to the router's NotFound method:

<code class="go">func ResourceHandler(w http.ResponseWriter, r *http.Request) {
    exists := ... // Find out if requested resource is valid and available
    if !exists {
        MyNotFound(w, r) // Pass ResponseWriter and Request
        // Or via the Router:
        // router.NotFound(w, r)
        return
    }

    // Resource exists, serve it
    // ...
}</code>
Copy after login

The above is the detailed content of How to Customize 404 Error Handling in httprouter with Custom Handlers?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template