Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error codes and define errors with it #343

Closed
wants to merge 12 commits into from
61 changes: 60 additions & 1 deletion constants/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Package constants provides constants.the convention of naming is to use MixedCaps or mixedCaps rather than underscores to write multiword names. https://golang.org/doc/effective_go#mixed-caps
package constants

import "errors"
import (
"errors"

zerrors "github.com/0chain/errors"
)

var (
// ErrInvalidParameter parameter is not specified or invalid
Expand Down Expand Up @@ -31,3 +35,58 @@ var (
// ErrNotFound ref not found
ErrNotFound = errors.New("ref not found")
)

// zerrors code
const (
UnknownCodeError = ""
TooManyRequestsError = "too_many_requests"
ParseError = "parse_error"
ValidateError = "validate_error"
TransactionVerificationError = "verify_transaction"
TransactionSendError = "transaction_send_error"
LeafExistError = "leaf_exist"
RecoverKeysError = "recover_keys"
WasmNotSupportedError = "wasm_not_supported"
WasmNotInitializedError = "wasm_not_initialized"
KeysGenerationError = "generate_keys"
WalletMarshalError = "wallet_marshal"
InvalidReferencePathError = "invalid_reference_path"
InvalidListPathError = "invalid_list_path"
FileNotFoundError = "file_not_found"
InvalidInputLengthError = "invalid_input_length"
InvalidRefPathError = "invalid_ref_path"
WriteMarkerValidationError = "write_marker_validation_failed"
SdkNotInitializedError = "sdk_not_initialized"
InvalidPathError = "invalid_path"
InvalidNameError = "invalid_name"
AuthTicketDecodeError = "auth_ticket_decode_error"
ConsensusNotReachedError = "consensus_not_reached"
ConsensusFailedError = "consensus_failed"
)

var (
ErrUnknown = zerrors.New(UnknownCodeError, "")
ErrTooManyRequests = zerrors.New(TooManyRequestsError, "")
ErrParse = zerrors.New(ParseError, "")
ErrValidate = zerrors.New(ValidateError, "")
ErrTransactionVerification = zerrors.New(TransactionVerificationError, "")
ErrTransactionSend = zerrors.New(TransactionSendError, "")
ErrLeafExist = zerrors.New(LeafExistError, "")
ErrRecoverKeys = zerrors.New(RecoverKeysError, "")
ErrWasmNotSupported = zerrors.New(WasmNotSupportedError, "")
ErrWasmNotInitialized = zerrors.New(WasmNotInitializedError, "")
ErrKeysGeneration = zerrors.New(KeysGenerationError, "")
ErrWalletMarshal = zerrors.New(WalletMarshalError, "")
ErrInvalidReferencePath = zerrors.New(InvalidReferencePathError, "")
ErrInvalidListPath = zerrors.New(InvalidListPathError, "")
ErrFileNotFound = zerrors.New(FileNotFoundError, "")
ErrInvalidInputLength = zerrors.New(InvalidInputLengthError, "")
ErrInvalidRefPath = zerrors.New(InvalidRefPathError, "")
ErrWriteMarkerValidation = zerrors.New(WriteMarkerValidationError, "")
ErrSdkNotInitialized = zerrors.New(SdkNotInitializedError, "")
ErrInvalidPath = zerrors.New(InvalidPathError, "")
ErrInvalidName = zerrors.New(InvalidNameError, "")
ErrAuthTicketDecode = zerrors.New(AuthTicketDecodeError, "")
ErrConsensusNotReached = zerrors.New(ConsensusNotReachedError, "")
ErrConsensusFailed = zerrors.New(ConsensusFailedError, "")
)
34 changes: 34 additions & 0 deletions core/errors/codes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gosdkError

const (
TooManyRequests = "too_many_requests"
AttrChangeProcessError = "attributes_change_process"
FileNotFound = "file_not_found"
InvalidValue = "invalid_value"
InvalidReferencePath = "invalid_reference_path"
MarshallError = "marshall_error"
UnmarshallError = "unmarshall_error"
DecodeError = "decode_error"
// Erasure Coding errors
ECError = "ec_error"
ECSplitError = "ec_split_error"
ECVerifyError = "ec_verify_error"
ECReconstructError = "ec_reconstruct_error"
ECEncodingError = "ec_encoding_error"
ECInvalidInputLength = "ec_invalid_input_length"
ECJoinError = "ec_join_error"

// Encryption/Decryption
EncryptError = "encrypt_error"
DecryptError = "decrypt_error"
ReEncryptError = "reencrypt_error"
ReDecryptError = "redecrypt_error"
InvalidCipherText = "invalid_cipher_text"
SymmetricDecryptionError = "sym_decryption_error"
SymmetricEncryptionError = "sym_encryption_error"
InvalidHeaderChecksums = "invalid_header_checksums"

//
InvalidListPath = "invalid_list_path"
InvalidRefPath = "invalid_ref_path"
)
32 changes: 32 additions & 0 deletions core/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gosdkError

import "github.com/0chain/errors"

var (
ErrTooManyRequests = errors.New(TooManyRequests, "")
ErrFileNotFound = errors.New(FileNotFound, "")
ErrInvalidReferencePath = errors.New(InvalidReferencePath, "")
ErrInvalidValue = errors.New(InvalidValue, "")
ErrMarshall = errors.New(MarshallError, "")
ErrUnmarshall = errors.New(UnmarshallError, "")
ErrDecode = errors.New(DecodeError, "")
// Erasure Coding Errors
ErrEC = errors.New(ECError, "")
ErrECSplit = errors.New(ECSplitError, "")
ErrECVerify = errors.New(ECVerifyError, "")
ErrECReconstruct = errors.New(ECReconstructError, "")
ErrECInvalidInputLength = errors.New(ECInvalidInputLength, "")
ErrECJoin = errors.New(ECJoinError, "")
// Encryption/decryption errors
ErrDecrypt = errors.New(DecryptError, "")
ErrEncrypt = errors.New(EncryptError, "")
ErrReEncrypt = errors.New(ReEncryptError, "")
ErrReDecrypt = errors.New(ReDecryptError, "")
ErrInvalidCipherText = errors.New(InvalidCipherText, "")
ErrSymmetricDecryption = errors.New(SymmetricDecryptionError, "")
ErrSymmetricEncryption = errors.New(SymmetricEncryptionError, "")
ErrInvalidHeaderChecksums = errors.New(InvalidHeaderChecksums, "")

ErrInvalidListPath = errors.New(InvalidListPath, "")
ErrInvalidRefPath = errors.New(InvalidRefPath, "")
)
5 changes: 3 additions & 2 deletions zboxcore/allocationchange/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"

"github.com/0chain/errors"
gosdkError "github.com/0chain/gosdk/core/errors"
"github.com/0chain/gosdk/zboxcore/fileref"
)

