diff --git a/index.js b/index.js index 7466bd9..5023785 100644 --- a/index.js +++ b/index.js @@ -8,12 +8,21 @@ const fs = require('fs'); const readFileAsync = require('util').promisify(fs.readFile); const oas3schema = require('./refs/oas3-schema.json'); -function InvalidTypeError(message) { - this.name = 'InvalidTypeError'; - this.message = message; +class InvalidTypeError extends Error { + constructor(message) { + super() + this.name = 'InvalidTypeError'; + this.message = message; + } } -InvalidTypeError.prototype = new Error(); +class DereferencingError extends Error { + constructor(errors) { + super() + this.name = 'DereferencingError'; + this.errors = errors; + } +} async function convert(schema, options = {}) { const { cloneSchema = true, dereference = false } = options; @@ -23,7 +32,11 @@ async function convert(schema, options = {}) { } if (dereference) { - ({ result: schema } = await resolver.resolve(schema)); + const result = await resolver.resolve(schema); + if (result.errors && result.errors.length > 0) { + throw new DereferencingError(result.errors) + } + schema = result.result; } const vocab = schemaWalker.getVocabulary(schema, schemaWalker.vocabularies.DRAFT_04); diff --git a/test/dereference_schema.test.js b/test/dereference_schema.test.js index e7b1111..95fbd60 100644 --- a/test/dereference_schema.test.js +++ b/test/dereference_schema.test.js @@ -104,3 +104,23 @@ it('dereferencing schema with file references', async () => { should(result).deepEqual(expected, 'result does not match the expected'); }); + +it('throws an error when dereferecing fails', async () => { + const schema = { + $schema: "http://json-schema.org/draft-04/schema#", + properties: { + foo: { + $ref: "./bad.json", + }, + }, + }; + + let error; + try { + await convert(schema, { dereference: true }); + } catch (e) { + error = e; + } + + should(error).have.property('errors') +})