duanhuan8983 2018-05-05 05:42
浏览 24
已采纳

如何避免在连续的错误处理中重复自己

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.

  • 写回答

3条回答 默认 最新

  • douxiang3978 2018-05-05 06:55
    关注

    If you are repeating a number of steps in multiple places the correct approach would be to abstract away those steps into a procedure, which is what programming is about. This applies to error handling just as well as to any other parts of your program.

    One option:

    func allNodesAndLinks() ([]*Node, []*Link, error) {
        nodes, err := model.AllNodes()
        if err != nil {
            return nil, nil, err
        }
        links, err := model.AllLinks()
        if err != nil {
            return nil, nil, err
        }
        return nodes, links, nil
    }
    
    // ...
    
    nodes, links, err := allNodesAndLinks()
    if err != nil {
        pr := progressRes{
            Type:        progressResponse,
            Message:     err.Error(),
            MessageType: errorMessage,
            Progress:    100,
        }
        go response(ws, pr.json())
        return
    }
    

    Another option:

    func respondWithError(ws io.Writer, err error) {
        pr := progressRes{
            Type:        progressResponse,
            Message:     err.Error(),
            MessageType: errorMessage,
            Progress:    100,
        }
        response(ws, pr.json())
    }
    
    // ...
    
    nodes, err := model.AllNodes()
    if err != nil {
        go respondWithError(ws, err)
        return
    }
    links, err := model.AllLinks()
    if err != nil {
        go respondWithError(ws, err)
        return
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b