Expand Down Expand Up @@ -39,7 +40,7 @@ func (ac *AttributesChange) ProcessChange(root *fileref.Ref) (err error) {
if found {
treelevel++
} else {
return errors.New("attributes_change_process",
return errors.New(gosdkError.AttrChangeProcessError,
"Invalid reference path from the blobber")
}
}
Expand All @@ -57,7 +58,7 @@ func (ac *AttributesChange) ProcessChange(root *fileref.Ref) (err error) {
}

if idx < 0 || file == nil {
return errors.New("attributes_change_process",
return errors.New(gosdkError.AttrChangeProcessError,
"File, to update attributes for, not found in blobber")
}

Expand Down
3 changes: 2 additions & 1 deletion zboxcore/allocationchange/copyobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/0chain/errors"
gosdkError "github.com/0chain/gosdk/core/errors"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/zboxutil"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func (ch *CopyFileChange) ProcessChange(rootRef *fileref.Ref) error {
}

if dirRef.GetPath() != ch.DestPath || dirRef.GetType() != fileref.DIRECTORY {
return errors.New("file_not_found", "Object to copy not found in blobber")
return errors.New(gosdkError.FileNotFound, "Object to copy not found in blobber")
}

var affectedRef *fileref.Ref
Expand Down
5 changes: 3 additions & 2 deletions zboxcore/allocationchange/deletefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"

"github.com/0chain/errors"
gosdkError "github.com/0chain/gosdk/core/errors"
"github.com/0chain/gosdk/zboxcore/fileref"
)

