Search code examples
gogo-http

golang - Exit http handler from anywhere


I'm using the net/http package and wondering how I can exit the handler from anywhere in the code. Say I have this code:

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request){
    err := checkSomeThing(w, r)

    if err != nil {
        return
    }

    fmt.Println("End of Handler.")
    return
}

func checkSomeThing(w http.ResponseWriter, r *http.Request) error{
    http.Error(w, "Bad Request!", http.StatusBadRequest) 
    
    return errors.New("bad request")
}

Ideally I'd like to exit the handler from within the checkSomeThing function without having to return and then return again up a level, which will get worse as the application grows. This is purely for code readability.


Solution

  • The idiomatic approach is to check error returns up the call chain.

    To exit the the handler from anywhere, use panic and recover following the pattern in the encoding/json package.

    Define a unique type for panic:

    type httpError struct {
        status  int
        message string
    }
    

    Write a function to be used in a defer statement. The function checks for the type and handles the error as appropriate. Otherwise, the function continues the panic.

    func handleExit(w http.ResponseWriter) {
        if r := recover(); r != nil {
            if he, ok := r.(httpError); ok {
                http.Error(w, he.message, he.status)
            } else {
                panic(r)
            }
        }
    }
    

    Write a helper function for the call to panic:

    func exit(status int, message string) {
        panic(httpError{status: status, message: message})
    }
    

    Use the functions like this:

    func example() {
       exit(http.StatusBadRequest, "Bad!")
    }
    
    func someHandler(w http.ResponseWriter, r *http.Request) {
       defer handleExit(w)
       example()
    }