简体   繁体   English

调用堆栈上的错误处理 - http 请求处理程序

[英]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出错时,我们在处理POSTGET然后return的处理程序中使用http.Error()

But for PUT request, call stack stack is ServeHTTP -> findID() and then ServeHTTP() -> updateProductHandler() .但是对于PUT请求,调用堆栈堆栈是ServeHTTP -> findID() 然后ServeHTTP() -> updateProductHandler() Error handling is required in findID() but cannot return immediately, because findID() returns int. findID()中需要错误处理但不能立即返回,因为findID()返回 int。


What is the design pattern to perform error handling for a call stack?为调用堆栈执行错误处理的设计模式是什么? Error wrapping using github.com/pkg/errors ....错误包装使用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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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