diff --git a/packages/agent/src/agent.ts b/packages/agent/src/agent.ts index 313fb8c02b..13d930a134 100644 --- a/packages/agent/src/agent.ts +++ b/packages/agent/src/agent.ts @@ -212,7 +212,7 @@ export default class Agent extends FrameworkMounter this.nocodeCustomizer.addDataSource(this.customizer.getFactory()); this.nocodeCustomizer.use(this.customizationService.addCustomizations); - const dataSource = await this.nocodeCustomizer.getDataSource(logger); + const dataSource = await this.nocodeCustomizer.getDataSource(logger, isProduction); const [router] = await Promise.all([ this.getRouter(dataSource), this.sendSchema(dataSource), diff --git a/packages/datasource-customizer/src/datasource-customizer.ts b/packages/datasource-customizer/src/datasource-customizer.ts index bfc06cb3a7..57efb49270 100644 --- a/packages/datasource-customizer/src/datasource-customizer.ts +++ b/packages/datasource-customizer/src/datasource-customizer.ts @@ -167,14 +167,14 @@ export default class DataSourceCustomizer { return this; } - async getDataSource(logger: Logger): Promise { - await this.stack.applyQueuedCustomizations(logger); + async getDataSource(logger: Logger, isProduction = false): Promise { + await this.stack.applyQueuedCustomizations(logger, isProduction); return this.stack.dataSource; } getFactory(): DataSourceFactory { - return async (logger: Logger) => this.getDataSource(logger); + return async (logger: Logger, isProduction = false) => this.getDataSource(logger, isProduction); } async updateTypesOnFileSystem( diff --git a/packages/datasource-customizer/src/decorators/composite-datasource.ts b/packages/datasource-customizer/src/decorators/composite-datasource.ts index 2f7a70d3df..b4a506b977 100644 --- a/packages/datasource-customizer/src/decorators/composite-datasource.ts +++ b/packages/datasource-customizer/src/decorators/composite-datasource.ts @@ -6,6 +6,8 @@ import { DataSourceSchema, } from '@forestadmin/datasource-toolkit'; +import { MissingCollectionError } from '../errors'; + export default class CompositeDatasource implements DataSource { @@ -28,7 +30,7 @@ export default class CompositeDatasource } } - throw new Error( + throw new MissingCollectionError( `Collection '${name}' not found. List of available collections: ${this.collections .map(c => c.name) .sort() diff --git a/packages/datasource-customizer/src/decorators/decorators-stack.ts b/packages/datasource-customizer/src/decorators/decorators-stack.ts index 940136d572..fe639c231f 100644 --- a/packages/datasource-customizer/src/decorators/decorators-stack.ts +++ b/packages/datasource-customizer/src/decorators/decorators-stack.ts @@ -18,6 +18,7 @@ import SegmentCollectionDecorator from './segment/collection'; import SortEmulateCollectionDecorator from './sort-emulate/collection'; import ValidationCollectionDecorator from './validation/collection'; import WriteDataSourceDecorator from './write/datasource'; +import { MissingCollectionError } from '../errors'; export default class DecoratorsStack { action: DataSourceDecorator; @@ -96,13 +97,21 @@ export default class DecoratorsStack { * This method will be called recursively and clears the queue at each recursion to ensure * that all customizations are applied in the right order. */ - async applyQueuedCustomizations(logger: Logger): Promise { + async applyQueuedCustomizations(logger: Logger, isProduction = false): Promise { const queuedCustomizations = this.customizations.slice(); this.customizations.length = 0; while (queuedCustomizations.length) { - await queuedCustomizations.shift()(logger); // eslint-disable-line no-await-in-loop - await this.applyQueuedCustomizations(logger); // eslint-disable-line no-await-in-loop + try { + await queuedCustomizations.shift()(logger); // eslint-disable-line no-await-in-loop + await this.applyQueuedCustomizations(logger); // eslint-disable-line no-await-in-loop + } catch (e) { + if (e instanceof MissingCollectionError && isProduction) { + logger('Warn', e.message); + } else { + throw e; + } + } } } } diff --git a/packages/datasource-customizer/src/errors.ts b/packages/datasource-customizer/src/errors.ts new file mode 100644 index 0000000000..cba8376a95 --- /dev/null +++ b/packages/datasource-customizer/src/errors.ts @@ -0,0 +1,2 @@ +// eslint-disable-next-line import/prefer-default-export +export class MissingCollectionError extends Error {}