处理错误真是相当无聊的。

huangapple go评论59阅读模式
英文:

Pretty boring to handle error

问题

我有一个结构体,其中有一个方法用于处理响应。

type ctrl struct {
    *base.AjaxCtrl
    file ini.File
}

func (rcv *ctrl) getVar() string {
    return mux.Vars(rcv.Req)["location"]
}

func (rcv *ctrl) getFile() string {
    return location.JoinPaths(folder, rcv.getVar()+ext)
}

func (rcv *ctrl) upload() {
    file, err := ini.LoadFile(rcv.getFile())
    if err != nil {
        rcv.AddErr("TextError", err.Error())
        return
    }
    rcv.file = file
}

// 将文本转换为 JSON 结构
func (rcv *ctrl) convertToJson() string {
    js, err := json.Marshal(rcv.file.Section("text/signup"))
    if err != nil {
        rcv.AddErr("ConvertError", err.Error())
        return ""
    }
    return string(js)
}

func (rcv *ctrl) serveHttp() (types.SuccsJSON, types.ErrorsJSON) {

    rcv.upload()
    if rcv.AnyErrors() {
        return nil, rcv.Errs
    }
    
    str := rcv.convertToJson()
    if rcv.AnyErrors() {
        return nil, rcv.Errs
    }
    return c.Sucss, nil
}

serveHttp() 方法处理向客户端的响应。正如你所看到的,我每次都使用 AnyErrors() 方法来处理错误。我觉得这种方式相当无聊,而且可能是错误的设计。抛出 panic 是否会更好呢?

func (rcv *ctrl) upload() {
    file, err := ini.LoadFile(rcv.getFile())
    if err != nil {
        rcv.AddErr("TextError", err.Error())
        panic(err)
    }
    rcv.file = file
}

请注意,抛出 panic 会导致程序中断并触发 panic 恢复机制。这意味着你需要在调用 serveHttp() 的地方进行适当的 panic 恢复处理。

英文:

I have struct that have a method that serve the response.

type ctrl struct {
	*base.AjaxCtrl
	file ini.File
}

func (rcv *ctrl) getVar() string {
	return mux.Vars(rcv.Req)["location"]
}

func (rcv *ctrl) getFile() string {
	return location.JoinPaths(folder, rcv.getVar()+ext)
}

func (rcv *ctrl) upload() {
	file, err := ini.LoadFile(rcv.getFile())
	if err != nil {
		rcv.AddErr("TextError", err.Error())
		return
	}
	rcv.file = file
}

// Convert text to json structure
func (rcv *ctrl) convertToJson() string {
	js, err := json.Marshal(rcv.file.Section("text/signup"))
	if err != nil {
		rcv.AddErr("ConvertError", err.Error())
		return ""
	}
	return string(js)
}

func (rcv *ctrl) serveHttp() (types.SuccsJSON, types.ErrorsJSON) {

	rcv.upload()
	if rcv.AnyErrors() {
		return nil, rcv.Errs
	}
	
	str := rcv.convertToJson()
	if rcv.AnyErrors() {
		return nil, rcv.Errs
	}
	return c.Sucss, nil
}

The method serveHttp() handle the response to client. As you can see, I am handling the error with the method AnyError() everytime. I find this way is pretty boring and maybe wrong design.
Would it be better to throw a panic instead of error handling?

func (rcv *ctrl) upload() {
	file, err := ini.LoadFile(rcv.getFile())
	if err != nil {
		rcv.AddErr("TextError", err.Error())
		panic()
	}
	rcv.file = file
}

答案1

得分: 4

不,这样做并不更好。在Go语言中检查错误并不是一种糟糕的设计。另一方面,当没有理由这样做时,引发panic是一种糟糕的设计,因为它更难以进行测试和调试。

Go Wiki明确不鼓励在非常特殊的情况下使用panic,即当错误是不可恢复的且程序状态无法修复时。

英文:

No, it would not be better. Checking your errors is not bad design in Go. Panicking when there is no reason to do so, on the other hand, is bad design, because it's harder to test and debug.

The Go Wiki explicitly discourages the use of panic outside of truly exceptional cases, when the error is unrecoverable and state of the program can't be fixed.

huangapple
  • 本文由 发表于 2015年2月11日 20:30:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/28454398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定