Packages
Error Handling

Error Handling

This section documents the custom error classes defined for handling specific error scenarios while interacting with POAP tokens.

These custom error classes allow for more precise error handling and better debugging, by providing specific error messages based on the type of error encountered. They extend the native JavaScript Error class and can be used in a similar manner, with the added benefit of POAP-specific error messages.

Example Usage

Below example demonstrates how one might use the PoapsClient class along with the custom error handling.

import {
  POAP,
  FinishedWithError,
  CodeAlreadyMintedError,
  CodeExpiredError,
} from '@poap-xyz/poaps';
 
try {
  const poap: POAP = await client.mintSync({
    mintCode: 'some-mint-code',
    address: '0x1234567890abcdef1234567890abcdef12345678',
  });
  console.info('POAP minted successfully', poap);
} catch (error: unknown) {
  if (error instanceof FinishedWithError) {
    console.error('Error concluding the mint process', error.message);
    // Action: Notify the user about the error and suggest retrying later.
  } else if (error instanceof CodeAlreadyMintedError) {
    console.error('This mint code has already been used', error.message);
    // Action: Notify the user that the mint code has already been used.
  } else if (error instanceof CodeExpiredError) {
    console.error('This mint code has expired', error.message);
    // Action: Notify the user that the mint code has expired.
  } else {
    console.error('An unknown error occurred', error.message);
    // Action: Log the error and notify the user of a general error.
  }
}