Expand Down Expand Up @@ -39,7 +40,7 @@ func (ch *DeleteFileChange) ProcessChange(rootRef *fileref.Ref) error {
if found {
treelevel++
} else {
return errors.New("invalid_reference_path", "Invalid reference path from the blobber")
return errors.New(gosdkError.InvalidReferencePath, "Invalid reference path from the blobber")
}
}
for i, child := range dirRef.Children {
Expand All @@ -49,7 +50,7 @@ func (ch *DeleteFileChange) ProcessChange(rootRef *fileref.Ref) error {
return nil
}
}
return errors.New("file_not_found", "File to delete not found in blobber")
return errors.New(gosdkError.FileNotFound, "File to delete not found in blobber")
}

func (n *DeleteFileChange) GetAffectedPath() string {
Expand Down
5 changes: 3 additions & 2 deletions zboxcore/allocationchange/renameobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"

"github.com/0chain/errors"
gosdkError "github.com/0chain/gosdk/core/errors"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/zboxutil"
)
Expand Down Expand Up @@ -33,7 +34,7 @@ func (ch *RenameFileChange) ProcessChange(rootRef *fileref.Ref) error {
if found {
treelevel++
} else {
return errors.New("invalid_reference_path", "Invalid reference path from the blobber")
return errors.New(gosdkError.InvalidReferencePath, "Invalid reference path from the blobber")
}
}
idx := -1
Expand All @@ -44,7 +45,7 @@ func (ch *RenameFileChange) ProcessChange(rootRef *fileref.Ref) error {
}
}
if idx < 0 {
return errors.New("file_not_found", "Object to rename not found in blobber")
return errors.New(gosdkError.FileNotFound, "Object to rename not found in blobber")
}
dirRef.Children[idx] = ch.ObjectTree
// Logger.Info("Old name: " + dirRef.Children[idx].GetName())
Expand Down
5 changes: 3 additions & 2 deletions zboxcore/allocationchange/updatefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"

"github.com/0chain/errors"
gosdkError "github.com/0chain/gosdk/core/errors"
"github.com/0chain/gosdk/zboxcore/fileref"
)

Expand Down Expand Up @@ -32,7 +33,7 @@ func (ch *UpdateFileChange) ProcessChange(rootRef *fileref.Ref) error {
if found {
treelevel++
} else {
return errors.New("invalid_reference_path", "Invalid reference path from the blobber")
return errors.New(gosdkError.InvalidReferencePath, "Invalid reference path from the blobber")
}
}
idx := -1
Expand All @@ -44,7 +45,7 @@ func (ch *UpdateFileChange) ProcessChange(rootRef *fileref.Ref) error {
}
}
if idx < 0 || ch.OldFile == nil {
return errors.New("file_not_found", "File to update not found in blobber")
return errors.New(gosdkError.FileNotFound, "File to update not found in blobber")
}
dirRef.Children[idx] = ch.NewFile
rootRef.CalculateHash()
Expand Down
5 changes: 4 additions & 1 deletion zboxcore/blockchain/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package blockchain
import (
"encoding/json"
"sync/atomic"

"github.com/0chain/errors"
gosdkError "github.com/0chain/gosdk/core/errors"
)

