Skip to content

Commit

Permalink
feat: add foundation of error handling logic (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
jahabeebs authored Oct 8, 2024
1 parent 86b48ec commit b67902e
Show file tree
Hide file tree
Showing 31 changed files with 1,173 additions and 545 deletions.
3 changes: 1 addition & 2 deletions apps/agent/src/config/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Caip2Utils } from "@ebo-agent/blocknumber";
import { Caip2ChainId } from "@ebo-agent/blocknumber/dist/types.js";
import { Caip2ChainId, Caip2Utils } from "@ebo-agent/blocknumber/src/index.js";
import { isAddress, isHex } from "viem";
import { z } from "zod";

Expand Down

This file was deleted.

67 changes: 67 additions & 0 deletions packages/automated-dispute/src/exceptions/customContractError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
EboEvent,
EboEventName,
ErrorContext,
ErrorHandlingStrategy,
ErrorName,
} from "../types/index.js";

export class CustomContractError extends Error {
public override name: ErrorName;
public strategy: ErrorHandlingStrategy;
public context!: ErrorContext;
private customActions: Map<ErrorName, (context: ErrorContext) => Promise<void> | void> =
new Map();

constructor(name: ErrorName, strategy: ErrorHandlingStrategy) {
super(`Contract reverted: ${name}`);
this.name = name;
this.strategy = strategy;
}

public setContext(context: ErrorContext): this {
this.context = context;
return this;
}

public getContext(): ErrorContext {
return this.context;
}

/**
* Sets the context specific to processEvents.
* This method replaces addContext for processEvents-related context updates.
*
* @param event The event being processed.
* @param reenqueueEvent Callback to reenqueue the event.
* @param terminateActor Callback to terminate the actor.
* @returns The error instance.
*/
public setProcessEventsContext(
event: EboEvent<EboEventName>,
reenqueueEvent: () => void,
terminateActor: () => void,
): this {
this.context = {
...this.context,
event,
reenqueueEvent,
terminateActor,
};
return this;
}

public on(errorName: string, action: (context: ErrorContext) => Promise<void> | void): this {
if (this.name === errorName) {
this.customActions.set(errorName, action);
}
return this;
}

public async executeCustomAction(): Promise<void> {
const action = this.customActions.get(this.name) || this.strategy.customAction;
if (action) {
await action(this.context);
}
}
}
Loading

0 comments on commit b67902e

Please sign in to comment.