From bb8d4d7fe2fd446aea64ddd74538eb4eee8d06f3 Mon Sep 17 00:00:00 2001 From: Javier Sierra Date: Mon, 23 Oct 2023 14:02:31 +0200 Subject: [PATCH] feat: control openapi schema not found Add custom error OpenApiSchemaNotFoundError No try/catch on index.js added to avoid future conflicts with #16. It will be done there. Closes #22 --- src/errors/openapi-schema-not-found-error.js | 8 ++++++++ src/services/user-flow-steps.js | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/errors/openapi-schema-not-found-error.js diff --git a/src/errors/openapi-schema-not-found-error.js b/src/errors/openapi-schema-not-found-error.js new file mode 100644 index 0000000..ab6b136 --- /dev/null +++ b/src/errors/openapi-schema-not-found-error.js @@ -0,0 +1,8 @@ +const OPENAPI_SCHEMA_NOT_FOUND_ERROR_MSG = 'No OpenAPI schema found in the given directory'; + +export class OpenApiSchemaNotFoundError extends Error { + constructor() { + super(OPENAPI_SCHEMA_NOT_FOUND_ERROR_MSG); + this.name = 'OpenApiSchemaNotFoundError'; + } +} diff --git a/src/services/user-flow-steps.js b/src/services/user-flow-steps.js index d9d239e..7941f94 100644 --- a/src/services/user-flow-steps.js +++ b/src/services/user-flow-steps.js @@ -1,6 +1,7 @@ import { checkbox, confirm, input } from '@inquirer/prompts'; import * as fs from 'node:fs'; import OpenApiMocker from '@os3/open-api-mocker'; +import { OpenApiSchemaNotFoundError } from '../errors/openapi-schema-not-found-error.js'; import cloneGitRepository from '../services/clone-git-repository.js'; import findOasFromDir from '../services/find-oas-from-dir.js'; import { originValidator, portValidator } from './inquirer-validators.js'; @@ -106,13 +107,16 @@ async function getOrigin() { * Start flow without config * @async * @function init - * @returns {Promise} An object with the complete config + * @returns {Promise} A object with the complete config + * @throws {OpenApiSchemaNotFoundError} When no schemas are found in the given directory */ async function init() { const schemasOrigin = await startNewFlow(); const schemas = await getSchemas(schemasOrigin); - + if (!schemas.length) { + throw new OpenApiSchemaNotFoundError(); + } const schemasToMock = await checkbox({ message: 'Select a schema', choices: schemas.map((schema) => {