Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'cause' does not exist on type 'Json & ExactOptionalGuard' #129

Open
Taewa opened this issue Jan 4, 2024 · 3 comments
Open

Property 'cause' does not exist on type 'Json & ExactOptionalGuard' #129

Taewa opened this issue Jan 4, 2024 · 3 comments

Comments

@Taewa
Copy link

Taewa commented Jan 4, 2024

Hi,
My main purpose of using serializeError is to get cause. it works perfectly but I get a following error

Property 'cause' does not exist on type 'Json & ExactOptionalGuard'.

What would be the appropriate type of it? My code is below:

const err = serializeError(e);  // 'e' from smart contract error
const cause = err?.data?.cause?.reason; // Property 'cause' does not exist on type 'Json & ExactOptionalGuard'
@mcmire
Copy link
Contributor

mcmire commented Jan 8, 2024

Hi @Taewa, it's possible for data to not contain a cause property, so you have to check for the existence of this property first before attempting to access it.

@0xelisa
Copy link

0xelisa commented Jan 25, 2024

@Taewa this is what I did and seems to work:

  .catch((e: any) => {
        const serializedError = serializeError(e) as SerializedError;
        const errorData = serializedError.data as MetaMaskErrorData; // Type assertion here
        if (errorData?.cause) {
          console.log("Serialized RPC Error:", errorData.cause.reason);
        }
        console.log(e.code);
      });
  create these interfaces:
   interface MetaMaskErrorData {
  cause?: {
    [key: string]: any; // Using an index signature for flexibility
  };
  [key: string]: any; // Allow for other dynamic properties
}

interface SerializedError {
  code: number;
  message: string;
  data?: MetaMaskErrorData;
}

Hope this helps :)

@rekmarks
Copy link
Member

@legobeat should we consider this closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants