With Errors (witherrors) package provide a flexible way to wrap and add additional information to your errors.
- New Error
- Wrap Error
- Error
- With Code
- With Custom Message
- With Dependency
- With Priority
- Get Code
- Get Custom Message
- Get Dependency
- Get Error
- Get Message
- Get Priority
- Clear
- Clear And Get
$ go get -u github.com/amirhsn/witherrors
Below is the detail of how to use this module with listed features. Firstly we need to import this modules.
import er "github.com/amirhsn/witherrors"
Same as built in go error which is create new error that takes one string parameter.
newErr := er.NewError("this is new error")
Wrap existing error to be passed into witherrors
struct attribute.
_, err := redis.HDEL(.....)
return er.WrapError(err)
Get error message as string data type, this is implementation of built in Error()
in Go.
_, err := redis.HDEL(.....)
myError := er.WrapError(err)
errMessage := myError.Error()
Set error code as a string data type.
_, err := redis.HDEL(.....)
return er.WrapError(err).WithCode("CODE-1")
Set error custom message as an empty interface data type.
_, err := redis.HDEL(.....)
return er.WrapError(err).WithCode("CODE-1").WithCustomMessage("Custom Message")
Set error dependency as a string data type.
_, err := redis.HDEL(.....)
return er.WrapError(err).WithCode("CODE-1").WithCustomMessage("Custom Message").WithDependency("MONGO")
Set error priority as a Priority
data type
_, err := redis.HDEL(.....)
return er.WrapError(err).WithCode("CODE-1").WithPriority(er.HIGH)
This method will receive Priority
data type which is consist of 5 values listed below.
VERY_LOW
LOW
MEDIUM
HIGH
VERY_HIGH
Get error code as a string data type, if no code is set, message
object will return empty code message.
_, err := redis.HDEL(.....)
myError := er.WrapError(err).WithCode("CODE-1")
code := myError.GetCode()
Get custom message error as an empty interface data type, if no custom message is set, message
object will return empty custom message.
_, err := redis.HDEL(.....)
myError := er.WrapError(err).WithCustomMessage("custom")
cMessage := myError.GetCustomMessage()
Get error dependency as a string data type, if no dependency is set, message
object will return empty dependency message.
_, err := redis.HDEL(.....)
myError := er.WrapError(err).WithDependency("MONGO")
dep := myError.GetDependency()
Get error as an error data type, if no error is set, message
object will return empty error message.
_, err := redis.HDEL(.....)
myError := er.WrapError(err)
newError := myError.GetError()
Get message as a string data type. Remember that this is not error message, but message because of empty get operation as mentioned above.
_, err := redis.HDEL(.....)
myError := er.WrapError(err)
message := myError.GetMessage()
Get error priority as a string data type, if no priority is set, message
object will return empty priority message.
_, err := redis.HDEL(.....)
myError := er.WrapError(err).WithPriority(er.HIGH)
priority := er.GetPriority()
Clear all existing data (code, dependency, priority, custom message).
_, err := redis.HDEL(.....)
myError := er.WrapError(err)
myError.Clear()
Clear all existing data (code, dependency, priority, custom message).
_, err := redis.HDEL(.....)
myError := er.WrapError(err)
clearedErr := myError.ClearAndGet().WithCustomMessage("new err")
MIT © Amir Husein