Skip to content

Commit

Permalink
Util, Block: added generic error types like ValidationError, UsageError
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 authored and jochem-brouwer committed Sep 30, 2024
1 parent 95b6b8b commit 88e6a31
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
23 changes: 20 additions & 3 deletions packages/block/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import { EthereumJSError } from '@ethereumjs/util'
import { UsageError, ValidationError } from '@ethereumjs/util'

export enum HeaderValidationErrorCode {
/**
* Always define error codes on the generic Util
* error class level (e.g. associated to `ValidationError`)
*/
export enum ValidationErrorCode {
WRONG_TX_TRIE_LENGTH = 'WRONG_TX_TRIE_LENGTH',
}

/**
* Additional types extending the generic Util
* error types (e.g. `ValidationErrorType`)
*/
export type HeaderValidationErrorType = {
block: string
received: string
}
export type HeaderUsageErrorType = {
block: string
}

export class HeaderValidationError extends EthereumJSError<HeaderValidationErrorType> {}
/**
* Dedicated error classes for the specific package,
* always to be subclassed from the generic Util error type
* classes (e.g. `ValidationError`, not: `EthereumJSError`)
*/
export class HeaderValidationError extends ValidationError<HeaderValidationErrorType> {}
export class HeaderUsageError extends UsageError<HeaderUsageErrorType> {}
4 changes: 2 additions & 2 deletions packages/block/src/header/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
CLIQUE_EXTRA_VANITY,
cliqueIsEpochTransition,
} from '../consensus/clique.js'
import { HeaderValidationError, HeaderValidationErrorCode } from '../errors.js'
import { HeaderValidationError, ValidationErrorCode } from '../errors.js'
import { fakeExponential } from '../helpers.js'
import { paramsBlock } from '../params.js'

Expand Down Expand Up @@ -270,7 +270,7 @@ export class BlockHeader {
if (transactionsTrie.length !== 32) {
const e = new HeaderValidationError(
'transactionsTrie must be 32 bytes',
HeaderValidationErrorCode.WRONG_TX_TRIE_LENGTH,
ValidationErrorCode.WRONG_TX_TRIE_LENGTH,
{
block: this.errorStr(),
received: `${bytesToHex(transactionsTrie)} (${transactionsTrie.length} bytes)`,
Expand Down
20 changes: 19 additions & 1 deletion packages/util/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Generic EthereumJS error with metadata attached
* Generic EthereumJS error class with metadata attached
*
* Kudos to https://github.com/ChainSafe/lodestar monorepo
* for the inspiration :-)
Expand Down Expand Up @@ -28,3 +28,21 @@ export class EthereumJSError<T extends {}> extends Error {
}
}
}

export type ValidationErrorType = {
received: string
}

/**
* Error along Object Validation
*
* Use directly or in a subclassed context for error comparison (`e instanceof ValidationError`)
*/
export class ValidationError<T extends ValidationErrorType> extends EthereumJSError<T> {}

/**
* Error along API Usage
*
* Use directly or in a subclassed context for error comparison (`e instanceof UsageError`)
*/
export class UsageError<T extends {}> extends EthereumJSError<T> {}

0 comments on commit 88e6a31

Please sign in to comment.