(body []byte, iface interface{})
| 40 | } |
| 41 | |
| 42 | func UnmarshalJSON(body []byte, iface interface{}) *util.JSONResponse { |
| 43 | if !utf8.Valid(body) { |
| 44 | return &util.JSONResponse{ |
| 45 | Code: http.StatusBadRequest, |
| 46 | JSON: jsonerror.NotJSON("Body contains invalid UTF-8"), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if err := json.Unmarshal(body, iface); err != nil { |
| 51 | // TODO: We may want to suppress the Error() return in production? It's useful when |
| 52 | // debugging because an error will be produced for both invalid/malformed JSON AND |
| 53 | // valid JSON with incorrect types for values. |
| 54 | return &util.JSONResponse{ |
| 55 | Code: http.StatusBadRequest, |
| 56 | JSON: jsonerror.BadJSON("The request body could not be decoded into valid JSON. " + err.Error()), |
| 57 | } |
| 58 | } |
| 59 | return nil |
| 60 | } |
no test coverage detected