Title: UnhandledPromiseRejection thrown from within SDK · Issue #41 · contentstack/contentstack-javascript · GitHub
Open Graph Title: UnhandledPromiseRejection thrown from within SDK · Issue #41 · contentstack/contentstack-javascript
X Title: UnhandledPromiseRejection thrown from within SDK · Issue #41 · contentstack/contentstack-javascript
Description: Greetings, It appears that the SDK has the potential to cause an UnhandledPromiseRejection when the Contentstack REST API returns a non-well-formed JSON response (such as HTML). In the future versions of Node.js, this will cause the proc...
Open Graph Description: Greetings, It appears that the SDK has the potential to cause an UnhandledPromiseRejection when the Contentstack REST API returns a non-well-formed JSON response (such as HTML). In the future versi...
X Description: Greetings, It appears that the SDK has the potential to cause an UnhandledPromiseRejection when the Contentstack REST API returns a non-well-formed JSON response (such as HTML). In the future versi...
Opengraph URL: https://github.com/contentstack/contentstack-javascript/issues/41
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"UnhandledPromiseRejection thrown from within SDK","articleBody":"Greetings,\r\n\r\nIt appears that the SDK has the potential to cause an `UnhandledPromiseRejection` when the Contentstack REST API returns a non-well-formed JSON response (such as HTML).\r\n\r\nIn the future versions of Node.js, this will cause the process to crash, as suggested by the deprecation warning:\r\n\r\n\u003e [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.\r\n\r\n## Exception Example\r\n\r\n```\r\n$ node index.js\r\n(node:26048) UnhandledPromiseRejectionWarning: FetchError: invalid json response body at https://REDACTED:443/v3/content_types/REDACTED/entries/?locale=en-us\u0026include_count=true\u0026environment=REDACTED reason: Unexpected token \u003c in JSON at position 9\r\n at eval (webpack://Contentstack/./node_modules/node-fetch/lib/index.mjs?:276:32)\r\n at processTicksAndRejections (internal/process/task_queues.js:97:5)\r\n(node:26048) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)\r\n(node:26048) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.\r\n```\r\n\r\n## Minimal reproduction example\r\n\r\n```\r\nnpm install dotenv node-request-interceptor contentstack\r\n```\r\n\r\n```js\r\nrequire('dotenv').config()\r\n\r\nconst Contentstack = require('contentstack');\r\nconst { RequestInterceptor } = require('node-request-interceptor');\r\nconst { default: withDefaultInterceptors } = require('node-request-interceptor/lib/presets/default');\r\n\r\nconst interceptor = new RequestInterceptor(withDefaultInterceptors)\r\n\r\n// Log any outgoing requests to contentstack and return a mock HTML response\r\n// which will cause the SDK to throw an unhandled exception\r\ninterceptor.use((req) =\u003e {\r\n\r\n console.log('%s %s', req.method, req.url.href);\r\n\r\n return {\r\n status: 401,\r\n headers: {\r\n 'x-powered-by': 'node-request-interceptor',\r\n 'content-type': 'text/html'\r\n },\r\n body: `\r\n \u003chtml\u003e\r\n \u003chead\u003e\u003c/head\u003e\r\n \u003cbody\u003e\r\n \u003ch1\u003eUnauthorized\u003c/h1\u003e\r\n \u003c/body\u003e\r\n \u003c/html\u003e\r\n `,\r\n }\r\n});\r\n\r\nconst Stack = Contentstack.Stack(\r\n process.env.CONTENTSTACK_API_KEY,\r\n process.env.CONTENTSTACK_DELIVERY_TOKEN,\r\n process.env.CONTENTSTACK_ENVIRONMENT,\r\n process.env.CONTENTSTACK_DB_REGION || null,\r\n { timeout: 5000 });\r\n\r\nStack.setHost(process.env.CONTENTSTACK_HOST);\r\n\r\nconst Query = Stack\r\n .ContentType(process.env.CONTENTSTACK_CONTENT_TYPE)\r\n .Query({})\r\n .language(\"en-us\")\r\n .includeCount();\r\n\r\n// Executing this async invocation cause an unhandled promise rejection\r\nQuery\r\n .toJSON()\r\n .find()\r\n .then((res) =\u003e {\r\n const [,count] = res;\r\n console.log(`${count} total blogs.`);\r\n }, function (err) {\r\n // Error is not caught\r\n console.error(err);\r\n })\r\n .catch((err) =\u003e {\r\n // Error is not caught\r\n console.error(err);\r\n });\r\n\r\n```","author":{"url":"https://github.com/viglucci","@type":"Person","name":"viglucci"},"datePublished":"2021-01-20T00:37:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/41/contentstack-javascript/issues/41"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b152a3b1-3c70-5d31-2f5a-5a554b8fb529 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F6E:3D5ADE:107D34F:15B6D5D:69814350 |
| html-safe-nonce | 7538fb49b808f6347e4ba530122c8ad25bbbacb511211928923605530ef90a4b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjZFOjNENUFERToxMDdEMzRGOjE1QjZENUQ6Njk4MTQzNTAiLCJ2aXNpdG9yX2lkIjoiNzAwMjUzNTE5MzkzMjI4NDc1MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1605213179d52f05af14023c550fee91373f5146615afb782c85dad3db4d1613 |
| hovercard-subject-tag | issue:789510428 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/contentstack/contentstack-javascript/41/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e6c0d072fbe2ce29cf37250eca0c421beef6806447b5968ae8233838e981865d/contentstack/contentstack-javascript/issues/41 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e6c0d072fbe2ce29cf37250eca0c421beef6806447b5968ae8233838e981865d/contentstack/contentstack-javascript/issues/41 |
| og:image:alt | Greetings, It appears that the SDK has the potential to cause an UnhandledPromiseRejection when the Contentstack REST API returns a non-well-formed JSON response (such as HTML). In the future versi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | viglucci |
| hostname | github.com |
| expected-hostname | github.com |
| None | 39fe8101494cbb823c09b619b68c80cd4d05ab7279997038dbe06bb91608abe1 |
| turbo-cache-control | no-preview |
| go-import | github.com/contentstack/contentstack-javascript git https://github.com/contentstack/contentstack-javascript.git |
| octolytics-dimension-user_id | 24450751 |
| octolytics-dimension-user_login | contentstack |
| octolytics-dimension-repository_id | 76627300 |
| octolytics-dimension-repository_nwo | contentstack/contentstack-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 76627300 |
| octolytics-dimension-repository_network_root_nwo | contentstack/contentstack-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d5b34a4e4898b066c629879feb4b184bc471d6a7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width