mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
refactor(core): optimize tenant not found error (#3128)
This commit is contained in:
parent
94edf8a827
commit
016833905d
2 changed files with 17 additions and 4 deletions
|
@ -6,7 +6,7 @@ import chalk from 'chalk';
|
||||||
import type Koa from 'koa';
|
import type Koa from 'koa';
|
||||||
|
|
||||||
import { EnvSet } from '#src/env-set/index.js';
|
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';
|
import { getTenantId } from '#src/utils/tenant.js';
|
||||||
|
|
||||||
const logListening = (type: 'core' | 'admin' = 'core') => {
|
const logListening = (type: 'core' | 'admin' = 'core') => {
|
||||||
|
@ -27,9 +27,20 @@ export default async function initApp(app: Koa): Promise<void> {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const tenant = await tenantPool.get(tenantId);
|
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;
|
const { isHttpsEnabled, httpsCert, httpsKey, urlSet, adminUrlSet } = EnvSet.values;
|
||||||
|
|
|
@ -8,6 +8,8 @@ import { parseDsn, stringifyDsn } from 'slonik';
|
||||||
|
|
||||||
import { EnvSet } from '#src/env-set/index.js';
|
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.
|
* 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]) {
|
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);
|
const options = parseDsn(dbUrl);
|
||||||
|
|
Loading…
Reference in a new issue