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

refactor(core): optimize tenant not found error (#3128)

This commit is contained in:
Gao Sun 2023-02-16 15:29:28 +08:00 committed by GitHub
parent 94edf8a827
commit 016833905d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View file

@ -6,7 +6,7 @@ import chalk from 'chalk';
import type Koa from 'koa';
import { EnvSet } from '#src/env-set/index.js';
import { tenantPool } from '#src/tenants/index.js';
import { TenantNotFoundError, tenantPool } from '#src/tenants/index.js';
import { getTenantId } from '#src/utils/tenant.js';
const logListening = (type: 'core' | 'admin' = 'core') => {
@ -27,9 +27,20 @@ export default async function initApp(app: Koa): Promise<void> {
return next();
}
const tenant = await tenantPool.get(tenantId);
try {
const tenant = await tenantPool.get(tenantId);
await tenant.run(ctx, next);
return tenant.run(ctx, next);
return;
} catch (error: unknown) {
if (error instanceof TenantNotFoundError) {
ctx.status = 404;
return next();
}
throw error;
}
});
const { isHttpsEnabled, httpsCert, httpsKey, urlSet, adminUrlSet } = EnvSet.values;

View file

@ -8,6 +8,8 @@ import { parseDsn, stringifyDsn } from 'slonik';
import { EnvSet } from '#src/env-set/index.js';
export class TenantNotFoundError extends Error {}
/**
* This function is to fetch the tenant password for the corresponding Postgres user.
*
@ -27,7 +29,7 @@ export const getTenantDatabaseDsn = async (tenantId: string) => {
`);
if (!rows[0]) {
throw new Error(`Cannot find valid tenant credentials for ID ${tenantId}`);
throw new TenantNotFoundError(`Cannot find valid tenant credentials for ID ${tenantId}`);
}
const options = parseDsn(dbUrl);