˷ԭ

    ǰλãҳ>>̿>>GO>>golangΪʲôҪͳһ
  • golangΪʲôҪͳһ
  • 2022/4/2 9:32:39
  • 1.ΪʲôҪͳһ

    ͳһĿΪǰ˿յ˵statuscode,֮ǰ߼ϿԼ200ɹ500ʧܣ400Ҳֹ쳣

    2.˷װͳһӿ

    /**
    * ͳһ
    * 룬responseݣerror
    */
    func handleresult(statuscode int, response *restful.response, value interface{}, err error) {
    if err != nil {
    handleallstatus(parsevalue(err, statuscode), response, err)
    return
    }
    
    if statuscode == http.statusok {
    handlesuccess(response, value)
    return
    }
    // 
    handleallstatus(parsevalue(value, statuscode), response, value)
    }

    3.ĺ

    func handle(statuscode int, response *restful.response, req *restful.request, err error) {
    _, fn, line, _ := runtime.caller(2)
    klog.errorf("%s:%d %v", fn, line, err)
    http.error(response, sanitizer.replace(err.error()), statuscode)
    }

    ӡ־ĸļУԼԭ

    4.

    func handleinternalerror(response *restful.response, req *restful.request, err error) {
    handle(http.statusinternalservererror, response, req, err)
    }
    
    // handlebadrequest writes http.statusbadrequest and log error
    func handlebadrequest(response *restful.response, req *restful.request, err error) {
    handle(http.statusbadrequest, response, req, err)
    }
    
    func handlenotfound(response *restful.response, req *restful.request, err error) {
    handle(http.statusnotfound, response, req, err)
    }
    
    func handleforbidden(response *restful.response, req *restful.request, err error) {
    handle(http.statusforbidden, response, req, err)
    }
    
    func handleunauthorized(response *restful.response, req *restful.request, err error) {
    handle(http.statusunauthorized, response, req, err)
    }
    
    func handletoomanyrequests(response *restful.response, req *restful.request, err error) {
    handle(http.statustoomanyrequests, response, req, err)
    }
    
    func handleconflict(response *restful.response, req *restful.request, err error) {
    handle(http.statusconflict, response, req, err)
    }

    5.ô

    golangͳһ_ǰ˿

    ?func handleallstatus(statuscode int, response *restful.response, value interface{}) {
    if value == nil {
    response.writeheader(statuscode)
    return
    }
    
    switch ee := value.(type) {
    case error:
    handle(statuscode, response, nil, ee)
    case string:
    response.writeheader(statuscode)
    response.writeasjson(value)
    return
    default:
    response.writeheader(statuscode)
    //ǷΪbyte
    b, ok := value.([]byte)
    if ok {
    response.write(b)
    } else {
    response.writeentity(value)
    }
    }
    }

    6.ԭ

    func parsevalue(value interface{}, statuscode int) int {
    if value == nil {
    return statuscode
    }
    
    obj := make(map[string]interface{})
    switch tvalue := value.(type) {
    case error:
    json.unmarshal([]byte(tvalue.error()), &obj)
    default:
    b, ok := value.([]byte)
    if ok {
    json.unmarshal(b, &obj)
    } else {
    j, err := json.marshal(value)
    if err == nil {
    json.unmarshal(j, &obj)
    }
    }
    }
    
    if s, o := obj["code"]; o {
    switch rs := s.(type) {
    case string:
    rs1, err := strconv.atoi(rs)
    if err != nil {
    return rs1
    }
    case int:
    return rs
    case float64:
    return int(rs)
    default:
    return statuscode
    }
    
    }
    return statuscode
    }

    ƪgolangΪʲôҪͳһ¾ͽܵ,golangͳһ̳ǰ»ϣԺֱ֧̳