douwuying4709 2016-11-22 17:41
浏览 333
已采纳

如何在encoding / json UnmarshalTypeError中使用偏移值,以更好地处理错误?

A little over a year ago, Go added an Offset value to the json.UnmarshalTypeError type (see closed issue here for context). The purpose behind the offset value makes sense, but I'm not sure how it can be used when reading a go http response body, which is of type io.ReadCloser.

// An UnmarshalTypeError describes a JSON value that was
// not appropriate for a value of a specific Go type.
type UnmarshalTypeError struct {
    Value  string       // description of JSON value - "bool", "array",   "number -5"
    Type   reflect.Type // type of Go value it could not be assigned to
    Offset int64        // error occurred after reading Offset bytes
}

For example:

var body CustomType
decoderErr := json.NewDecoder(response.Body).Decode(&body)

if decoderErr != nil {

    if typeError, ok := decoderErr.(*json.UnmarshalTypeError); ok {
        // Do something with typeError.Offset here
    }


}

At the point of the error getting caught, I've already read from response.Body via json.NewDecoder.... I'm looking for a way to read response.Body again, but only up to the point of the error by using the Offset value in typeError.

  • 写回答

1条回答 默认 最新

  • dongyuan8469 2016-11-22 18:56
    关注

    Since you want to reuse the request body you should read and store the body before you Unmarshal the body, then if there is a JSON syntax or type error you can return a more useful error using the body you previously stored.

    Proof of concept:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
    )
    
    type Hello struct {
        Name    string `json:"name"`
        Message string `json:"message"`
    }
    
    func main() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            b, err := ioutil.ReadAll(r.Body)
            if err != nil {
                http.Error(w, "Error reading body", 400)
                return
            }
    
            h := &Hello{}
            if err := json.Unmarshal(b, &h); err != nil {
                var msg string
                switch t := err.(type) {
                case *json.SyntaxError:
                    jsn := string(b[0:t.Offset])
                    jsn += "<--(Invalid Character)"
                    msg = fmt.Sprintf("Invalid character at offset %v
     %s", t.Offset, jsn)
                case *json.UnmarshalTypeError:
                    jsn := string(b[0:t.Offset])
                    jsn += "<--(Invalid Type)"
                    msg = fmt.Sprintf("Invalid value at offset %v
     %s", t.Offset, jsn)
                default:
                    msg = err.Error()
                }
                http.Error(w, msg, 400)
                return
            }
    
            w.Write([]byte(`Good to go!`))
        })
    
        if err := http.ListenAndServe(":8000", nil); err != nil {
            log.Fatal(err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写