简体   繁体   中英

Error handling on a call stack - http request handler

In the below code:

func (p *ProductHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // handle the request for a list of products
    if r.Method == http.MethodGet {
        p.getProductHandler(w, r)
        return
    }

    if r.Method == http.MethodPost {
        p.addProductHandler(w, r)
        return
    }

    if r.Method == http.MethodPut {
        id := findID(w, r)
        p.updateProductHandler(id, w, r)
        return
    }
    // catch all
    // if no method is satisfied return an error
    w.WriteHeader(http.StatusMethodNotAllowed)
}

// getProducts returns the products from the data store
func (p *ProductHandler) getProductHandler(w http.ResponseWriter, r *http.Request) {
    p.l.Println("Handle GET Products")

    // fetch the products from the datastore
    productList := data.GetProducts()

    // serialize the list to JSON
    err := productList.WriteJSON(w)
    if err != nil {
        http.Error(w, "Unable to marshal json", http.StatusInternalServerError)
        return
    }
}

func (p *ProductHandler) addProductHandler(w http.ResponseWriter, r *http.Request) {
    p.l.Println("Handle POST products")

    // Read the item from the incoming request
    productItem := &data.Product{}

    err := productItem.ReadJSON(r.Body)
    if err != nil {
        http.Error(w, "Unable to unmarshal JSON", http.StatusBadRequest)
        return
    }

    p.l.Printf("Product item: %#v\n", productItem)
    data.AddProductItem(productItem)
}

func (p *ProductHandler) updateProductHandler(id int, w http.ResponseWriter, r *http.Request) {
    // whatever
    return
}

func findID(w http.ResponseWriter, r *http.Request) int {
    // expect the id in the URI
    dfa := regexp.MustCompile(`/([0-9]+)`)
    matches := dfa.FindAllStringSubmatch(r.URL.Path, -1) // returns [][]string
    if len(matches) != 1 {
        http.Error(w, "Invalid URI", http.StatusBadRequest)
        return
    }
    if len(matches[0]) != 2 {
        http.Error(w, "Invlaid URI", http.StatusBadRequest)
        return
    }

    idString := matches[0][1]
    id, err := strconv.Atoi(idString)
    if err != nil {
        http.Error(w, "Invlaid URI", http.StatusBadRequest)
        return
    }
    return id
}

On error, we use http.Error() within handlers that handle POST & GET and then return

But for PUT request, call stack stack is ServeHTTP -> findID() and then ServeHTTP() -> updateProductHandler() . Error handling is required in findID() but cannot return immediately, because findID() returns int.


What is the design pattern to perform error handling for a call stack? Error wrapping using github.com/pkg/errors ....

func (p *ProductHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    // handle the request for a list of products
    if r.Method == http.MethodGet {
        p.getProductHandler(w, r)
        return
    }

    if r.Method == http.MethodPost {
        p.addProductHandler(w, r)
        return
    }

    if r.Method == http.MethodPut {
        id, err := findID(r.URL.Path)
        if err == nil {
            p.updateProductHandler(id, w, r)
        } else {
            http.Error(w, err.Error(), http.StatusBadRequest)
        }
        return
    }
    // catch all
    // if no method is satisfied return an error
    w.WriteHeader(http.StatusMethodNotAllowed)
    w.Header().Add("Allow", "GET, POST, PUT")
}

func findID(path string) (int, error) {
    // expect the id in the URI
    dfa := regexp.MustCompile(`/([0-9]+)`)
    matches := dfa.FindAllStringSubmatch(path, -1) // returns [][]string
    if len(matches) != 1 {
        return 0, fmt.Errorf("Invalid URI %s", path)
    }
    if len(matches[0]) != 2 {
        return 0, fmt.Errorf("Invalid URI %s", path)
    }

    idString := matches[0][1]
    id, err := strconv.Atoi(idString)
    if err != nil {
        return 0, fmt.Errorf("Invalid URI %s", path)
    }
    return id, nil
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM