0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

fix(core): prevent uncaught promise rejection (#6009)

* 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
This commit is contained in:
simeng-li 2024-06-12 15:05:42 +08:00 committed by GitHub
parent 136320584f
commit 930f23e363
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View file

@ -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:');

View file

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