Skip to content

Commit

Permalink
add AdapterErrorWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityblast committed May 9, 2024
1 parent cd61e22 commit 3a63bdb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
44 changes: 21 additions & 23 deletions packages/common/src/allo-strategy-adapters/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import { Address, PublicClient } from "viem";

export class AdapterError extends Error {
public readonly reason: string;
public readonly innerError?: Error;

constructor(reason: string, innerError?: Error) {
super(reason);
this.reason = reason;
this.innerError = innerError;
}
}

export class AdapterResponseError extends AdapterError {
constructor(reason: string, error: AdapterError) {
super(reason, error);
}
}

export class UnknownAdapterError extends AdapterError {
constructor(strategyName: string) {
super(`Adapter not found for strategy: ${strategyName}`);
}
}
export type AdapterNotFoundError = {
type: "ADAPTER_NOT_FOUND";
strategyName: string;
error: Error;
};

export type AdapterAllocationUnauthorizedError = {
type: "ADAPTER_ALLOCATION_UNAUTHORIZED";
error: Error;
};

export type AdapterUnknownError = {
type: "ADAPTER_UNKNOWN_ERROR";
error: Error;
};

export type AdapterErrorWrapper =
| AdapterNotFoundError
| AdapterAllocationUnauthorizedError
| AdapterUnknownError;

export type AdapterResponse<T> =
| { type: "success"; value: T }
| { type: "error"; error: AdapterError };
| { type: "error"; error: AdapterErrorWrapper };

export interface AllocationAdapter {
// Returns true if the allocator can allocate to the pool.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, PublicClient } from "viem";
import { AdapterResponse, AdapterError } from "../../adapter";
import { AdapterResponse } from "../../adapter";

export function canAllocate(
_client: PublicClient,
Expand All @@ -25,7 +25,10 @@ export function canAllocate(
return resolve({
type: "error",

error: new AdapterError("you don't have a badge to vote."),
error: {
type: "ADAPTER_ALLOCATION_UNAUTHORIZED",
error: new Error("you don't have a badge to vote."),
},
});
}, 2000);
});
Expand Down
14 changes: 9 additions & 5 deletions packages/common/src/allo-strategy-adapters/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Address } from "wagmi";
import { PublicClient } from "viem";
import { useState, useEffect } from "react";
import { getAllocationAdapter } from "./index";
import { AdapterError, UnknownAdapterError } from "./adapter";
import { AdapterErrorWrapper } from "./adapter";

type AdapterResponseWrapper<T> = {
loading: boolean;
error: Error | undefined;
error: AdapterErrorWrapper | undefined;
value: T | undefined;
};

Expand All @@ -17,7 +17,7 @@ export function useAdapterCanAllocate(
address: Address | undefined
): AdapterResponseWrapper<boolean> {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<AdapterError>();
const [error, setError] = useState<AdapterErrorWrapper>();
const [value, setValue] = useState<boolean>();

useEffect(() => {
Expand All @@ -33,7 +33,11 @@ export function useAdapterCanAllocate(

const adapter = getAllocationAdapter(strategyName);
if (adapter === undefined) {
setError(new UnknownAdapterError(strategyName));
setError({
type: "ADAPTER_NOT_FOUND",
strategyName,
error: new Error(`Adapter not found for strategy: ${strategyName}`),
});
setLoading(false);
return;
}
Expand All @@ -47,7 +51,7 @@ export function useAdapterCanAllocate(
setError(resp.error);
}
})
.catch((e: AdapterError) => {
.catch((e: AdapterErrorWrapper) => {
setError(e);
})
.finally(() => {
Expand Down
5 changes: 2 additions & 3 deletions packages/common/src/allo-strategy-adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { AllocationAdapter, Adapter } from "./adapter";
import allov2DonationVotingMerkleDistributionDirectTransferStrategy from "./adapters/allov2.DonationVotingMerkleDistributionDirectTransferStrategy";
import alloV2QF from "./adapters/allov2.QF";

export const adapters: { [key: string]: Adapter } = {
"allov2.DonationVotingMerkleDistributionDirectTransferStrategy":
allov2DonationVotingMerkleDistributionDirectTransferStrategy,
"allov2.DonationVotingMerkleDistributionDirectTransferStrategy": alloV2QF,
};

export function getAllocationAdapter(
Expand Down
16 changes: 13 additions & 3 deletions packages/grant-explorer/src/features/round/ViewRoundPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -865,12 +865,22 @@ export function CartButtonToggle(props: {
return <div>Loading...</div>;
}

if (error) {
return <div>Error checking if you can allocate: {error.message}</div>;
if (error !== undefined) {
switch (error.type) {
case "ADAPTER_ALLOCATION_UNAUTHORIZED":
return (
<div>
You are not authorized to allocate:
{error.error.message}
</div>
);
default:
return <div>Something went wrong {error.error.message}</div>;
}
}

if (!canAllocate) {
return <div>You can't allocate</div>;
return <div>Allocation not allowed</div>;
}

return (
Expand Down

0 comments on commit 3a63bdb

Please sign in to comment.