2019-07-19 15:04:47 +00:00
|
|
|
package aerrors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2019-07-22 16:08:54 +00:00
|
|
|
func New(retCode uint8, message string) ActorError {
|
|
|
|
return &actorError{
|
|
|
|
retCode: retCode,
|
|
|
|
|
|
|
|
msg: message,
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 15:04:47 +00:00
|
|
|
func Wrap(err ActorError, message string) ActorError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &actorError{
|
|
|
|
fatal: IsFatal(err),
|
|
|
|
retCode: RetCode(err),
|
|
|
|
|
|
|
|
msg: message,
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap extens chain of errors with
|
|
|
|
func Wrapf(err ActorError, format string, args ...interface{}) ActorError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &actorError{
|
|
|
|
fatal: IsFatal(err),
|
|
|
|
retCode: RetCode(err),
|
|
|
|
|
|
|
|
msg: fmt.Sprintf(format, args...),
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Absorb takes and error and makes in not fatal ActorError
|
|
|
|
func Absorb(err error, retCode uint8, msg string) ActorError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if aerr, ok := err.(ActorError); ok && IsFatal(aerr) {
|
|
|
|
return &actorError{
|
|
|
|
fatal: true,
|
|
|
|
retCode: 0,
|
|
|
|
|
|
|
|
msg: "tried absorbing an error that is alreay an fatal error",
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if retCode == 0 {
|
|
|
|
return &actorError{
|
|
|
|
fatal: true,
|
|
|
|
retCode: 0,
|
|
|
|
|
|
|
|
msg: "tried absorbing an error and setting RetCode to 0",
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &actorError{
|
|
|
|
fatal: false,
|
|
|
|
retCode: retCode,
|
|
|
|
|
|
|
|
msg: msg,
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Escalate takes and error and escalates it into a fatal error
|
|
|
|
func Escalate(err error, msg string) ActorError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &actorError{
|
|
|
|
fatal: true,
|
|
|
|
|
|
|
|
msg: msg,
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|