Skip to content
Snippets Groups Projects
Unverified Commit 3149d02c authored by Hans Moog's avatar Hans Moog Committed by GitHub
Browse files

Feat: Added a package for common sentinel errors (#798)


* Feat: Added a package for common sentinel errors

* Fix: fixed wrong import

Co-authored-by: default avatarHans Moog <hm@mkjc.net>
parent a925b982
No related branches found
No related tags found
No related merge requests found
// Package cerrors provides sentinel error values for common errors that are likely to appear in multiple packages.
//
// Since wrapping errors using the new error handling API of Go creates anonymous errors it is not possible to wrap an
// existing sentinel error with another sentinel error without loosing the stack trace.
//
// Using shared sentinel errors enables them to be annotated and used across packages borders without the caller having
// to distinguish between similar errors types of different packages that would otherwise be identical.
package cerrors
import "errors"
var (
// ErrBase58DecodeFailed is returned if a base58 encoded string can not be decoded.
ErrBase58DecodeFailed = errors.New("failed to decode base58 encoded string")
// ErrParseBytesFailed is returned if information can not be parsed from a sequence of bytes.
ErrParseBytesFailed = errors.New("failed to parse bytes")
)
package payload
import (
"github.com/iotaledger/goshimmer/packages/cerrors"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/stringify"
"golang.org/x/xerrors"
......@@ -44,7 +45,7 @@ func GenericDataPayloadFromBytes(bytes []byte) (genericDataPayload *GenericDataP
func GenericDataPayloadFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (genericDataPayload *GenericDataPayload, err error) {
payloadSize, err := marshalUtil.ReadUint32()
if err != nil {
err = xerrors.Errorf("failed to parse payload size (%v): %w", err, ErrParseBytesFailed)
err = xerrors.Errorf("failed to parse payload size (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
......@@ -54,7 +55,7 @@ func GenericDataPayloadFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (ge
return
}
if genericDataPayload.data, err = marshalUtil.ReadBytes(int(payloadSize)); err != nil {
err = xerrors.Errorf("failed to parse data (%v): %w", err, ErrParseBytesFailed)
err = xerrors.Errorf("failed to parse data (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
......
package payload
import (
"errors"
)
var (
// ErrParseBytesFailed is returned if information can not be parsed from a sequence of bytes.
ErrParseBytesFailed = errors.New("failed to parse bytes")
)
......@@ -3,6 +3,7 @@ package payload
import (
"fmt"
"github.com/iotaledger/goshimmer/packages/cerrors"
"github.com/iotaledger/hive.go/marshalutil"
"golang.org/x/xerrors"
)
......@@ -38,11 +39,11 @@ func FromBytes(payloadBytes []byte) (payload Payload, consumedBytes int, err err
func FromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (payload Payload, err error) {
payloadSize, err := marshalUtil.ReadUint32()
if err != nil {
err = xerrors.Errorf("failed to parse payload size (%v): %w", err, ErrParseBytesFailed)
err = xerrors.Errorf("failed to parse payload size (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
if payloadSize > MaxSize {
err = xerrors.Errorf("maximum payload size of %d bytes exceeded: %w", MaxSize, ErrParseBytesFailed)
err = xerrors.Errorf("maximum payload size of %d bytes exceeded: %w", MaxSize, cerrors.ErrParseBytesFailed)
return
}
......@@ -55,7 +56,7 @@ func FromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (payload Payload, err
marshalUtil.ReadSeek(-marshalutil.UINT32_SIZE * 2)
payloadBytes, err := marshalUtil.ReadBytes(int(payloadSize) + 8)
if err != nil {
err = xerrors.Errorf("failed to unmarshal payload bytes (%v): %w", err, ErrParseBytesFailed)
err = xerrors.Errorf("failed to unmarshal payload bytes (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
......
......@@ -5,6 +5,7 @@ import (
"strconv"
"sync"
"github.com/iotaledger/goshimmer/packages/cerrors"
"github.com/iotaledger/hive.go/marshalutil"
"golang.org/x/xerrors"
)
......@@ -64,7 +65,7 @@ func TypeFromBytes(typeBytes []byte) (typeResult Type, consumedBytes int, err er
func TypeFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (typeResult Type, err error) {
typeUint32, err := marshalUtil.ReadUint32()
if err != nil {
err = xerrors.Errorf("failed to parse type (%v): %w", err, ErrParseBytesFailed)
err = xerrors.Errorf("failed to parse type (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
typeResult = Type(typeUint32)
......
......@@ -4,7 +4,9 @@ import (
"context"
"crypto"
"github.com/iotaledger/goshimmer/packages/cerrors"
"golang.org/x/xerrors"
// Only want to use init
_ "golang.org/x/crypto/blake2b"
......@@ -57,7 +59,7 @@ func FromBytes(bytes []byte) (result *Request, consumedBytes int, err error) {
result = &Request{}
if _, err = marshalUtil.ReadUint32(); err != nil {
err = xerrors.Errorf("failed to parse payload size (%v): %w", err, payload.ErrParseBytesFailed)
err = xerrors.Errorf("failed to parse payload size (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
result.payloadType, err = payload.TypeFromMarshalUtil(marshalUtil)
......@@ -67,18 +69,18 @@ func FromBytes(bytes []byte) (result *Request, consumedBytes int, err error) {
}
addr, err := marshalUtil.ReadBytes(address.Length)
if err != nil {
err = xerrors.Errorf("failed to parse address of faucet request (%v): %w", err, payload.ErrParseBytesFailed)
err = xerrors.Errorf("failed to parse address of faucet request (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
result.address, _, err = address.FromBytes(addr)
if err != nil {
err = xerrors.Errorf("failed to unmarshal address of faucet request (%v): %w", err, payload.ErrParseBytesFailed)
err = xerrors.Errorf("failed to unmarshal address of faucet request (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
result.nonce, err = marshalUtil.ReadUint64()
if err != nil {
err = xerrors.Errorf("failed to unmarshal nonce of faucet request (%v): %w", err, payload.ErrParseBytesFailed)
err = xerrors.Errorf("failed to unmarshal nonce of faucet request (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
consumedBytes = marshalUtil.ReadOffset()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment