Skip to content

Commit

Permalink
fix(plugin): throw an explicit error message if service id is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
glc-omid committed Sep 27, 2024
1 parent 2fb077c commit 321b5ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ type Props = {
debug?: boolean;
};

const validateOptions = (options: Props) => {
if (!options.services) {
throw new Error('Please provide correct services configuration');
}
if (options.services.some((service) => !service.id)) {
throw new Error('The service id is required. please provide a service id');
}
if (options.services.some((service) => !service.path)) {
throw new Error('The service path is required. please provide the path to specification file');
}
};

export default async (_: any, options: Props) => {
if (!process.env.PROJECT_DIR) {
throw new Error('Please provide catalog url (env variable PROJECT_DIR)');
Expand All @@ -37,6 +49,7 @@ export default async (_: any, options: Props) => {
} = utils(process.env.PROJECT_DIR);

const services = options.services ?? [];
validateOptions(options);
for (const serviceSpec of services) {
console.log(chalk.green(`Processing ${serviceSpec.path}`));

Expand Down
27 changes: 27 additions & 0 deletions src/test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,33 @@ describe('OpenAPI EventCatalog Plugin', () => {

expect(service).toBeDefined();
});

it('[id] if the `id` not provided in the service config options, The generator throw an explicit error', async () => {
await expect(
plugin(config, {
services: [
{
path: join(openAPIExamples, 'petstore.yml'),
} as any,
],
})
).rejects.toThrow('The service id is required');
});
it('[services] if the `services` not provided in options, The generator throw an explicit error', async () => {
await expect(plugin(config, {} as any)).rejects.toThrow('Please provide correct services configuration');
});
it('[path] if the `path` not provided in service config options, The generator throw an explicit error', async () => {
await expect(
plugin(config, {
services: [
{
name: 'Awesome account service',
id: 'awsome-service',
} as any,
],
})
).rejects.toThrow('The service path is required. please provide the path to specification file');
});
});
});
});
Expand Down

0 comments on commit 321b5ea

Please sign in to comment.