I have the following handling of consecutive errors:
nodes, err := model.AllNodes()
if err != nil { // This error handling
pr := progressRes{ // is Just a copy of
Type: progressResponse, // error handling
Message: err.Error(), // for the next error,
MessageType: errorMessage, // what is the
Progress: 100, // best practice
} // to avoid
go response(ws, pr.json()) // repeating myself here
return // without making the code
} // complicated
links, err := model.AllLinks()
if err != nil {
pr := progressRes{
Type: progressResponse,
Message: err.Error(),
MessageType: errorMessage,
Progress: 100,
}
go response(ws, pr.json())
return
}
What is the best practice to avoid repeating myself in the above code without making the code complex? I can think of adding a new func
, however I just thought there might be a better idea which I'm not aware of.