Skip to content

Commit

Permalink
fix(core): prevent uncaught promise rejection (#6009)
Browse files Browse the repository at this point in the history
* fix(core): prevent uncaught promise rejection

prevent uncaught promise rejection crashing the app

* refactor(core): remove inline await

remove inline await statement

* chore(core): update comment

update comment
  • Loading branch information
simeng-li committed Jun 12, 2024
1 parent 1363205 commit 930f23e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/core/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ try {
SystemContext.shared.loadProviderConfigs(sharedAdminPool),
]);

/**
* Catch unhandled promise rejections and log them to Application Insights.
* The unhandled promise rejection was first observed in the `TenantPool.get()` method.
*
* In this method, if the `tenantId` is not found, `Tenant.create()` will throw an error.
* We use a try-catch block to catch the error and throw it with logging.
* However, if the `Tenant.create()` Promise is read from the cache, somehow the error is not caught.
* The root cause of this error is still unknown. To avoid the app from crashing, we catch the error here.
*
* @see TenantPool.get
*/
process.on('unhandledRejection', (error) => {
consoleLog.error(error);
void appInsights.trackException(error);
});

await initApp(app);
} catch (error: unknown) {
consoleLog.error('Error while initializing app:');
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/tenants/Tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export default class Tenant implements TenantContext {
// Try to avoid unexpected "triggerUncaughtException" by using try-catch block
try {
// Treat the default database URL as the management URL
const envSet = new EnvSet(id, await getTenantDatabaseDsn(id));
const tenantDatabaseDsn = await getTenantDatabaseDsn(id);
const envSet = new EnvSet(id, tenantDatabaseDsn);
// Custom endpoint is used for building OIDC issuer URL when the request is a custom domain
await envSet.load(customDomain);

Expand Down

0 comments on commit 930f23e

Please sign in to comment.