type ChainConfig struct {
Expand Down Expand Up @@ -39,7 +42,7 @@ func (sn *StorageNode) IsSkip() bool {
func PopulateNodes(nodesjson string) ([]string, error) {
sharders := make([]string, 0)
err := json.Unmarshal([]byte(nodesjson), &sharders)
return sharders, err
return sharders, errors.New(gosdkError.MarshallError, err.Error())
}

var chain *ChainConfig
Expand Down
5 changes: 4 additions & 1 deletion zboxcore/client/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package client
import (
"encoding/json"

"github.com/0chain/errors"

gosdkError "github.com/0chain/gosdk/core/errors"
"github.com/0chain/gosdk/core/sys"
"github.com/0chain/gosdk/core/zcncrypto"
)
Expand Down Expand Up @@ -48,7 +51,7 @@ func PopulateClients(clientJsons []string, signatureScheme string) error {
for _, clientJson := range clientJsons {
c := new(Client)
if err := json.Unmarshal([]byte(clientJson), c); err != nil {
return err
return errors.New(gosdkError.MarshallError, err.Error())
}
c.SignatureScheme = signatureScheme
clients = append(clients, c)
Expand Down
16 changes: 9 additions & 7 deletions zboxcore/encoder/erasurecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"bufio"
"bytes"

"errors"
"github.com/0chain/errors"

gosdkError "github.com/0chain/gosdk/core/errors"
l "github.com/0chain/gosdk/zboxcore/logger"

"github.com/klauspost/reedsolomon"
Expand All @@ -25,7 +26,7 @@ func NewEncoder(iDataShards, iParityShards int) (*StreamEncoder, error) {

e.erasureCode, err = reedsolomon.New(iDataShards, iParityShards, reedsolomon.WithAutoGoroutines(64*1024))
if err != nil {
return nil, err
return nil, errors.New(gosdkError.ECError, err.Error())
}
e.iDataShards = iDataShards
e.iParityShards = iParityShards
Expand All @@ -38,7 +39,7 @@ func (e *StreamEncoder) Encode(in []byte) ([][]byte, error) {
e.data, err = e.erasureCode.Split(in)
if err != nil {
l.Logger.Error("Split failed", err.Error())
return [][]byte{}, err
return [][]byte{}, errors.New(gosdkError.ECSplitError, err.Error())
}

err = e.erasureCode.Encode(e.data)
Expand All @@ -52,18 +53,18 @@ func (e *StreamEncoder) Encode(in []byte) ([][]byte, error) {
func (e *StreamEncoder) Decode(in [][]byte, shardSize int) ([]byte, error) {
// Verify the input
if (len(in) < e.iDataShards+e.iParityShards) || (shardSize <= 0) {
return []byte{}, errors.New("Invalid input length")
return []byte{}, gosdkError.ErrECInvalidInputLength
}

err := e.erasureCode.Reconstruct(in)
if err != nil {
l.Logger.Error("Reconstruct failed -", err)
return []byte{}, err
return []byte{}, errors.New(gosdkError.ECReconstructError, err.Error())
}
_, err = e.erasureCode.Verify(in)
if err != nil {
l.Logger.Error("Verification failed after reconstruction, data likely corrupted.", err.Error())
return []byte{}, err
return []byte{}, errors.New(gosdkError.ECVerifyError, err.Error())
}

var bytesBuf bytes.Buffer
Expand All @@ -72,8 +73,9 @@ func (e *StreamEncoder) Decode(in [][]byte, shardSize int) ([]byte, error) {
err = e.erasureCode.Join(bufWriter, in, (shardSize * e.iDataShards))
if err != nil {
l.Logger.Error("join failed", err.Error())
return []byte{}, err
return []byte{}, errors.New(gosdkError.ECJoinError, err.Error())
}

bufWriter.Flush()
outBuf := bytesBuf.Bytes()
return outBuf, nil
Expand Down
Loading