From 4bb03df6da8f2fadcef578fdea7d0e54fa7bb7d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Varela?= Date: Wed, 17 Apr 2024 21:00:20 +0100 Subject: [PATCH] Api: Bubble up the error to prevent `unhandledRejection` (#22231) * Bubble up the error to prevent `unhandledRejection` * Add changeset * Remove unused async context * Catch callback errors Just in the off chance the bus.publish will throw an error --------- Co-authored-by: Rijk van Zanten --- .changeset/lemon-cobras-draw.md | 5 +++++ api/src/utils/get-schema.ts | 19 ++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 .changeset/lemon-cobras-draw.md diff --git a/.changeset/lemon-cobras-draw.md b/.changeset/lemon-cobras-draw.md new file mode 100644 index 0000000000..54c8ceb581 --- /dev/null +++ b/.changeset/lemon-cobras-draw.md @@ -0,0 +1,5 @@ +--- +'@directus/api': patch +--- + +Fixed an issue that could cause errors happening during schema retrieval to exit the process rather than return a 500 diff --git a/api/src/utils/get-schema.ts b/api/src/utils/get-schema.ts index 4013008fab..ab05460520 100644 --- a/api/src/utils/get-schema.ts +++ b/api/src/utils/get-schema.ts @@ -68,22 +68,27 @@ export async function getSchema( if (currentProcessShouldHandleOperation === false) { logger.trace('Schema cache is prepared in another process, waiting for result.'); - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const TIMEOUT = 10000; - const timeout: NodeJS.Timeout = setTimeout(async () => { + const timeout: NodeJS.Timeout = setTimeout(() => { logger.trace('Did not receive schema callback message in time. Pulling schema...'); - callback(); + callback().catch(reject); }, TIMEOUT); bus.subscribe(messageKey, callback); async function callback() { - if (timeout) clearTimeout(timeout); + try { + if (timeout) clearTimeout(timeout); - const schema = await getSchema(options, attempt + 1); - resolve(schema); - bus.unsubscribe(messageKey, callback); + const schema = await getSchema(options, attempt + 1); + resolve(schema); + } catch (error) { + reject(error); + } finally { + bus.unsubscribe(messageKey, callback); + } } }); }