facebook / facebook-nodejs-business-sdk

Node.js SDK for Meta Marketing APIs
https://developers.facebook.com/docs/business-sdk
Other
487 stars 221 forks source link

Issue importing 'FacebookRequestError' #270

Open mrowles opened 6 months ago

mrowles commented 6 months ago

Which SDK version are you using?

18.0.0

What's the issue?

Cannot import FacebookRequestError from facebook-nodejs-business-sdk/src/exceptions.d.ts (can import anything else which is exported from index.d.ts of course).

I would like to import this as to do:

   } catch (error) {
     if (error instanceof FacebookRequestError) {
      throw new Error(`Something went wrong: [${error.response}]`);
    }
    throw new Error('Something went wrong');
   }

I assume there is some convoluted way to import this safely without complaints, but would prefer this is just accessible so I can reference the class easily. If there is a smarter way to do this, all ears!

My workaround is:

   } catch (error) {
     if ('response' in error) {
      throw new Error(`Something went wrong: [${error.response}]`);
    }
    throw new Error('Something went wrong');
   }

Steps/Sample code to reproduce the issue

import {
  Ad,
  AdAccount,
  Campaign,
  FacebookAdsApi,
} from 'facebook-nodejs-business-sdk';
import FacebookExceptions from 'facebook-nodejs-business-sdk/src/exceptions'; // <-- it would be good if this was exposed through index.d.ts as this import does not work (see observed results)

Observed Results:

export class FacebookRequestError extends FacebookError {
^^^^^^
SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Object.require.extensions.<computed> [as .js] (/Users/user/workspace/some-repo/node_modules/.pnpm/ts-node@10.9.1_@types+node@20.9.1_typescript@5.2.2/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/Users/user/workspace/some-repo/packages/service/src/lib/facebook.ts:7:1)

Expected Results:

This was easily importable since it's exported from index.

kishanio commented 5 months ago

This is needed for above usecase

gregleongf commented 5 months ago

I'm currently doing

if (error.constructor.name === 'FacebookRequestError') {
    ...
}

But it would be great to import the class.

mauudev commented 1 month ago

Hello here, I'm facing the same issue and doing the same workaround .. did you were able to import any sdk exceptions? Since there is a lot code errors to handle, this is a must for any conversions API development. I'm concerned that it's still open :/