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 <rijkvanzanten@me.com>
This commit is contained in:
José Varela
2024-04-17 21:00:20 +01:00
committed by GitHub
parent bf47a19428
commit 4bb03df6da
2 changed files with 17 additions and 7 deletions

View File

@@ -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

View File

@@ -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);
}
}
});
}