Repository: NethermindEth/juno
Pull request #268 (2022-07-04 18:18:25)

Improve database error management
## Changes:
- Change database panics for return errors

## Types of changes

What types of changes does your code introduce?
_Put an `x` in the boxes that apply_

- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation Update
- [ ] Code style update (formatting, renaming)
- [x] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Other (please describe): 

## Testing
**Requires testing**

- [ ] Yes
- [x] No

**In case you checked yes, did you write tests??**

- [ ] Yes
- [ ] No



codecov[bot]: # [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/268?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) Report
> Merging [#268](https://codecov.io/gh/NethermindEth/juno/pull/268?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (309e21a) into [main](https://codecov.io/gh/NethermindEth/juno/commit/cbd368b222ac19a69e75e8ab82f01443ad21f590?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (cbd368b) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff            @@
##              main      #268   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           65        65           
  Lines         5907      5845   -62     
=========================================
- Hits          5907      5845   -62     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `100.00% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/NethermindEth/juno/pull/268?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) | Coverage Δ | |
|---|---|---|
| [pkg/starknet/state.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL3N0YXJrbmV0L3N0YXRlLmdv) | `100.00% <ø> (ø)` | |
| [internal/db/abi/manager.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-aW50ZXJuYWwvZGIvYWJpL21hbmFnZXIuZ28=) | `100.00% <100.00%> (ø)` | |
| [internal/db/block/manager.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-aW50ZXJuYWwvZGIvYmxvY2svbWFuYWdlci5nbw==) | `100.00% <100.00%> (ø)` | |
| [internal/db/state/code.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-aW50ZXJuYWwvZGIvc3RhdGUvY29kZS5nbw==) | `100.00% <100.00%> (ø)` | |
| [internal/db/state/storage.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-aW50ZXJuYWwvZGIvc3RhdGUvc3RvcmFnZS5nbw==) | `100.00% <100.00%> (ø)` | |
| [internal/db/transaction/manager.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-aW50ZXJuYWwvZGIvdHJhbnNhY3Rpb24vbWFuYWdlci5nbw==) | `100.00% <100.00%> (ø)` | |
| [internal/services/abi.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-aW50ZXJuYWwvc2VydmljZXMvYWJpLmdv) | `100.00% <100.00%> (ø)` | |
| [internal/services/block.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-aW50ZXJuYWwvc2VydmljZXMvYmxvY2suZ28=) | `100.00% <100.00%> (ø)` | |
| [internal/services/contractHash.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-aW50ZXJuYWwvc2VydmljZXMvY29udHJhY3RIYXNoLmdv) | `100.00% <100.00%> (ø)` | |
| [internal/services/state.go](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-aW50ZXJuYWwvc2VydmljZXMvc3RhdGUuZ28=) | `100.00% <100.00%> (ø)` | |
| ... and [9 more](https://codecov.io/gh/NethermindEth/juno/pull/268/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/NethermindEth/juno/pull/268?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/268?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Last update [cbd368b...309e21a](https://codecov.io/gh/NethermindEth/juno/pull/268?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).


Patch:
diff --git a/internal/db/abi/manager.go b/internal/db/abi/manager.go
index ab084e43..4afa0a4e 100644
--- a/internal/db/abi/manager.go
+++ b/internal/db/abi/manager.go
@@ -1,19 +1,10 @@
 package abi
 
 import (
-	"errors"
-	"fmt"
-
 	"github.com/NethermindEth/juno/internal/db"
 	"google.golang.org/protobuf/proto"
 )
 
-var (
-	DbError        = errors.New("database error")
-	UnmarshalError = errors.New("unmarshal error")
-	MarshalError   = errors.New("marshal error")
-)
-
 // Manager is a database to store and get the contracts ABI.
 type Manager struct {
 	database db.Database
@@ -26,45 +17,34 @@ func NewABIManager(database db.Database) *Manager {
 
 // GetABI gets the ABI associated with the contract address. If the ABI does
 // not exist, then returns nil without error.
-func (m *Manager) GetABI(contractAddress string) *Abi {
+func (m *Manager) GetABI(contractAddress string) (*Abi, error) {
 	// Build the key from contract address
 	key := []byte(contractAddress)
 	// Query to database
 	data, err := m.database.Get(key)
 	if err != nil {
-		if db.ErrNotFound == err {
-			return nil
-		}
-		// notest
-		panic(any(fmt.Errorf("%w: %s", DbError, err)))
-	}
-	if data == nil {
-		// notest
-		return nil
+		return nil, err
 	}
 	// Unmarshal the data from database
 	abi := new(Abi)
 	if err := proto.Unmarshal(data, abi); err != nil {
-		// notest
-		panic(any(fmt.Errorf("%w: %s", UnmarshalError, err.Error())))
+		return nil, err
 	}
-	return abi
+	return abi, nil
 }
 
 // PutABI puts the ABI to the contract address.
-func (m *Manager) PutABI(contractAddress string, abi *Abi) {
+func (m *Manager) PutABI(contractAddress string, abi *Abi) error {
 	// Build the key from contract address
 	key := []byte(contractAddress)
 	value, err := proto.Marshal(abi)
 	if err != nil {
-		// notest
-		panic(any(fmt.Errorf("%w: %s", MarshalError, err)))
+		return err
 	}
-	err = m.database.Put(key, value)
-	if err != nil {
-		// notest
-		panic(any(fmt.Errorf("%w: %s", DbError, err.Error())))
+	if err := m.database.Put(key, value); err != nil {
+		return err
 	}
+	return nil
 }
 
 // Close closes the associated database
diff --git a/internal/db/block/manager.go b/internal/db/block/manager.go
index edfc8d5f..96dcff6f 100644
--- a/internal/db/block/manager.go
+++ b/internal/db/block/manager.go
@@ -20,82 +20,68 @@ func NewManager(database db.Database) *Manager {
 
 // GetBlockByHash search the block with the given block hash. If the block does
 // not exist then returns nil. If any error happens, then panic.
-func (manager *Manager) GetBlockByHash(blockHash types.BlockHash) *types.Block {
+func (manager *Manager) GetBlockByHash(blockHash types.BlockHash) (*types.Block, error) {
 	// Build the hash key
 	hashKey := buildHashKey(blockHash)
 	// Search on the database
 	rawResult, err := manager.database.Get(hashKey)
 	if err != nil {
-		panic(any(err))
-	}
-	// Check not found
-	if rawResult == nil {
-		// notest
-		return nil
+		return nil, err
 	}
 	// Unmarshal the data
 	block, err := unmarshalBlock(rawResult)
 	if err != nil {
-		panic(any(err))
+		return nil, err
 	}
-	return block
+	return block, nil
 }
 
 // GetBlockByNumber search the block with the given block number. If the block
 // does not exist then returns nil. If any error happens, then panic.
-func (manager *Manager) GetBlockByNumber(blockNumber uint64) *types.Block {
+func (manager *Manager) GetBlockByNumber(blockNumber uint64) (*types.Block, error) {
 	// Build the number key
 	numberKey := buildNumberKey(blockNumber)
 	// Search for the hash key
 	hashKey, err := manager.database.Get(numberKey)
 	if err != nil {
-		panic(any(err))
-	}
-	// Check not found
-	if hashKey == nil {
-		// notest
-		return nil
+		return nil, err
 	}
 	// Search for the block
 	rawResult, err := manager.database.Get(hashKey)
 	if err != nil {
-		panic(any(err))
-	}
-	// Check not found
-	if rawResult == nil {
-		// notest
-		return nil
+		return nil, err
 	}
 	// Unmarshal the data
 	block, err := unmarshalBlock(rawResult)
 	if err != nil {
-		panic(any(err))
+		return nil, err
 	}
-	return block
+	return block, nil
 }
 
 // PutBlock saves the given block with the given hash as key. If any error happens
 // then panic.
-func (manager *Manager) PutBlock(blockHash types.BlockHash, block *types.Block) {
+func (manager *Manager) PutBlock(blockHash types.BlockHash, block *types.Block) error {
 	// Build the keys
 	hashKey := buildHashKey(blockHash)
 	numberKey := buildNumberKey(block.BlockNumber)
 	// Encode the block as []byte
 	rawValue, err := marshalBlock(block)
 	if err != nil {
-		panic(any(err))
+		return err
 	}
 	// TODO: Use transaction?
 	// Save (hashKey, block)
 	err = manager.database.Put(hashKey, rawValue)
 	if err != nil {
-		panic(any(err))
+		return err
 	}
 	// Save (hashNumber, hashKey)
 	err = manager.database.Put(numberKey, hashKey)
 	if err != nil {
-		panic(any(err))
+		return err
 	}
+	return nil
 }
 
 func (manager *Manager) Close() {
diff --git a/internal/db/block_specific.go b/internal/db/block_specific.go
index 73e09a97..93b29df1 100644
--- a/internal/db/block_specific.go
+++ b/internal/db/block_specific.go
@@ -3,6 +3,7 @@ package db
 import (
 	"encoding/binary"
 	"encoding/json"
+	"errors"
 	"fmt"
 
 	. "github.com/NethermindEth/juno/internal/log"
@@ -86,7 +87,7 @@ func (db *BlockSpecificDatabase) get(key []byte) []byte {
 	data, err := db.database.Get(key)
 	if err != nil {
 		// notest
-		if IsNotFound(err) {
+		if errors.Is(err, ErrNotFound) {
 			return nil
 		}
 		panicWithError(err)
diff --git a/internal/db/mdbx.go b/internal/db/mdbx.go
index 641d650b..e29720d2 100644
--- a/internal/db/mdbx.go
+++ b/internal/db/mdbx.go
@@ -196,11 +196,6 @@ func (tx MDBXTransaction) NumberOfItems() (uint64, error) {
 	return numberOfItems(tx.txn, tx.dbi)
 }
 
-// IsNotFound checks is the given error is an ErrNotFound.
-func IsNotFound(err error) bool {
-	return errors.Is(err, ErrNotFound)
-}
-
 func has(txn *mdbx.Txn, dbi mdbx.DBI, key []byte) (bool, error) {
 	_, err := txn.Get(dbi, key)
 	if err != nil {
diff --git a/internal/db/state/code.go b/internal/db/state/code.go
index 555c19e8..8560d0bc 100644
--- a/internal/db/state/code.go
+++ b/internal/db/state/code.go
@@ -1,38 +1,33 @@
 package state
 
 import (
-	"fmt"
-
 	"google.golang.org/protobuf/proto"
 )
 
 // GetCode returns the ContractCode associated with the given contract address.
 // If the contract code is not found, then nil is returned.
-func (x *Manager) GetCode(contractAddress []byte) *Code {
+func (x *Manager) GetCode(contractAddress []byte) (*Code, error) {
 	rawData, err := x.codeDatabase.Get(contractAddress)
 	if err != nil {
-		panic(any(fmt.Errorf("database error: %s", err)))
-	}
-	if rawData == nil {
-		// notest
-		return nil
+		return nil, err
 	}
 	code := new(Code)
 	if err := proto.Unmarshal(rawData, code); err != nil {
-		panic(any(fmt.Errorf("unmarshal error: %s", err)))
+		return nil, err
 	}
-	return code
+	return code, nil
 }
 
 // PutCode stores a new contract code into the database, associated with the
 // given contract address. If the contract address already have a contract code
 // in the database, then the value is updated.
-func (x *Manager) PutCode(contractAddress []byte, code *Code) {
+func (x *Manager) PutCode(contractAddress []byte, code *Code) error {
 	rawData, err := proto.Marshal(code)
 	if err != nil {
-		panic(any(fmt.Errorf("marshal error: %s", err)))
+		return err
 	}
 	if err := x.codeDatabase.Put(contractAddress, rawData); err != nil {
-		panic(any(fmt.Errorf("database error: %s", err)))
+		return err
 	}
+	return nil
 }
diff --git a/internal/db/state/storage.go b/internal/db/state/storage.go
index 783dd1b2..4bde697b 100644
--- a/internal/db/state/storage.go
+++ b/internal/db/state/storage.go
@@ -1,8 +1,6 @@
 package state
 
 import (
-	"fmt"
-
 	"google.golang.org/protobuf/proto"
 )
 
@@ -20,33 +18,27 @@ func (s *Storage) Update(other *Storage) {
 // and block number. If no exists a version for exactly the given block number,
 // then returns the newest version lower than the given block number. If the
 // contract storage does not exist then returns nil.
-func (x *Manager) GetStorage(contractAddress string, blockNumber uint64) *Storage {
+func (x *Manager) GetStorage(contractAddress string, blockNumber uint64) (*Storage, error) {
 	rawData, err := x.storageDatabase.Get([]byte(contractAddress), blockNumber)
 	if err != nil {
-		panic(any(fmt.Errorf("database error: %s", err)))
-	}
-	// Check not found
-	if rawData == nil {
-		// notest
-		return nil
+		return nil, err
 	}
 	value := new(Storage)
-	err = proto.Unmarshal(rawData, value)
-	if err != nil {
-		panic(any(fmt.Errorf("unmarshal error: %s", err)))
+	if err := proto.Unmarshal(rawData, value); err != nil {
+		return nil, err
 	}
-	return value
+	return value, nil
 }
 
 // PutStorage saves a new version of the contract storage at the given block
 // number.
-func (x *Manager) PutStorage(contractAddress string, blockNumber uint64, storage *Storage) {
+func (x *Manager) PutStorage(contractAddress string, blockNumber uint64, storage *Storage) error {
 	rawValue, err := proto.Marshal(storage)
 	if err != nil {
-		panic(any(fmt.Errorf("marshal error: %s", err)))
+		return err
 	}
-	err = x.storageDatabase.Put([]byte(contractAddress), blockNumber, rawValue)
-	if err != nil {
-		panic(any(fmt.Errorf("database error: %s", err)))
+	if err := x.storageDatabase.Put([]byte(contractAddress), blockNumber, rawValue); err != nil {
+		return err
 	}
+	return nil
 }
diff --git a/internal/db/transaction/manager.go b/internal/db/transaction/manager.go
index e3561ba6..e84a3d6f 100644
--- a/internal/db/transaction/manager.go
+++ b/internal/db/transaction/manager.go
@@ -4,7 +4,6 @@ import (
 	"fmt"
 
 	"github.com/NethermindEth/juno/internal/db"
-	. "github.com/NethermindEth/juno/internal/log"
 	"github.com/NethermindEth/juno/pkg/types"
 	"google.golang.org/protobuf/proto"
 )
@@ -25,75 +24,57 @@ func NewManager(txDb, receiptDb db.Database) *Manager {
 // PutTransaction stores new transactions in the database. This method does not
 // check if the key already exists. In the case, that the key already exists the
 // value is overwritten.
-func (m *Manager) PutTransaction(txHash types.TransactionHash, tx types.IsTransaction) {
+func (m *Manager) PutTransaction(txHash types.TransactionHash, tx types.IsTransaction) error {
 	rawData, err := marshalTransaction(tx)
 	if err != nil {
-		// notest
-		Logger.With("error", err).Panic("error marshalling Transaction")
+		return err
 	}
-	err = m.txDb.Put(txHash.Bytes(), rawData)
-	if err != nil {
-		// notest
-		Logger.With("error", err).Panicf("database error")
+	if err := m.txDb.Put(txHash.Bytes(), rawData); err != nil {
+		return err
 	}
+	return nil
 }
 
 // GetTransaction searches in the database for the transaction associated with the
 // given key. If the key does not exist then returns nil.
-func (m *Manager) GetTransaction(txHash types.TransactionHash) types.IsTransaction {
+func (m *Manager) GetTransaction(txHash types.TransactionHash) (types.IsTransaction, error) {
 	rawData, err := m.txDb.Get(txHash.Bytes())
 	if err != nil {
-		// notest
-		Logger.With("error", err).Panicf("database error")
-	}
-	// Check not found
-	if rawData == nil {
-		// notest
-		return nil
+		return nil, err
 	}
 	tx, err := unmarshalTransaction(rawData)
 	if err != nil {
-		// notest
-		Logger.With("error", err).Panicf("unmarshalling error")
+		return nil, err
 	}
-	return tx
+	return tx, nil
 }
 
 // PutReceipt stores  new transactions receipts in the database. This method
 // does not check if the key already exists. In the case, that the key already
 // exists the value is overwritten.
-func (m *Manager) PutReceipt(txHash types.TransactionHash, txReceipt *types.TransactionReceipt) {
+func (m *Manager) PutReceipt(txHash types.TransactionHash, txReceipt *types.TransactionReceipt) error {
 	rawData, err := marshalTransactionReceipt(txReceipt)
 	if err != nil {
-		// notest
-		Logger.With("error", err).Panic("error marshaling TransactionReceipt")
+		return err
 	}
-	err = m.receiptDb.Put(txHash.Bytes(), rawData)
-	if err != nil {
-		// notest
-		Logger.With("error", err).Panic("database error")
+	if err := m.receiptDb.Put(txHash.Bytes(), rawData); err != nil {
+		return err
 	}
+	return nil
 }
 
 // GetReceipt searches in the database for the transaction receipt associated
 // with the given key. If the key does not exist then returns nil.
-func (m *Manager) GetReceipt(txHash types.TransactionHash) *types.TransactionReceipt {
+func (m *Manager) GetReceipt(txHash types.TransactionHash) (*types.TransactionReceipt, error) {
 	rawData, err := m.receiptDb.Get(txHash.Bytes())
 	if err != nil {
-		// notest
-		Logger.With("error", err).Panicf("database error")
-	}
-	// Check not found
-	if rawData == nil {
-		// notest
-		return nil
+		return nil, err
 	}
 	receipt, err := unmarshalTransactionReceipt(rawData)
 	if err != nil {
-		// notest
-		Logger.With("error", err).Panicf("unmarshalling error")
+		return nil, err
 	}
-	return receipt
+	return receipt, nil
 }
 
 // Close closes the manager, specific the associated database.
@@ -128,8 +109,7 @@ func marshalTransaction(transaction types.IsTransaction) ([]byte, error) {
 
 func unmarshalTransaction(b []byte) (types.IsTransaction, error) {
 	var protoTx Transaction
-	err := proto.Unmarshal(b, &protoTx)
-	if err != nil {
+	if err := proto.Unmarshal(b, &protoTx); err != nil {
 		return nil, err
 	}
 	if tx := protoTx.GetInvoke(); tx != nil {
@@ -180,8 +160,7 @@ func marshalTransactionReceipt(receipt *types.TransactionReceipt) ([]byte, error
 
 func unmarshalTransactionReceipt(b []byte) (*types.TransactionReceipt, error) {
 	var protoReceipt TransactionReceipt
-	err := proto.Unmarshal(b, &protoReceipt)
-	if err != nil {
+	if err := proto.Unmarshal(b, &protoReceipt); err != nil {
 		return nil, err
 	}
 	receipt := &types.TransactionReceipt{
diff --git a/internal/services/abi.go b/internal/services/abi.go
index 161382ab..e5a50f35 100644
--- a/internal/services/abi.go
+++ b/internal/services/abi.go
@@ -71,26 +71,16 @@ func (s *abiService) Close(ctx context.Context) {
 
 // StoreAbi stores an ABI in the database. If the key (contractAddress) already
 // exists then the value is overwritten for the given ABI.
-func (s *abiService) StoreAbi(contractAddress string, abi *abi.Abi) {
+func (s *abiService) StoreAbi(contractAddress string, abi *abi.Abi) error {
 	s.service.AddProcess()
 	defer s.service.DoneProcess()
-
-	s.logger.
-		With("contractAddress", contractAddress).
-		Info("StoreAbi")
-
-	s.manager.PutABI(contractAddress, abi)
+	return s.manager.PutABI(contractAddress, abi)
 }
 
 // GetAbi search in the database for the ABI associated with the given contract
 // address.
-func (s *abiService) GetAbi(contractAddress string) *abi.Abi {
+func (s *abiService) GetAbi(contractAddress string) (*abi.Abi, error) {
 	s.service.AddProcess()
 	defer s.service.DoneProcess()
-
-	s.logger.
-		With("contractAddress", contractAddress).
-		Info("GetAbi")
-
 	return s.manager.GetABI(contractAddress)
 }
diff --git a/internal/services/block.go b/internal/services/block.go
index 5f1757ff..10fbc7f0 100644
--- a/internal/services/block.go
+++ b/internal/services/block.go
@@ -74,40 +74,25 @@ func (s *blockService) Close(ctx context.Context) {
 
 // GetBlockByHash searches for the block associated with the given block hash.
 // If the block does not exist on the database, then returns nil.
-func (s *blockService) GetBlockByHash(blockHash types.BlockHash) *types.Block {
+func (s *blockService) GetBlockByHash(blockHash types.BlockHash) (*types.Block, error) {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("blockHash", blockHash).
-		Debug("GetBlockByHash")
-
 	return s.manager.GetBlockByHash(blockHash)
 }
 
 // GetBlockByNumber searches for the block associated with the given block
 // number. If the block does not exist on the database, then returns nil.
-func (s *blockService) GetBlockByNumber(blockNumber uint64) *types.Block {
+func (s *blockService) GetBlockByNumber(blockNumber uint64) (*types.Block, error) {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("blockNumber", blockNumber).
-		Debug("GetBlockByNumber")
-
 	return s.manager.GetBlockByNumber(blockNumber)
 }
 
 // StoreBlock stores the given block into the database. The key used to map the
 // block it's the hash of the block. If the database already has a block with
 // the same key, then the value is overwritten.
-func (s *blockService) StoreBlock(blockHash types.BlockHash, block *types.Block) {
+func (s *blockService) StoreBlock(blockHash types.BlockHash, block *types.Block) error {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("blockHash", blockHash.Hex()).
-		Debug("StoreBlock")
-
-	s.manager.PutBlock(blockHash, block)
+	return s.manager.PutBlock(blockHash, block)
 }
diff --git a/internal/services/contractHash.go b/internal/services/contractHash.go
index 25fde15c..cd3074e6 100644
--- a/internal/services/contractHash.go
+++ b/internal/services/contractHash.go
@@ -60,38 +60,16 @@ func (s *contractHashService) Close(ctx context.Context) {
 	s.db.Close()
 }
 
-func (s *contractHashService) StoreContractHash(contractAddress string, contractHash *big.Int) {
+func (s *contractHashService) StoreContractHash(contractAddress string, contractHash *big.Int) error {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("contractAddress", contractAddress).
-		Debug("StoreContractHash")
-
-	err := s.db.Put([]byte(contractAddress), contractHash.Bytes())
-	if err != nil {
-		// notest
-		s.logger.
-			With("error", err).
-			Error("StoreContractHash error")
-	}
+	return s.db.Put([]byte(contractAddress), contractHash.Bytes())
 }
 
-func (s *contractHashService) GetContractHash(contractAddress string) *big.Int {
+func (s *contractHashService) GetContractHash(contractAddress string) (*big.Int, error) {
 	// notest
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("contractAddress", contractAddress).
-		Debug("GetContractHash")
-
 	rawData, err := s.db.Get([]byte(contractAddress))
-	if err != nil {
-		s.logger.
-			With("error", err).
-			Error("GetContractHash error")
-		return nil
-	}
-	return new(big.Int).SetBytes(rawData)
+	return new(big.Int).SetBytes(rawData), err
 }
diff --git a/internal/services/state.go b/internal/services/state.go
index e15f16cf..b0272c11 100644
--- a/internal/services/state.go
+++ b/internal/services/state.go
@@ -6,7 +6,6 @@ import (
 	"github.com/NethermindEth/juno/internal/db"
 	"github.com/NethermindEth/juno/internal/db/state"
 	. "github.com/NethermindEth/juno/internal/log"
-	"github.com/ethereum/go-ethereum/common"
 )
 
 var StateService stateService
@@ -67,64 +66,42 @@ func (s *stateService) Close(ctx context.Context) {
 	s.manager.Close()
 }
 
-func (s *stateService) StoreCode(contractAddress []byte, code *state.Code) {
+func (s *stateService) StoreCode(contractAddress []byte, code *state.Code) error {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("contractAddress", common.Bytes2Hex(contractAddress)).
-		Debug("StoreCode")
-
-	s.manager.PutCode(contractAddress, code)
+	return s.manager.PutCode(contractAddress, code)
 }
 
-func (s *stateService) GetCode(contractAddress []byte) *state.Code {
+func (s *stateService) GetCode(contractAddress []byte) (*state.Code, error) {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("contractAddress", contractAddress).
-		Debug("GetCode")
-
 	return s.manager.GetCode(contractAddress)
 }
 
-func (s *stateService) StoreStorage(contractAddress string, blockNumber uint64, storage *state.Storage) {
+func (s *stateService) StoreStorage(contractAddress string, blockNumber uint64, storage *state.Storage) error {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("contractAddress", contractAddress, "blockNumber", blockNumber).
-		Debug("StoreStorage")
-
-	s.manager.PutStorage(contractAddress, blockNumber, storage)
+	return s.manager.PutStorage(contractAddress, blockNumber, storage)
 }
 
-func (s *stateService) GetStorage(contractAddress string, blockNumber uint64) *state.Storage {
+func (s *stateService) GetStorage(contractAddress string, blockNumber uint64) (*state.Storage, error) {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("contractAddress", contractAddress, "blockNumber", blockNumber).
-		Debug("GetStorage")
-
 	return s.manager.GetStorage(contractAddress, blockNumber)
 }
 
-func (s *stateService) UpdateStorage(contractAddress string, blockNumber uint64, storage *state.Storage) {
+func (s *stateService) UpdateStorage(contractAddress string, blockNumber uint64, storage *state.Storage) error {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("contractAddress", contractAddress, "blockNumber", blockNumber).
-		Debug("UpdateStorage")
-
-	oldStorage := s.GetStorage(contractAddress, blockNumber)
+	oldStorage, err := s.GetStorage(contractAddress, blockNumber)
+	if err != nil {
+		return err
+	}
 	if oldStorage == nil {
 		// notest
-		s.StoreStorage(contractAddress, blockNumber, storage)
+		return s.StoreStorage(contractAddress, blockNumber, storage)
 	} else {
 		oldStorage.Update(storage)
-		s.StoreStorage(contractAddress, blockNumber, oldStorage)
+		return s.StoreStorage(contractAddress, blockNumber, oldStorage)
 	}
 }
diff --git a/internal/services/transactions.go b/internal/services/transactions.go
index 31d092f1..58f1a462 100644
--- a/internal/services/transactions.go
+++ b/internal/services/transactions.go
@@ -80,50 +80,34 @@ func (s *transactionService) Close(ctx context.Context) {
 // GetTransaction searches for the transaction associated with the given
 // transaction hash. If the transaction does not exist on the database, then
 // returns nil.
-func (s *transactionService) GetTransaction(txHash types.TransactionHash) types.IsTransaction {
+func (s *transactionService) GetTransaction(txHash types.TransactionHash) (types.IsTransaction, error) {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("txHash", txHash).
-		Debug("GetTransaction")
-
 	return s.manager.GetTransaction(txHash)
 }
 
 // StoreTransaction stores the given transaction into the database. The key used
 // to map the transaction it's the hash of the transaction. If the database
 // already has a transaction with the same key, then the value is overwritten.
-func (s *transactionService) StoreTransaction(txHash types.TransactionHash, tx types.IsTransaction) {
+func (s *transactionService) StoreTransaction(txHash types.TransactionHash, tx types.IsTransaction) error {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.
-		With("txHash", txHash.String()).
-		Debug("StoreTransaction")
-
-	s.manager.PutTransaction(txHash, tx)
+	return s.manager.PutTransaction(txHash, tx)
 }
 
 // GetReceipt searches for the transaction receipt associated with the given
 // transaction hash. If the transaction does not exist on the database, then
 // returns nil.
-func (s *transactionService) GetReceipt(txHash types.TransactionHash) *types.TransactionReceipt {
+func (s *transactionService) GetReceipt(txHash types.TransactionHash) (*types.TransactionReceipt, error) {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.With("txHash", txHash).Debug("GetReceipt")
-
 	return s.manager.GetReceipt(txHash)
 }
 
 // StoreReceipt stores the given transaction receipt into the database. If the
 // database already has a receipt with the same key, the value is overwritten.
-func (s *transactionService) StoreReceipt(txHash types.TransactionHash, receipt *types.TransactionReceipt) {
+func (s *transactionService) StoreReceipt(txHash types.TransactionHash, receipt *types.TransactionReceipt) error {
 	s.AddProcess()
 	defer s.DoneProcess()
-
-	s.logger.With("txHash", txHash).Debug("StoreReceipt")
-
-	s.manager.PutReceipt(txHash, receipt)
+	return s.manager.PutReceipt(txHash, receipt)
 }
diff --git a/pkg/rpc/api.go b/pkg/rpc/api.go
index 4aa3cac6..d06e8942 100644
--- a/pkg/rpc/api.go
+++ b/pkg/rpc/api.go
@@ -6,6 +6,7 @@ import (
 	"errors"
 	"fmt"
 
+	"github.com/NethermindEth/juno/internal/db"
 	dbAbi "github.com/NethermindEth/juno/internal/db/abi"
 	"github.com/NethermindEth/juno/internal/services"
 	"github.com/NethermindEth/juno/pkg/feeder"
@@ -160,13 +161,18 @@ func getBlockByTag(_ context.Context, blockTag BlockTag, scope RequestedScope) (
 
 func getBlockByHash(ctx context.Context, blockHash types.BlockHash, scope RequestedScope) (*BlockResponse, error) {
 	Logger.With("blockHash", blockHash, "scope", scope).Info("StarknetGetBlockByHash")
-	dbBlock := services.BlockService.GetBlockByHash(blockHash)
-	if dbBlock == nil {
+	dbBlock, err := services.BlockService.GetBlockByHash(blockHash)
+	if err != nil {
 		// notest
-		// TODO: Send custom error for not found. Maybe sent InvalidBlockHash?
-		return nil, errors.New("block not found")
+		if errors.Is(err, db.ErrNotFound) {
+			return nil, errors.New("block not found")
+		}
+		return nil, fmt.Errorf("database error: %v", err)
+	}
+	response, err := NewBlockResponse(dbBlock, scope)
+	if err != nil {
+		return nil, fmt.Errorf("error creating block response: %v", err)
 	}
-	response := NewBlockResponse(dbBlock, scope)
 	return response, nil
 }
 
@@ -196,12 +202,18 @@ func (HandlerRPC) StarknetGetBlockByHashOpt(ctx context.Context, blockHashOrTag
 
 func getBlockByNumber(ctx context.Context, blockNumber uint64, scope RequestedScope) (*BlockResponse, error) {
 	Logger.With("blockNumber", blockNumber, "scope", scope).Info("StarknetGetBlockNyNumber")
-	dbBlock := services.BlockService.GetBlockByNumber(blockNumber)
-	if dbBlock == nil {
+	dbBlock, err := services.BlockService.GetBlockByNumber(blockNumber)
+	if err != nil {
 		// notest
-		return nil, errors.New("block not found")
+		if errors.Is(err, db.ErrNotFound) {
+			return nil, errors.New("block not found")
+		}
+		return nil, fmt.Errorf("database error: %v", err)
+	}
+	response, err := NewBlockResponse(dbBlock, scope)
+	if err != nil {
+		return nil, fmt.Errorf("error creating block response: %v", err)
 	}
-	response := NewBlockResponse(dbBlock, scope)
 	return response, nil
 }
 
@@ -264,10 +276,13 @@ func (HandlerRPC) StarknetGetStorageAt(
 ) (Felt, error) {
 	var blockNumber uint64
 	if hash := blockHash.Hash; hash != nil {
-		block := services.BlockService.GetBlockByHash(*blockHash.Hash)
-		if block == nil {
+		block, err := services.BlockService.GetBlockByHash(*blockHash.Hash)
+		if err != nil {
 			// notest
-			return "", fmt.Errorf("block not found")
+			if errors.Is(err, db.ErrNotFound) {
+				return "", fmt.Errorf("block not found")
+			}
+			return "", fmt.Errorf("database error: %v", err)
 		}
 		blockNumber = block.BlockNumber
 	} else if tag := blockHash.Tag; tag != nil {
@@ -282,10 +297,13 @@ func (HandlerRPC) StarknetGetStorageAt(
 		return "", fmt.Errorf("invalid block hash or tag")
 	}
 
-	storage := services.StateService.GetStorage(string(contractAddress), blockNumber)
-	if storage == nil {
+	storage, err := services.StateService.GetStorage(string(contractAddress), blockNumber)
+	if err != nil {
 		// notest
-		return "", fmt.Errorf("storage not found")
+		if errors.Is(err, db.ErrNotFound) {
+			return "", fmt.Errorf("storage not found")
+		}
+		return "", fmt.Errorf("database error: %v", err)
 	}
 
 	return Felt(storage.Storage[string(key)]), nil
@@ -296,11 +314,14 @@ func (HandlerRPC) StarknetGetStorageAt(
 func (HandlerRPC) StarknetGetTransactionByHash(
 	c context.Context, transactionHash types.TransactionHash,
 ) (*Txn, error) {
-	tx := services.TransactionService.GetTransaction(transactionHash)
-	if tx == nil {
+	tx, err := services.TransactionService.GetTransaction(transactionHash)
+	if err != nil {
 		// notest
-		// TODO: return not found error
-		return &Txn{}, nil
+		if errors.Is(err, db.ErrNotFound) {
+			// TODO: return not found error
+			return &Txn{}, nil
+		}
+		return nil, fmt.Errorf("database error: %v", err)
 	}
 	return NewTxn(tx), nil
 }
@@ -310,14 +331,28 @@ func (HandlerRPC) StarknetGetTransactionByHash(
 // no transaction is found, a null value is returned.
 func (HandlerRPC) StarknetGetTransactionByBlockHashAndIndex(c context.Context, blockHashOrTag BlockHashOrTag, index int) (*Txn, error) {
 	if blockHash := blockHashOrTag.Hash; blockHash != nil {
-		block := services.BlockService.GetBlockByHash(*blockHash)
+		block, err := services.BlockService.GetBlockByHash(*blockHash)
+		if err != nil {
+			// notest
+			if errors.Is(err, db.ErrNotFound) {
+				return nil, fmt.Errorf("block not found")
+			}
+			return nil, fmt.Errorf("database error: %v", err)
+		}
 		// TODO check if block is nil?
 		if index < 0 || len(block.TxHashes) <= index {
 			// notest
 			return nil, fmt.Errorf("invalid index %d", index)
 		}
 		txHash := block.TxHashes[index]
-		txn := services.TransactionService.GetTransaction(txHash)
+		txn, err := services.TransactionService.GetTransaction(txHash)
+		if err != nil {
+			// notest
+			if errors.Is(err, db.ErrNotFound) {
+				return nil, fmt.Errorf("transaction not found")
+			}
+			return nil, fmt.Errorf("database error: %v", err)
+		}
 		return NewTxn(txn), nil
 	}
 	// notest
@@ -339,13 +374,27 @@ func (HandlerRPC) StarknetGetTransactionByBlockHashAndIndex(c context.Context, b
 // no transaction is found, null is returned.
 func (HandlerRPC) StarknetGetTransactionByBlockNumberAndIndex(ctx context.Context, blockNumberOrTag BlockNumberOrTag, index int) (*Txn, error) {
 	if blockNumber := blockNumberOrTag.Number; blockNumber != nil {
-		block := services.BlockService.GetBlockByNumber(*blockNumber)
+		block, err := services.BlockService.GetBlockByNumber(*blockNumber)
+		if err != nil {
+			// notest
+			if errors.Is(err, db.ErrNotFound) {
+				return nil, fmt.Errorf("block not found")
+			}
+			return nil, fmt.Errorf("database error: %v", err)
+		}
 		if index < 0 || len(block.TxHashes) <= index {
 			// notest
 			return nil, fmt.Errorf("invalid index %d", index)
 		}
 		txHash := block.TxHashes[index]
-		txn := services.TransactionService.GetTransaction(txHash)
+		txn, err := services.TransactionService.GetTransaction(txHash)
+		if err != nil {
+			// notest
+			if errors.Is(err, db.ErrNotFound) {
+				return nil, fmt.Errorf("transaction not found")
+			}
+			return nil, fmt.Errorf("database error: %v", err)
+		}
 		return NewTxn(txn), nil
 	}
 	if tag := blockNumberOrTag.Tag; tag != nil {
@@ -374,51 +423,57 @@ func (HandlerRPC) StarknetGetTransactionReceipt(
 func (HandlerRPC) StarknetGetCode(
 	c context.Context, contractAddress types.Address,
 ) (*CodeResult, error) {
-	abi := services.AbiService.GetAbi(contractAddress.Hex())
-	if abi == nil {
-		// Try the feeder gateway for pending block
-		code, err := feederClient.GetCode(contractAddress.Felt().String(), "", string(BlocktagPending))
-		if err != nil {
-			// notest
-			return nil, fmt.Errorf("abi not found %v", err)
-		}
-		// Convert feeder type to RPC CodeResult type
-		bytecode := make([]types.Felt, len(code.Bytecode))
-		for i, code := range code.Bytecode {
-			bytecode[i] = types.HexToFelt(code)
-		}
-		marshal, err := json.Marshal(code.Abi)
-		if err != nil {
-			// notest
-			return nil, fmt.Errorf("abi not found %v", err)
-		}
-		var abiResponse dbAbi.Abi
-		err = json.Unmarshal(marshal, &abiResponse)
-		if err != nil {
-			// notest
-			return nil, fmt.Errorf("abi not found %v", err)
-		}
-		for i, str := range code.Abi.Structs {
-			abiResponse.Structs[i].Fields = make([]*dbAbi.Struct_Field, len(str.Members))
-			for j, field := range str.Members {
-				abiResponse.Structs[i].Fields[j] = &dbAbi.Struct_Field{
-					Name:   field.Name,
-					Type:   field.Type,
-					Offset: uint32(field.Offset),
+	abi, err := services.AbiService.GetAbi(contractAddress.Hex())
+	if err != nil {
+		if errors.Is(err, db.ErrNotFound) {
+			// Try the feeder gateway for pending block
+			code, err := feederClient.GetCode(contractAddress.Felt().String(), "", string(BlocktagPending))
+			if err != nil {
+				// notest
+				return nil, fmt.Errorf("abi not found %v", err)
+			}
+			// Convert feeder type to RPC CodeResult type
+			bytecode := make([]types.Felt, len(code.Bytecode))
+			for i, code := range code.Bytecode {
+				bytecode[i] = types.HexToFelt(code)
+			}
+			marshal, err := json.Marshal(code.Abi)
+			if err != nil {
+				// notest
+				return nil, fmt.Errorf("abi not found %v", err)
+			}
+			var abiResponse dbAbi.Abi
+			err = json.Unmarshal(marshal, &abiResponse)
+			if err != nil {
+				// notest
+				return nil, fmt.Errorf("abi not found %v", err)
+			}
+			for i, str := range code.Abi.Structs {
+				abiResponse.Structs[i].Fields = make([]*dbAbi.Struct_Field, len(str.Members))
+				for j, field := range str.Members {
+					abiResponse.Structs[i].Fields[j] = &dbAbi.Struct_Field{
+						Name:   field.Name,
+						Type:   field.Type,
+						Offset: uint32(field.Offset),
+					}
 				}
 			}
+			marshal, err = json.Marshal(abiResponse)
+			if err != nil {
+				// notest
+				return nil, fmt.Errorf("unexpected marshal error %v", err)
+			}
+			return &CodeResult{Abi: string(marshal), Bytecode: bytecode}, nil
 		}
-		marshal, err = json.Marshal(abiResponse)
-		if err != nil {
-			// notest
-			return nil, fmt.Errorf("unexpected marshal error %v", err)
-		}
-		return &CodeResult{Abi: string(marshal), Bytecode: bytecode}, nil
+		return nil, fmt.Errorf("database error: %v", err)
 	}
-	code := services.StateService.GetCode(contractAddress.Bytes())
-	if code == nil {
+	code, err := services.StateService.GetCode(contractAddress.Bytes())
+	if err != nil {
 		// notest
-		return nil, fmt.Errorf("code not found")
+		if errors.Is(err, db.ErrNotFound) {
+			return nil, fmt.Errorf("code not found")
+		}
+		return nil, fmt.Errorf("database error: %v", err)
 	}
 	marshalledAbi, err := json.Marshal(abi)
 	if err != nil {
diff --git a/pkg/rpc/types.go b/pkg/rpc/types.go
index 7494830a..231292db 100644
--- a/pkg/rpc/types.go
+++ b/pkg/rpc/types.go
@@ -415,7 +415,7 @@ type BlockResponse struct {
 	Transactions interface{} `json:"transactions"`
 }
 
-func NewBlockResponse(block *types.Block, scope RequestedScope) *BlockResponse {
+func NewBlockResponse(block *types.Block, scope RequestedScope) (*BlockResponse, error) {
 	response := &BlockResponse{
 		BlockHash:    block.BlockHash,
 		ParentHash:   block.ParentHash,
@@ -432,7 +432,10 @@ func NewBlockResponse(block *types.Block, scope RequestedScope) *BlockResponse {
 	case ScopeFullTxns:
 		txns := make([]*Txn, len(block.TxHashes))
 		for i, txHash := range block.TxHashes {
-			transaction := services.TransactionService.GetTransaction(txHash)
+			transaction, err := services.TransactionService.GetTransaction(txHash)
+			if err != nil {
+				return nil, err
+			}
 			txn := NewTxn(transaction)
 			txns[i] = txn
 		}
@@ -441,12 +444,18 @@ func NewBlockResponse(block *types.Block, scope RequestedScope) *BlockResponse {
 		txns := make([]*TxnAndReceipt, len(block.TxHashes))
 		for i, txHash := range block.TxHashes {
 			txnAndReceipt := &TxnAndReceipt{}
-			transaction := services.TransactionService.GetTransaction(txHash)
+			transaction, err := services.TransactionService.GetTransaction(txHash)
+			if err != nil {
+				return nil, err
+			}
 			if transaction != nil {
 				txn := NewTxn(transaction)
 				txnAndReceipt.Txn = *txn
 			}
-			receipt := services.TransactionService.GetReceipt(txHash)
+			receipt, err := services.TransactionService.GetReceipt(txHash)
+			if err != nil {
+				return nil, err
+			}
 			if receipt != nil {
 				r := NewTxnReceipt(receipt)
 				txnAndReceipt.TxnReceipt = *r
@@ -455,7 +464,7 @@ func NewBlockResponse(block *types.Block, scope RequestedScope) *BlockResponse {
 		}
 		response.Transactions = txns
 	}
-	return response
+	return response, nil
 }
 
 type StarkNetHash struct{}
diff --git a/pkg/starknet/state.go b/pkg/starknet/state.go
index 8333279b..17de5d95 100644
--- a/pkg/starknet/state.go
+++ b/pkg/starknet/state.go
@@ -377,7 +377,13 @@ func (s *Synchronizer) updateAndCommitState(
 	contractHashMap := make(map[string]*big.Int)
 	for contractAddress := range stateDiff.StorageDiffs {
 		formattedAddress := remove0x(contractAddress)
-		contractHashMap[formattedAddress] = services.ContractHashService.GetContractHash(formattedAddress)
+		contractHash, err := services.ContractHashService.GetContractHash(formattedAddress)
+		if err != nil {
+			// notest
+			metr.IncreaseCountStarknetStateFailed()
+			Logger.Fatal("Couldn't get contract hash")
+		}
+		contractHashMap[formattedAddress] = contractHash
 	}
 
 	err := s.database.RunTxn(func(txn db.DatabaseOperations) error {
diff --git a/pkg/starknet/utils.go b/pkg/starknet/utils.go
index 96d0256b..fc1de388 100644
--- a/pkg/starknet/utils.go
+++ b/pkg/starknet/utils.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"encoding/binary"
 	"encoding/json"
+	"errors"
 	"math/big"
 	"strings"
 
@@ -132,7 +133,7 @@ func getNumericValueFromDB(database db.Database, key string) (uint64, error) {
 	value, err := database.Get([]byte(key))
 	if err != nil {
 		// notest
-		if db.IsNotFound(err) {
+		if errors.Is(err, db.ErrNotFound) {
 			return 0, nil
 		}
 		return 0, err


Test patch:
diff --git a/internal/db/abi/manager_test.go b/internal/db/abi/manager_test.go
index 201e4c26..5db74ddc 100644
--- a/internal/db/abi/manager_test.go
+++ b/internal/db/abi/manager_test.go
@@ -1,6 +1,7 @@
 package abi
 
 import (
+	"errors"
 	"testing"
 
 	"github.com/NethermindEth/juno/internal/db"
@@ -18,10 +19,15 @@ func TestManager(t *testing.T) {
 	manager := NewABIManager(database)
 
 	for address, abi := range abis {
-		manager.PutABI(address, abi)
-		abi2 := manager.GetABI(address)
-		if abi2 == nil {
-			t.Errorf("ABI with key %s not found after insertion with the same key", address)
+		if err := manager.PutABI(address, abi); err != nil {
+			t.Error(err)
+		}
+		abi2, err := manager.GetABI(address)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("ABI not found for address %s", address)
+			}
+			t.Error(err)
 		}
 		if !abi.Equal(abi2) {
 			t.Errorf("ABI are not equal after Put-Get operations, address: %s", address)
diff --git a/internal/db/block/block_test.go b/internal/db/block/block_test.go
index 794a2a05..62a88a81 100644
--- a/internal/db/block/block_test.go
+++ b/internal/db/block/block_test.go
@@ -1,6 +1,7 @@
 package block
 
 import (
+	"errors"
 	"reflect"
 	"testing"
 
@@ -41,19 +42,27 @@ func TestManager(t *testing.T) {
 	manager := NewManager(database)
 	for _, block := range blocks {
 		key := block.BlockHash
-		manager.PutBlock(key, block)
+		if err := manager.PutBlock(key, block); err != nil {
+			t.Error(err)
+		}
 		// Get block by hash
-		returnedBlock := manager.GetBlockByHash(key)
-		if returnedBlock == nil {
-			t.Errorf("unexpected nil after search for block with hash %s", block.BlockHash)
+		returnedBlock, err := manager.GetBlockByHash(key)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("unexpected nil after search for block with hash %s", block.BlockHash)
+			}
+			t.Error(err)
 		}
 		if !reflect.DeepEqual(block, returnedBlock) {
 			t.Errorf("block")
 		}
 		// Get block by number
-		returnedBlock = manager.GetBlockByNumber(block.BlockNumber)
-		if returnedBlock == nil {
-			t.Errorf("unexpected nil after search for block with number %d", block.BlockNumber)
+		returnedBlock, err = manager.GetBlockByNumber(block.BlockNumber)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("unexpected nil after search for block with number %d", block.BlockNumber)
+			}
+			t.Error(err)
 		}
 		if !reflect.DeepEqual(block, returnedBlock) {
 			t.Errorf("block")
diff --git a/internal/db/mdbx_test.go b/internal/db/mdbx_test.go
index 7b2e5653..a5af0210 100644
--- a/internal/db/mdbx_test.go
+++ b/internal/db/mdbx_test.go
@@ -75,7 +75,7 @@ func TestMDBXDatabase_Get_NotFound(t *testing.T) {
 
 	db := dbs[0]
 	value, err := db.Get([]byte("key"))
-	if err == nil || !IsNotFound(err) {
+	if err == nil || !errors.Is(err, ErrNotFound) {
 		t.Errorf("error must be an ErrNotFound")
 	}
 	if value != nil {
diff --git a/internal/db/state/code_test.go b/internal/db/state/code_test.go
index 23e071fb..dc17005e 100644
--- a/internal/db/state/code_test.go
+++ b/internal/db/state/code_test.go
@@ -3,6 +3,7 @@ package state
 import (
 	"bytes"
 	"encoding/hex"
+	"errors"
 	"testing"
 
 	"github.com/NethermindEth/juno/internal/db"
@@ -43,8 +44,16 @@ func TestManager_Code(t *testing.T) {
 	storageDatabase := db.NewBlockSpecificDatabase(storageDb)
 	manager := NewStateManager(codeDatabase, storageDatabase)
 	for _, code := range codes {
-		manager.PutCode(code.Address, code.Code)
-		obtainedCode := manager.GetCode(code.Address)
+		if err := manager.PutCode(code.Address, code.Code); err != nil {
+			t.Error(err)
+		}
+		obtainedCode, err := manager.GetCode(code.Address)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("Code not found for address %s", code.Address)
+			}
+			t.Error(err)
+		}
 		if !equalCodes(t, code.Code, obtainedCode) {
 			t.Errorf("Code are different afte Put-Get operation")
 		}
diff --git a/internal/db/state/storage_test.go b/internal/db/state/storage_test.go
index 7c6b3e5e..a55e7743 100644
--- a/internal/db/state/storage_test.go
+++ b/internal/db/state/storage_test.go
@@ -1,6 +1,7 @@
 package state
 
 import (
+	"errors"
 	"testing"
 
 	"github.com/NethermindEth/juno/internal/db"
@@ -46,7 +47,9 @@ func TestManager_Storage(t *testing.T) {
 	storageDatabase := db.NewBlockSpecificDatabase(storageDb)
 	manager := NewStateManager(codeDb, storageDatabase)
 	for _, data := range initialData {
-		manager.PutStorage(data.Contract, data.BlockNumber, data.Storage)
+		if err := manager.PutStorage(data.Contract, data.BlockNumber, data.Storage); err != nil {
+			t.Error(err)
+		}
 	}
 	tests := [...]struct {
 		Contract    string
@@ -78,8 +81,11 @@ func TestManager_Storage(t *testing.T) {
 		},
 	}
 	for _, test := range tests {
-		obtainedStorage := manager.GetStorage(test.Contract, test.BlockNumber)
-		if test.Ok && obtainedStorage == nil {
+		obtainedStorage, err := manager.GetStorage(test.Contract, test.BlockNumber)
+		if err != nil && !errors.Is(err, db.ErrNotFound) {
+			t.Error(err)
+		}
+		if test.Ok && errors.Is(err, db.ErrNotFound) {
 			t.Errorf("storage of contract %s must not found for bloc %d", test.Contract, test.BlockNumber)
 		}
 		if obtainedStorage != nil && test.Checks != nil {
diff --git a/internal/db/transaction/manager_test.go b/internal/db/transaction/manager_test.go
index 535ef4db..7a3c6cc1 100644
--- a/internal/db/transaction/manager_test.go
+++ b/internal/db/transaction/manager_test.go
@@ -1,6 +1,7 @@
 package transaction
 
 import (
+	"errors"
 	"reflect"
 	"testing"
 
@@ -87,7 +88,9 @@ func initManager(t *testing.T) *Manager {
 func TestManager_PutTransaction(t *testing.T) {
 	manager := initManager(t)
 	for _, tx := range txs {
-		manager.PutTransaction(tx.GetHash(), tx)
+		if err := manager.PutTransaction(tx.GetHash(), tx); err != nil {
+			t.Error(err)
+		}
 	}
 	manager.Close()
 }
@@ -96,11 +99,19 @@ func TestManager_GetTransaction(t *testing.T) {
 	manager := initManager(t)
 	// Insert all the transactions
 	for _, tx := range txs {
-		manager.PutTransaction(tx.GetHash(), tx)
+		if err := manager.PutTransaction(tx.GetHash(), tx); err != nil {
+			t.Error(err)
+		}
 	}
 	// Get all the transactions and compare
 	for _, tx := range txs {
-		outTx := manager.GetTransaction(tx.GetHash())
+		outTx, err := manager.GetTransaction(tx.GetHash())
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("Transaction %s not found", tx.GetHash())
+			}
+			t.Error(err)
+		}
 		if !reflect.DeepEqual(tx, outTx) {
 			t.Errorf("transaction not equal after Put/Get operations")
 		}
@@ -163,7 +174,9 @@ var receipts = []*types.TransactionReceipt{
 func TestManager_PutReceipt(t *testing.T) {
 	manager := initManager(t)
 	for _, receipt := range receipts {
-		manager.PutReceipt(receipt.TxHash, receipt)
+		if err := manager.PutReceipt(receipt.TxHash, receipt); err != nil {
+			t.Error(err)
+		}
 	}
 	manager.Close()
 }
@@ -171,10 +184,18 @@ func TestManager_PutReceipt(t *testing.T) {
 func TestManager_GetReceipt(t *testing.T) {
 	manager := initManager(t)
 	for _, receipt := range receipts {
-		manager.PutReceipt(receipt.TxHash, receipt)
+		if err := manager.PutReceipt(receipt.TxHash, receipt); err != nil {
+			t.Error(err)
+		}
 	}
 	for _, receipt := range receipts {
-		outReceipt := manager.GetReceipt(receipt.TxHash)
+		outReceipt, err := manager.GetReceipt(receipt.TxHash)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("Receipt %s not found", receipt.TxHash)
+			}
+			t.Error(err)
+		}
 
 		if !reflect.DeepEqual(receipt, outReceipt) {
 			t.Errorf("receipt not equal after Put/Get operations")
diff --git a/internal/services/abi_test.go b/internal/services/abi_test.go
index 32445c54..86565687 100644
--- a/internal/services/abi_test.go
+++ b/internal/services/abi_test.go
@@ -2,6 +2,7 @@ package services
 
 import (
 	"context"
+	"errors"
 	"testing"
 
 	"github.com/NethermindEth/juno/internal/db"
@@ -24,12 +25,17 @@ func TestAbiService_StoreGet(t *testing.T) {
 	defer AbiService.Close(context.Background())
 
 	for address, a := range abis {
-		AbiService.StoreAbi(address, a)
+		if err := AbiService.StoreAbi(address, a); err != nil {
+			t.Error(err)
+		}
 	}
 	for address, a := range abis {
-		result := AbiService.GetAbi(address)
-		if result == nil {
-			t.Errorf("abi not foud for key: %s", address)
+		result, err := AbiService.GetAbi(address)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("ABI not found for address %s", address)
+			}
+			t.Error(err)
 		}
 		if !a.Equal(result) {
 			t.Errorf("ABI are not equal after Store-Get operations, address: %s", address)
diff --git a/internal/services/block_test.go b/internal/services/block_test.go
index f95baeba..5d644343 100644
--- a/internal/services/block_test.go
+++ b/internal/services/block_test.go
@@ -2,6 +2,7 @@ package services
 
 import (
 	"context"
+	"errors"
 	"reflect"
 	"testing"
 
@@ -46,19 +47,27 @@ func TestService(t *testing.T) {
 	}
 	for _, b := range blocks {
 		key := b.BlockHash
-		BlockService.StoreBlock(key, b)
+		if err := BlockService.StoreBlock(key, b); err != nil {
+			t.Error(err)
+		}
 		// Get block by hash
-		returnedBlock := BlockService.GetBlockByHash(key)
-		if returnedBlock == nil {
-			t.Errorf("unexpected nil after search for block with hash %s", key)
+		returnedBlock, err := BlockService.GetBlockByHash(key)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("Block not found for hash %s", key.Hex())
+			}
+			t.Errorf("Unexpected error: %s", err)
 		}
 		if !reflect.DeepEqual(b, returnedBlock) {
 			t.Errorf("b")
 		}
 		// Get block by number
-		returnedBlock = BlockService.GetBlockByNumber(b.BlockNumber)
-		if returnedBlock == nil {
-			t.Errorf("unexpected nil after search for block with number %d", b.BlockNumber)
+		returnedBlock, err = BlockService.GetBlockByNumber(b.BlockNumber)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("Block not found for number %d", b.BlockNumber)
+			}
+			t.Errorf("Unexpected error: %s", err)
 		}
 		if !reflect.DeepEqual(b, returnedBlock) {
 			t.Errorf("b")
diff --git a/internal/services/state_test.go b/internal/services/state_test.go
index feec34e9..a534af22 100644
--- a/internal/services/state_test.go
+++ b/internal/services/state_test.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"context"
 	"encoding/hex"
+	"errors"
 	"testing"
 
 	"github.com/NethermindEth/juno/internal/db"
@@ -55,8 +56,16 @@ func TestStateService_Code(t *testing.T) {
 	defer StateService.Close(context.Background())
 
 	for _, code := range codes {
-		StateService.StoreCode(code.Address, code.Code)
-		obtainedCode := StateService.GetCode(code.Address)
+		if err := StateService.StoreCode(code.Address, code.Code); err != nil {
+			t.Error(err)
+		}
+		obtainedCode, err := StateService.GetCode(code.Address)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("code not found: %s", err)
+			}
+			t.Errorf("error: %s", err)
+		}
 		if !equalCodes(t, code.Code, obtainedCode) {
 			t.Errorf("Code are different afte Put-Get operation")
 		}
@@ -91,7 +100,9 @@ func TestService_Storage(t *testing.T) {
 	defer StateService.Close(context.Background())
 
 	for _, data := range initialData {
-		StateService.UpdateStorage(data.Contract, data.BlockNumber, data.Storage)
+		if err := StateService.UpdateStorage(data.Contract, data.BlockNumber, data.Storage); err != nil {
+			t.Error(err)
+		}
 	}
 	tests := [...]struct {
 		Contract    string
@@ -123,8 +134,11 @@ func TestService_Storage(t *testing.T) {
 		},
 	}
 	for _, test := range tests {
-		obtainedStorage := StateService.GetStorage(test.Contract, test.BlockNumber)
-		if test.Ok && obtainedStorage == nil {
+		obtainedStorage, err := StateService.GetStorage(test.Contract, test.BlockNumber)
+		if err != nil && !errors.Is(err, db.ErrNotFound) {
+			t.Error(err)
+		}
+		if test.Ok && errors.Is(err, db.ErrNotFound) {
 			t.Errorf("storage of contract %s must not found for bloc %d", test.Contract, test.BlockNumber)
 		}
 		if obtainedStorage != nil && test.Checks != nil {
diff --git a/internal/services/transactions_test.go b/internal/services/transactions_test.go
index 535f05df..6aaaeb08 100644
--- a/internal/services/transactions_test.go
+++ b/internal/services/transactions_test.go
@@ -2,6 +2,7 @@ package services
 
 import (
 	"context"
+	"errors"
 	"reflect"
 	"testing"
 
@@ -94,7 +95,9 @@ func TestTransactionService_StoreTransaction(t *testing.T) {
 	}
 
 	for _, tx := range txs {
-		TransactionService.StoreTransaction(tx.GetHash(), tx)
+		if err := TransactionService.StoreTransaction(tx.GetHash(), tx); err != nil {
+			t.Error(err)
+		}
 	}
 	TransactionService.Close(context.Background())
 }
@@ -108,11 +111,19 @@ func TestManager_GetTransaction(t *testing.T) {
 	}
 	// Insert all the transactions
 	for _, tx := range txs {
-		TransactionService.StoreTransaction(tx.GetHash(), tx)
+		if err := TransactionService.StoreTransaction(tx.GetHash(), tx); err != nil {
+			t.Error(err)
+		}
 	}
 	// Get all the transactions and compare
 	for _, tx := range txs {
-		outTx := TransactionService.GetTransaction(tx.GetHash())
+		outTx, err := TransactionService.GetTransaction(tx.GetHash())
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("transaction not found: %s", tx.GetHash().String())
+			}
+			t.Error(err)
+		}
 
 		if !reflect.DeepEqual(tx, outTx) {
 			t.Errorf("transaction not equal after Put/Get operations")
@@ -177,7 +188,9 @@ func TestManager_PutReceipt(t *testing.T) {
 		t.Errorf("error running the service: %s", err)
 	}
 	for _, receipt := range receipts {
-		TransactionService.StoreReceipt(receipt.TxHash, receipt)
+		if err := TransactionService.StoreReceipt(receipt.TxHash, receipt); err != nil {
+			t.Error(err)
+		}
 	}
 	TransactionService.Close(context.Background())
 }
@@ -190,10 +203,18 @@ func TestManager_GetReceipt(t *testing.T) {
 		t.Errorf("error running the service: %s", err)
 	}
 	for _, receipt := range receipts {
-		TransactionService.StoreReceipt(receipt.TxHash, receipt)
+		if err := TransactionService.StoreReceipt(receipt.TxHash, receipt); err != nil {
+			t.Error(err)
+		}
 	}
 	for _, receipt := range receipts {
-		outReceipt := TransactionService.GetReceipt(receipt.TxHash)
+		outReceipt, err := TransactionService.GetReceipt(receipt.TxHash)
+		if err != nil {
+			if errors.Is(err, db.ErrNotFound) {
+				t.Errorf("receipt not found: %s", receipt.TxHash.String())
+			}
+			t.Error(err)
+		}
 
 		if !reflect.DeepEqual(receipt, outReceipt) {
 			t.Errorf("receipt not equal after Put/Get operations")