ebay-api icon indicating copy to clipboard operation
ebay-api copied to clipboard

Update docs: How to handle errors?

Open MickL opened this issue 1 year ago • 3 comments

Thanks for this amazing library! What I dont see from the docs is how to work with errors, for example when I use trading.GetItem but the item is not found ebay throws the error 17 but I dont see any exports from this library:

I see you have an EBayError class and all the error codes are there as well. But I cant import them? I am looking for something like this:

try {
    const result = await useEbay().trading.GetItem({
      ItemID: '123',
    });
    // ...
  } catch (e) {
      if (e.meta.Errors.ErrorCode === EBayErrorCodes.ItemNotFound) {
        throw createError({
          statusCode: 400,
          message: `Item with id ${id} not found`,
        });
      } else {
        throw e;
      }
  }

So in short I dont know how to import EBayError class and I the error code enum.

MickL avatar Apr 18 '24 11:04 MickL

@MickL Not all error codes are mapped to EBayError class only the one that are handled by the lib directly. EBay has hundreds of error codes…

You are doing it correctly:

const EBayErrorCodes = {
  ItemNotFound: 17
};

 if (e.meta.Errors.ErrorCode === EBayErrorCodes.ItemNotFound) {
        throw createError({
          statusCode: 400,
          message: `Item with id ${id} not found`,
        });
      } 

I'll think about to add all Error Codes to this lib.

dantio avatar Apr 19 '24 11:04 dantio

I see :) It would be nice to have an enum with all the error codes and also being able to import EBayError to do:

try {
   // ...
} catch (e) {
   if (e instanceof EBayError) {
        ...
   } else {
       throw e;
   }
}

MickL avatar Apr 19 '24 12:04 MickL

@MickL I think it should be

if (e.meta.ErrorCode === EBayErrorCodes.ItemNotFound) {} 

Without .Errors.

You can import the EBayError:

import { EBayError } from 'ebay-api/lib/errors';

dantio avatar Apr 19 '24 12:04 dantio

Hi,

In your code snippet, is "EBayErrorCodes" coming from your library or the consumer? I don't see it in this repo. Also, "meta" is typed as any. Is it safe to assume it'll always have an "ErrorCode" that's a number?

export declare class EBayError extends Error {
    readonly meta: any;
    readonly description: string;
    constructor(message: string, description?: string, meta?: any);
    toJSON(): {
        message: string;
        description: string;
        stack: string | undefined;
        type: string;
        meta: any;
    };
}

Thanks!

nrathi avatar May 25 '24 16:05 nrathi

Hey, checkout the last release 9.0.0-RC.0. The error handling was refactored.

The errorCode is now exposed:

export class EbayApiError extends EBayError {
  public readonly errorCode: number | undefined;
  public readonly meta?: EBayErrorMeta;

  constructor(message: string,
              description?: string,
              meta?: EBayErrorMeta,
              errorCode?: number | undefined) {
    super(message, description, meta);
    this.errorCode = errorCode;
  }
}

It's either number or undefined now.

dantio avatar May 29 '24 18:05 dantio

Thank you so much!

nrathi avatar May 29 '24 18:05 nrathi

And when it's undefined, how should we interpret that?

nrathi avatar May 29 '24 18:05 nrathi

I'm not sure. Actually it's should not happen that the error code is not defined. We are observing this version in prod. Let's see if this will ever happen.

Let me know if you have any other issue or suggestions to the error handling.

dantio avatar May 29 '24 19:05 dantio

@dantio just checking in - did you see any instances of "undefined" in prod?

nrathi avatar Jun 21 '24 13:06 nrathi

I am just trying 9.0.2 :)

My IDE (Webstorm) shows me two imports for EBayApiError for some reason, one is from ebay-api/errors and one is form ebay-api/lib/errors. The first one gives an error, the second seems to work. Is that correct?

And is the following the correct usage? If yes, can we have enums for the error codes? :)

    try {
      const result = await useEbay().trading.GetItem({
        ItemID: id,
      });
      // ...
    } catch (e: any) {
      if (e instanceof EBayApiError && e.errorCode === 17) {
        // Item not found :)
      } else {
        throw e;
      }
    }

MickL avatar Jun 22 '24 16:06 MickL

@nrathi

just checking in - did you see any instances of "undefined" in prod?

Yes. errorCode can be undefined especially in OAuth Error response.

dantio avatar Jun 23 '24 11:06 dantio

@MickL ebay-api/lib/errors is correct. It will point to ebay-api/dist/index.js if you are using import or ebay-api/lib/index.js with require.

However, you can also use ebay-api/errors/index.js, that should also work.

And is the following the correct usage?

Yes, it is.

If yes, can we have enums for the error codes? :)

eBay has a huge list of error codes. I have to check best practices how to deal with that first.

dantio avatar Jun 23 '24 11:06 dantio

Import from ebay-api/errors threw a TypeScript error for some reason. Otherwise I close this issue as resolved, thanks a lot! :)

MickL avatar Jun 23 '24 11:06 MickL