Proposed error conventions
I propose the following changes:
I like the idea of creating an error convention:
err = someFunc()
if err != nil {
errorHandlingFunc()
// control statement, IE break, continue, or return
}
// else code
If for some reason there is no control statement (this would happen if you are merely logging errors), then it needs to be documented inside of the if statement. If there is no err != nil
, that needs to be documented directly above where error is created.
This is not okay:
err = someFunc()
return
because errors should always be checked. If code gets moved around, the person moving the code will need to remember to add error checking, and that's a chance for making mistakes, especially if the person moving the code is not the person who originally wrote the code.
This is also not okay:
err = someFunc()
if err != nil {
// stuff1
// stuff2
/// control statement
}
Either stuff1 and stuff2 need to be combined into a single function, or documentation needs to be added explaining why the convention was broken and what the error handling should look like.