Skip to content

Commit

Permalink
Provide an option to allow throwing on parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinchernev committed Mar 11, 2021
1 parent ff80f64 commit e85a78d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "swagger-jsdoc",
"description": "Generates swagger doc based on JSDoc",
"version": "6.0.9",
"version": "6.1.0",
"engines": {
"node": ">=12.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { build } = require('./specification');
* Generates the specification.
* @param {object} options - Configuration options
* @param {string} options.encoding Optional, passed to readFileSync options. Defaults to 'utf8'.
* @param {boolean} options.failOnErrors Whether or not to throw when parsing errors. Defaults to false.
* @param {string} options.format Optional, defaults to '.json' - target file format '.yml' or '.yaml'.
* @param {object} options.swaggerDefinition
* @param {object} options.definition
Expand Down
3 changes: 3 additions & 0 deletions src/specification.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ function build(options) {
.filter((error) => !!error);

if (errReport.length) {
if (options.failOnErrors) {
throw new Error(errReport);
}
// Place to provide feedback for errors. Previously throwing, now reporting only.
console.info(
'Not all input has been taken into account at your final specification.'
Expand Down
24 changes: 24 additions & 0 deletions test/lib.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ describe('Main lib module', () => {
tags: [],
});
});

it('should support a flag for throw errors', () => {
expect(() => {
swaggerJsdoc({
swaggerDefinition: {
info: {
title: 'Example weird characters',
version: '1.0.0',
},
},
apis: [path.resolve(__dirname, './files/v2/wrong_syntax.yaml')],
failOnErrors: true,
});
})
.toThrow(`YAMLSemanticError: The !!! tag handle is non-default and was not declared. at line 2, column 3:
!!!title: Hello World
^^^^^^^^^^^^^^^^^^^^^…
YAMLSemanticError: Implicit map keys need to be on a single line at line 2, column 3:
!!!title: Hello World
^^^^^^^^^^^^^^^^^^^^^…`);
});
});

describe('Error handling', () => {
Expand Down

0 comments on commit e85a78d

Please sign in to comment.