2019-07-19 15:04:47 +00:00
|
|
|
package aerrors
|
|
|
|
|
|
|
|
import (
|
2019-07-23 13:06:39 +00:00
|
|
|
"errors"
|
2019-07-19 15:04:47 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-02-04 22:19:05 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-07-19 15:04:47 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2019-07-23 13:04:45 +00:00
|
|
|
// New creates a new non-fatal error
|
2019-07-22 16:08:54 +00:00
|
|
|
func New(retCode uint8, message string) ActorError {
|
2019-07-23 13:04:45 +00:00
|
|
|
if retCode == 0 {
|
|
|
|
return &actorError{
|
|
|
|
fatal: true,
|
|
|
|
retCode: 0,
|
|
|
|
|
|
|
|
msg: "tried creating an error and setting RetCode to 0",
|
|
|
|
frame: xerrors.Caller(1),
|
2019-07-23 13:06:39 +00:00
|
|
|
err: errors.New(message),
|
2019-07-23 13:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-22 16:08:54 +00:00
|
|
|
return &actorError{
|
|
|
|
retCode: retCode,
|
|
|
|
|
|
|
|
msg: message,
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:15:16 +00:00
|
|
|
// Newf creates a new non-fatal error
|
|
|
|
func Newf(retCode uint8, format string, args ...interface{}) ActorError {
|
|
|
|
if retCode == 0 {
|
|
|
|
return &actorError{
|
|
|
|
fatal: true,
|
|
|
|
retCode: 0,
|
|
|
|
|
|
|
|
msg: "tried creating an error and setting RetCode to 0",
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: fmt.Errorf(format, args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &actorError{
|
|
|
|
retCode: retCode,
|
|
|
|
|
|
|
|
msg: fmt.Sprintf(format, args...),
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 07:59:53 +00:00
|
|
|
// todo: bit hacky
|
|
|
|
func NewfSkip(skip int, retCode uint8, format string, args ...interface{}) ActorError {
|
|
|
|
if retCode == 0 {
|
|
|
|
return &actorError{
|
|
|
|
fatal: true,
|
|
|
|
retCode: 0,
|
|
|
|
|
|
|
|
msg: "tried creating an error and setting RetCode to 0",
|
|
|
|
frame: xerrors.Caller(skip),
|
|
|
|
err: fmt.Errorf(format, args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &actorError{
|
|
|
|
retCode: retCode,
|
|
|
|
|
|
|
|
msg: fmt.Sprintf(format, args...),
|
|
|
|
frame: xerrors.Caller(skip),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
func Fatal(message string, args ...interface{}) ActorError {
|
|
|
|
return &actorError{
|
|
|
|
fatal: true,
|
|
|
|
msg: message,
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalf(format string, args ...interface{}) ActorError {
|
|
|
|
return &actorError{
|
|
|
|
fatal: true,
|
|
|
|
msg: fmt.Sprintf(format, args...),
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:04:45 +00:00
|
|
|
// Wrap extens chain of errors with a message
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:04:45 +00:00
|
|
|
// Wrapf extens chain of errors with a message
|
2019-07-19 15:04:47 +00:00
|
|
|
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,
|
|
|
|
|
2019-10-15 05:19:37 +00:00
|
|
|
msg: "tried absorbing an error that is already a fatal error",
|
2019-07-19 15:04:47 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 05:19:37 +00:00
|
|
|
|
|
|
|
func HandleExternalError(err error, msg string) ActorError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if aerr, ok := err.(ActorError); ok {
|
|
|
|
return &actorError{
|
|
|
|
fatal: IsFatal(aerr),
|
|
|
|
retCode: RetCode(aerr),
|
|
|
|
|
|
|
|
msg: msg,
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: aerr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:19:05 +00:00
|
|
|
if xerrors.Is(err, &cbor.SerializationError{}) {
|
2019-12-18 03:41:58 +00:00
|
|
|
return &actorError{
|
|
|
|
fatal: false,
|
|
|
|
retCode: 253,
|
|
|
|
msg: msg,
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 05:19:37 +00:00
|
|
|
return &actorError{
|
2020-01-08 03:46:55 +00:00
|
|
|
fatal: false,
|
|
|
|
retCode: 219,
|
2019-10-15 05:19:37 +00:00
|
|
|
|
|
|
|
msg: msg,
|
|
|
|
frame: xerrors.Caller(1),
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|