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) => {