Error Handling
Error logging is another important aspect in ensuring the TaskCollect server runs smoothly and if an error were to occur, that it will be reported. TaskCollect’s errors
package adds new functionality to suit the needs of the project such as a custom error wrapper that has the ability to provide more context about where the error originated, what the error type is, and allow for better management of tracing errors.
To prevent the need for two error library imports, Go’s standard error library has been implemented right into our own library.
Import TaskCollect’smain/errors
package, rather than Go’serrors
package.
TaskCollect utilises many custom error variables. An example is ErrAuthFailed
for when authentication fails. These sort of variables should only be created if the same error message is being used more than once.
Custom error variables located in the errors
package should be created in the following fashion:
var ErrNameOfError = errors.New("error message")
For errors that are intended for use only within a certain package should be created like so:
var errNameOfError = errors.NewError("packageName.funcName", "error message", err)
It is important to note a few things:
- The error variable must begin with
err
orErr
to make it clear that it is an error variable. errors.NewError
is much more informative and versatile, providing the error origin, error message, as well as any other error message that is wrapped into the new error.