react-notion-x icon indicating copy to clipboard operation
react-notion-x copied to clipboard

Remove console.log and console.error from notion-client and instead throw errors or emit an error with EventEmitter

Open 9oelM opened this issue 3 years ago • 0 comments
trafficstars

Description

When notion-client sometimes encounters an error after requesting Notion pages, it prints the error with console.log or console.error. While it may be a good idea to print them to let the developer know what is happening, it may not be so if the developer wants to leave selective logs on the cloud from stdout/stderr, because you can't stop the underlying library from stdout/stderring .

The relevant source codes are here:

https://github.com/NotionX/react-notion-x/blob/bf81f2aa75c60b785e850b363901f47b9a2cd464/packages/notion-client/src/notion-api.ts#L183-L184 https://github.com/NotionX/react-notion-x/blob/bf81f2aa75c60b785e850b363901f47b9a2cd464/packages/notion-client/src/notion-api.ts#L271

In my specific case, I send a lot of requests to many different pages using notion-client, and receive lots of NotionAPI getSignedfileUrls error HTTPError: Response code 400 (Bad Request) or NotionAPI collectionQuery error <pageid> Cannot read properties of undefined , like so:

node notiontest.js

NotionAPI getSignedfileUrls error HTTPError: Response code 400 (Bad Request)
    at Request.<anonymous> (/Users/jm/Documents/Code/notion/node_modules/got/dist/source/as-promise/index.js:118:42)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1661741255805,
    socket: 1661741255806,
    lookup: 1661741255806,
    connect: 1661741256044,
    secureConnect: 1661741256328,
    upload: 1661741256334,
    response: 1661741256636,
    end: 1661741256636,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 1,
      dns: 0,
      tcp: 238,
      tls: 284,
      request: 6,
      firstByte: 302,
      download: 0,
      total: 831
    }
  }
}
NotionAPI getSignedfileUrls error HTTPError: Response code 400 (Bad Request)
    at Request.<anonymous> (/Users/jm/Documents/Code/notion/node_modules/got/dist/source/as-promise/index.js:118:42)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1661741256269,
    socket: 1661741256269,
    lookup: 1661741256284,
    connect: 1661741256402,
    secureConnect: 1661741256475,
    upload: 1661741256477,
    response: 1661741256727,
    end: 1661741256727,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 0,
      dns: 15,
      tcp: 118,
      tls: 73,
      request: 2,
      firstByte: 250,
      download: 0,
      total: 458
    }
  }
}
NotionAPI getSignedfileUrls error HTTPError: Response code 400 (Bad Request)
    at Request.<anonymous> (/Users/jm/Documents/Code/notion/node_modules/got/dist/source/as-promise/index.js:118:42)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1661741256667,
    socket: 1661741256667,
    lookup: 1661741256668,
    connect: 1661741256719,
    secureConnect: 1661741256794,
    upload: 1661741256808,
    response: 1661741257042,
    end: 1661741257042,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 0,
      dns: 1,
      tcp: 51,
      tls: 75,
      request: 14,
      firstByte: 234,
      download: 0,
      total: 375
    }
  }
}
NotionAPI getSignedfileUrls error HTTPError: Response code 400 (Bad Request)
    at Request.<anonymous> (/Users/jm/Documents/Code/notion/node_modules/got/dist/source/as-promise/index.js:118:42)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1661741256869,
    socket: 1661741256869,
    lookup: 1661741256889,
    connect: 1661741256956,
    secureConnect: 1661741257013,
    upload: 1661741257014,
    response: 1661741257299,
    end: 1661741257299,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 0,
      dns: 20,
      tcp: 67,
      tls: 57,
      request: 1,
      firstByte: 285,
      download: 0,
      total: 430
    }
  }
}

// ... and 

NotionAPI collectionQuery error adf27dc0-d1e2-4de8-910b-c6760a8da743 Cannot read properties of undefined (reading 'format')
TypeError: Cannot read properties of undefined (reading 'format')
    at ee.getCollectionData (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:3832)
    at j.concurrency (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:1821)
    at file:///Users/jm/Documents/Code/notion/test/node_modules/p-map/index.js:141:26
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
NotionAPI collectionQuery error ff91407e-976d-45ea-a4a6-04667e332e1a Cannot read properties of undefined (reading 'format')
TypeError: Cannot read properties of undefined (reading 'format')
    at ee.getCollectionData (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:3832)
    at j.concurrency (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:1821)
    at file:///Users/jm/Documents/Code/notion/test/node_modules/p-map/index.js:141:26
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
NotionAPI collectionQuery error 33f41029-e7dd-4956-86c2-780afeadbb55 Cannot read properties of undefined (reading 'format')
TypeError: Cannot read properties of undefined (reading 'format')
    at ee.getCollectionData (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:3832)
    at j.concurrency (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:1821)
    at file:///Users/jm/Documents/Code/notion/test/node_modules/p-map/index.js:141:26
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
NotionAPI collectionQuery error 181e961a-eb5c-4ee6-9153-07c0dfd5156d Cannot read properties of undefined (reading 'format')
TypeError: Cannot read properties of undefined (reading 'format')
    at ee.getCollectionData (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:3832)
    at j.concurrency (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:1821)
    at file:///Users/jm/Documents/Code/notion/test/node_modules/p-map/index.js:141:26
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
NotionAPI collectionQuery error 181e961a-eb5c-4ee6-9153-07c0dfd5156d Cannot read properties of undefined (reading 'format')
TypeError: Cannot read properties of undefined (reading 'format')
    at ee.getCollectionData (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:3832)
    at j.concurrency (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:1821)
    at file:///Users/jm/Documents/Code/notion/test/node_modules/p-map/index.js:141:26
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
NotionAPI collectionQuery error 181e961a-eb5c-4ee6-9153-07c0dfd5156d Cannot read properties of undefined (reading 'format')
TypeError: Cannot read properties of undefined (reading 'format')
    at ee.getCollectionData (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:3832)
    at j.concurrency (file:///Users/jm/Documents/Code/notion/test/node_modules/notion-client/build/index.js:1:1821)
    at file:///Users/jm/Documents/Code/notion/test/node_modules/p-map/index.js:141:26
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

I understand that seeing error messages may help you and getting an error is normal. But I think changing the library in the way that developers can be notified in some other way by catching an thrown error from notion-client or subscribing to an error with callbacks using some sort of EventEmitter would be much better. This is because it does not pollute the log.

For example, it could be something like:

try {
  await notionAPI.getPage(id)
} catch (e) {
   // the developer chooses what to do here
}

or

const notionAPI = new NotionAPI()

notionAPI.on(`error`, (err) => {
  // the developer chooses what to do here
})

await notionAPI.getPage(id)

Notion Test Page ID

The test page ID is e040febf70a94950b8620e6f00005004, but it's a bit more complicated than that. What my application essentially does is to start from a given notion page, and run BFS on all children pages until I tell it to stop. This is why sometimes so many errors are created, which is natural but needs to be handled properly.

You can replicate the exact thing by

node -v # use v16.15.1. That's what I'm using

npm init -y

npm i --save @graphcentral/notion-graph-scraper

curl "https://gist.githubusercontent.com/9oelM/ea5083eeccf3baa9697d4ce8af73d7ac/raw/5a0ca79fd258b41289c3a70c9d29172b67da7529/test.js" -o test.mjs

node test.mjs

Thanks a lot in advance.

9oelM avatar Aug 29 '22 03:08 9oelM