by mFixman · 4y ago · 0 comments
func ThisCouldUseExceptions() error {
err := Exceptions()
if err != nil {
return terrors.Propagate(err)
}
err = GeneraliseBetter()
if err != nil {
return terrors.Propagate(err)
}
value, err = InFunctionsWithMultipleErrorPoints()
if err != nil {
return terrors.Propagate(err)
}
value, err := AlsoTheyHelp()
if err != nil {
return terrors.Propagate(err)
}
chained_val, err := WithChaining(value)
err = AndPreventAccidentallyIgnoringErrors(chained_val)
if err != nil {
return terrors.Propagate(err)
}
} func ThisCouldUseExceptions() error {
err := Exceptions(arg)
if err != nil {
return fmt.Errorf("if a '%v' dev: %w", err)
}
err = GeneraliseBetter()
if err != nil {
return fmt.Errorf("has to actually: %w", err)
}
value, err = InFunctionsWithMultipleErrorPoints()
if err != nil {
return fmt.Errorf("read these errors: %w", err)
}
value, err := AlsoTheyHelp()
if err != nil {
return fmt.Errorf("they will actually be: %w", err)
}
chained_val, err := WithChaining(value)
if err != nil {
return fmt.Errorf("useful: %w", err)
}
err = AndPreventAccidentallyIgnoringErrors(chained_val)
if err != nil {
return fmt.Errorf("this isnt a real problem: %w", err)
}
} if err != nil {
return mherr.WrapErr(err, "optional context")
}
I just wrote a custom function that adds the stack trace to the error. I also have it setup as a code snippet in VS Code so all I have to do is type "if err" and hit tab. Yes it looks a bit verbose but it adds approximately zero extra work and makes error handling extremely easy by default.Also, turn on log flags to add line numbers. log.SetFlags(log.Llongfile | log.LstdFlags)