github.com/amp-space/amp-sdk-go@v0.7.6/amp/errors.go (about) 1 package amp 2 3 import ( 4 "fmt" 5 ) 6 7 var ( 8 ErrStreamClosed = ErrCode_NotConnected.Error("stream closed") 9 ErrCellNotFound = ErrCode_CellNotFound.Error("cell not found") 10 ErrPinCtxClosed = ErrCode_PinContextClosed.Error("pin context closed") 11 ErrNotPinnable = ErrCode_PinFailed.Error("not pinnable") 12 ErrUnimplemented = ErrCode_PinFailed.Error("not implemented") 13 ErrBadCellTx = ErrCode_MalformedTx.Error("missing cell spec / target cell") 14 ErrNothingToPin = ErrCode_PinFailed.Error("nothing to pin") 15 ErrShuttingDown = ErrCode_ShuttingDown.Error("shutting down") 16 ErrTimeout = ErrCode_Timeout.Error("timeout") 17 ErrNoAuthToken = ErrCode_AuthFailed.Error("no auth token") 18 ) 19 20 // Error makes our custom error type conform to a standard Go error 21 func (err *Err) Error() string { 22 codeStr, exists := ErrCode_name[int32(err.Code)] 23 if !exists { 24 codeStr = ErrCode_name[int32(ErrCode_UnnamedErr)] 25 } 26 27 if len(err.Msg) == 0 { 28 return codeStr 29 } 30 31 return err.Msg + " / " + codeStr 32 } 33 34 // Error returns an *Err with the given error code 35 func (code ErrCode) Error(msg string) error { 36 if code == ErrCode_NoErr { 37 return nil 38 } 39 return &Err{ 40 Code: code, 41 Msg: msg, 42 } 43 } 44 45 // Errorf returns an *Err with the given error code and msg. 46 // If one or more args are given, msg is used as a format string 47 func (code ErrCode) Errorf(format string, msgArgs ...interface{}) error { 48 if code == ErrCode_NoErr { 49 return nil 50 } 51 52 err := &Err{ 53 Code: code, 54 } 55 if len(msgArgs) == 0 { 56 err.Msg = format 57 } else { 58 err.Msg = fmt.Sprintf(format, msgArgs...) 59 } 60 61 return err 62 } 63 64 // Wrap returns a ReqErr with the given error code and "cause" error 65 func (code ErrCode) Wrap(cause error) error { 66 if cause == nil { 67 return nil 68 } 69 return &Err{ 70 Code: code, 71 Msg: cause.Error(), 72 } 73 } 74 75 func GetErrCode(err error) ErrCode { 76 if err == nil { 77 return ErrCode_NoErr 78 } 79 80 if arcErr, ok := err.(*Err); ok { 81 return arcErr.Code 82 } 83 84 return ErrCode_UnnamedErr 85 }