Skip to content

Commit

Permalink
Implement error module (#341)
Browse files Browse the repository at this point in the history
* Implement error module

* add errors classes to distinguish error instances
  • Loading branch information
pablomendezroyo authored Sep 20, 2024
1 parent 29e58ac commit 377f861
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 162 deletions.
9 changes: 9 additions & 0 deletions packages/brain/src/createErrorFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function createErrorFactory(errorName: string) {
return class CustomError extends Error {
constructor(message: string) {
super(message);
this.name = errorName; // Set the error name provided to the factory
Error.captureStackTrace(this, CustomError); // Capture the stack trace and omit the constructor frame
}
};
}
8 changes: 8 additions & 0 deletions packages/brain/src/modules/apiClients/beaconchain/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiError } from "../error.js";

export class BeaconchainApiError extends ApiError {
constructor(message: string) {
super(message);
this.name = "BeaconchainApiError"; // Override the name if needed
}
}
43 changes: 15 additions & 28 deletions packages/brain/src/modules/apiClients/beaconchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { StandardApi } from "../standard.js";
import path from "path";
import { ApiParams } from "../types.js";
import { Network } from "@stakingbrain/common";
import { BeaconchainApiError } from "./error.js";

// TODO: BlockId can also be a simple slot in the form of a string. Is this type still necessary?
type BlockId = "head" | "genesis" | "finalized" | string | `0x${string}`;
Expand Down Expand Up @@ -67,8 +68,7 @@ export class BeaconchainApi extends StandardApi {
body: JSON.stringify(postVoluntaryExitsRequest)
});
} catch (e) {
e.message += `Error posting (POST) voluntary exits to beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error posting (POST) voluntary exits to beaconchain. ${e.message}`);
}
}

Expand All @@ -84,8 +84,7 @@ export class BeaconchainApi extends StandardApi {
endpoint: path.join(this.beaconchainEndpoint, "genesis")
});
} catch (e) {
e.message += `Error getting (GET) genesis from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (GET) genesis from beaconchain. ${e.message}`);
}
}

Expand All @@ -106,8 +105,7 @@ export class BeaconchainApi extends StandardApi {
endpoint: path.join(this.beaconchainEndpoint, "states", stateId, "fork")
});
} catch (e) {
e.message += `Error getting (GET) fork from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (GET) fork from beaconchain. ${e.message}`);
}
}

Expand All @@ -128,8 +126,7 @@ export class BeaconchainApi extends StandardApi {
endpoint: path.join(this.beaconchainEndpoint, "states", stateId, "finality_checkpoints")
});
} catch (e) {
e.message += `Error getting (GET) state finality checkpoints from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (GET) state finality checkpoints from beaconchain. ${e.message}`);
}
}

Expand All @@ -153,8 +150,7 @@ export class BeaconchainApi extends StandardApi {
endpoint: path.join(this.beaconchainEndpoint, "states", state, "validators", pubkey)
});
} catch (e) {
e.message += `Error getting (GET) validator from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (GET) validator from beaconchain. ${e.message}`);
}
}

Expand All @@ -181,8 +177,7 @@ export class BeaconchainApi extends StandardApi {
body: JSON.stringify(body)
});
} catch (e) {
e.message += `Error getting (POST) state validators from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (POST) state validators from beaconchain. ${e.message}`);
}
}

Expand Down Expand Up @@ -213,8 +208,7 @@ export class BeaconchainApi extends StandardApi {
endpoint: path.join(this.beaconchainEndpoint, "blocks", blockId, "attestations")
});
} catch (e) {
e.message += `Error getting (GET) block attestations from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (GET) block attestations from beaconchain. ${e.message}`);
}
}

Expand All @@ -239,8 +233,7 @@ export class BeaconchainApi extends StandardApi {
body: JSON.stringify(pubkeysOrIndexes)
});
} catch (e) {
e.message += `Error getting (POST) attestation rewards from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (POST) attestation rewards from beaconchain. ${e.message}`);
}
}

Expand All @@ -265,8 +258,7 @@ export class BeaconchainApi extends StandardApi {
body: JSON.stringify(validatorIndexesOrPubkeys)
});
} catch (e) {
e.message += `Error getting (POST) sync committee rewards from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (POST) sync committee rewards from beaconchain. ${e.message}`);
}
}

Expand All @@ -284,8 +276,7 @@ export class BeaconchainApi extends StandardApi {
endpoint: path.join(this.validatorEndpoint, "duties", "proposer", epoch)
});
} catch (e) {
e.message += `Error getting (GET) proposer duties from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (GET) proposer duties from beaconchain. ${e.message}`);
}
}

Expand All @@ -302,8 +293,7 @@ export class BeaconchainApi extends StandardApi {
endpoint: path.join(this.beaconchainEndpoint, "rewards", "block", blockId)
});
} catch (e) {
e.message += `Error getting (GET) block rewards from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (GET) block rewards from beaconchain. ${e.message}`);
}
}

Expand Down Expand Up @@ -332,8 +322,7 @@ export class BeaconchainApi extends StandardApi {
body: JSON.stringify(validatorIndexes)
});
} catch (e) {
e.message += `Error getting (POST) liveness from validator. `;
throw e;
throw new BeaconchainApiError(`Error getting (POST) liveness from validator. ${e.message}`);
}
}

Expand All @@ -349,8 +338,7 @@ export class BeaconchainApi extends StandardApi {
endpoint: path.join(this.nodeEndpoint, "syncing")
});
} catch (e) {
e.message += `Error getting (GET) syncing status from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (GET) syncing status from beaconchain. ${e.message}`);
}
}

