Skip to content

Commit

Permalink
feat: control openapi schema not found
Browse files Browse the repository at this point in the history
Add custom error OpenApiSchemaNotFoundError
No try/catch on index.js added to avoid future conflicts with #16. It will be done there.

Closes #22
  • Loading branch information
javier-sierra-sngular committed Oct 25, 2023
1 parent 51d9b1d commit bb8d4d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/errors/openapi-schema-not-found-error.js
Original file line number Diff line number Diff line change
@@ -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';
}
}
8 changes: 6 additions & 2 deletions src/services/user-flow-steps.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -106,13 +107,16 @@ async function getOrigin() {
* Start flow without config
* @async
* @function init
* @returns {Promise<Config>} An object with the complete config
* @returns {Promise<Config>} 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) => {
Expand Down

0 comments on commit bb8d4d7

Please sign in to comment.