I'm trying to implement my own API. This code excerpt should show the basic working routines. The storage is temporary an in-memory-array and the only HTTP method for now is POST.I outsourced the parsing into ParseRecordRequest(), because I use the code twice for POST and PUT requests and will extend it with further conditions.All responses (errors and records) are generated by GenerateJSON(), which allows me to customize the response header etc. within a single place.But what should I do, if GenerateJSON() can't marhsal the JSON? All usual errors are sent back with an HandlerError struct, that can't be marshalled in this case.package mainimport (    encoding/json    fmt    net/http    github.com/julienschmidt/httprouter)type Record struct {    ID   int    `json:id`    Name string `json:name`}var DB []Recordvar latestID int = 0type HandlerError struct {    Code    int    `json:code`    Message string `json:message`}func CreateRecord(w http.ResponseWriter, r *http.Request, p httprouter.Params) {    payload, err := ParseRecordRequest(w, r)    if err != nil {        GenerateResponse(w, err)        return    }    latestID++    record := Record{latestID, payload.Name}    DB = append(DB, record)    GenerateResponse(w, record)}func ParseRecordRequest(w http.ResponseWriter, r *http.Request) (*Record, *HandlerError) {    data := new(Record)    err := json.NewDecoder(r.Body).Decode(data)    if err != nil {        return nil, &HandlerError{http.StatusInternalServerError, Can't decode the JSON.}    }    if data.Name ==  {        return nil, &HandlerError{http.StatusBadRequest, Invalid record name.}    }    return data, nil}func GenerateResponse(w http.ResponseWriter, data interface{}) {    payload, err := json.MarshalIndent(data, ,     )    if err != nil {        // How should the error be handeled?        // Sending an ErrorHandler is impossible,        // because the Marshaler doesn't work    }    w.Header().Set(Content-Type, application/json; charset=UTF-8)    w.Header().Set(Accept, application/json)    w.Write(payload)}func main() {    router := httprouter.New()    router.POST(/api/record, CreateRecord)    fmt.Println(Server is running...)    http.ListenAndServe(:8080, router)}
Restful API for parsing records
json;error handling;api;go;rest