Expand All @@ -368,8 +356,7 @@ export class BeaconchainApi extends StandardApi {
endpoint: path.join(this.beaconchainEndpoint, "headers", blockId)
});
} catch (e) {
e.message += `Error getting (GET) block header from beaconchain. `;
throw e;
throw new BeaconchainApiError(`Error getting (GET) block header from beaconchain. ${e.message}`);
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/brain/src/modules/apiClients/blockExplorer/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiError } from "../error.js";

export class BlockExplorerApiError extends ApiError {
constructor(message: string) {
super(message);
this.name = "BlockExplorerApiError"; // Override the name if needed
}
}
4 changes: 2 additions & 2 deletions packages/brain/src/modules/apiClients/blockExplorer/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StandardApi } from "../standard.js";
import { BeaconchaGetResponse } from "./types.js";
import { BlockExplorerApiError } from "./error.js";

const maxValidatorsPerRequest = 100; //For beaconcha.in --> TODO: is it the same for Gnosis?

Expand Down Expand Up @@ -43,8 +44,7 @@ export class BlockExplorerApi extends StandardApi {
endpoint
})) as BeaconchaGetResponse;
} catch (e) {
e.message += "Error on getting indexes for validator public keys";
throw e;
throw new BlockExplorerApiError(`Error on getting indexes for validator public keys. ${e.message}`);
}
}
}
23 changes: 2 additions & 21 deletions packages/brain/src/modules/apiClients/error.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
import type { ErrorCode, ErrnoException } from "./types.js";
import { createErrorFactory } from "../../createErrorFactory.js";

export class ApiError extends Error {
public code: ErrorCode;
public errno: number;
public path?: string;
public syscall?: string;
public hostname?: string;
public address?: string;
public port?: number;

constructor(error: ErrnoException) {
super(error.message);
this.code = error.code || "UNKNOWN";
this.errno = error.errno || -1;
this.path = error.path;
this.syscall = error.syscall;
this.hostname = error.hostname;
this.address = error.address;
this.port = error.port;
}
}
export const ApiError = createErrorFactory("ApiError");
8 changes: 8 additions & 0 deletions packages/brain/src/modules/apiClients/postgres/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiError } from "../error.js";

export class PostgresApiError extends ApiError {
constructor(message: string) {
super(message);
this.name = "PostgresApiError"; // Override the name if needed
}
}
4 changes: 2 additions & 2 deletions packages/brain/src/modules/apiClients/postgres/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import postgres from "postgres";
import logger from "../../logger/index.js";
import { BlockProposalStatus, ValidatorPerformance } from "./types.js";
import { PostgresApiError } from "./error.js";

export class PostgresClient {
private readonly tableName = "validators_performance";
Expand Down Expand Up @@ -31,8 +32,7 @@ SELECT pg_total_relation_size('${this.tableName}');
const result = await this.sql.unsafe(query);
return result[0].pg_total_relation_size;
} catch (err) {
err.message = "Error getting table size: " + err.message;
throw err;
throw new PostgresApiError(`Error getting table size: ${err.message}`);
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/brain/src/modules/apiClients/signer/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiError } from "../error.js";

export class SignerApiError extends ApiError {
constructor(message: string) {
super(message);
this.name = "SignerApiError";
}
}
21 changes: 9 additions & 12 deletions packages/brain/src/modules/apiClients/signer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { StandardApi } from "../standard.js";
import path from "node:path";
import { prefix0xPubkey } from "../prefix0xPubkey.js";
import { SignerApiError } from "./error.js";

/**
* Key Manager API standard
Expand Down Expand Up @@ -71,8 +72,9 @@ export class Web3SignerApi extends StandardApi {
headers: this.originHeader
});
} catch (e) {
e.message += `Error signing (POST) voluntary exit for validator index ${signerVoluntaryExitRequest.voluntary_exit.validator_index}. `;
throw e;
throw new SignerApiError(
`Error signing (POST) voluntary exit for validator index ${signerVoluntaryExitRequest.voluntary_exit.validator_index}. ${e.message}`
);
}
}

Expand All @@ -98,8 +100,7 @@ export class Web3SignerApi extends StandardApi {
}
});
} catch (e) {
e.message += `Error signing (POST) proof of validation for validator ${pubkey}. `;
throw e;
throw new SignerApiError(`Error signing (POST) proof of validation for validator ${pubkey}. ${e.message}`);
}
}

Expand All @@ -117,8 +118,7 @@ export class Web3SignerApi extends StandardApi {
headers: this.originHeader
});
} catch (e) {
e.message += `Error importing (POST) keystores to remote signer. `;
throw e;
throw new SignerApiError(`Error importing (POST) keystores to remote signer. ${e.message}`);
}
}

Expand All @@ -140,8 +140,7 @@ export class Web3SignerApi extends StandardApi {
headers: this.originHeader
});
} catch (e) {
e.message += `Error deleting (DELETE) keystores from remote signer. `;
throw e;
throw new SignerApiError(`Error deleting (DELETE) keystores from remote signer. ${e.message}`);
}
}

Expand All @@ -157,8 +156,7 @@ export class Web3SignerApi extends StandardApi {
headers: this.originHeader
});
} catch (e) {
e.message += `Error getting (GET) keystores from remote signer. `;
throw e;
throw new SignerApiError(`Error getting (GET) keystores from remote signer. ${e.message}`);
}
}

Expand All @@ -174,8 +172,7 @@ export class Web3SignerApi extends StandardApi {
headers: this.originHeader
});
} catch (e) {
e.message += `Error getting (GET) server status. Is Web3Signer running? `;
throw e;
throw new SignerApiError(`Error getting (GET) server status. Is Web3Signer running?: ${e.message}`);
}
}
}
Loading

0 comments on commit 377f861

Please sign in to comment.