diff --git a/packages/apidom-converter/src/errors/ConvertError.ts b/packages/apidom-converter/src/errors/ConvertError.ts index 4c72056d07..80c038e32e 100644 --- a/packages/apidom-converter/src/errors/ConvertError.ts +++ b/packages/apidom-converter/src/errors/ConvertError.ts @@ -1,5 +1,5 @@ -import { ApiDOMError } from '@swagger-api/apidom-error'; +import { ApiDOMStructuredError } from '@swagger-api/apidom-error'; -class ConvertError extends ApiDOMError {} +class ConvertError extends ApiDOMStructuredError {} export default ConvertError; diff --git a/packages/apidom-converter/src/index.ts b/packages/apidom-converter/src/index.ts index 6d86bf6c2d..cf0857f9a8 100644 --- a/packages/apidom-converter/src/index.ts +++ b/packages/apidom-converter/src/index.ts @@ -23,7 +23,11 @@ export const convertApiDOM = async (element: ParseResultElement, options = {}) = throw new UnmatchedConvertStrategyError(file.uri); } - return strategy.convert(file, mergedOptions); + try { + return strategy.convert(file, mergedOptions); + } catch (error) { + throw new ConvertError(`Error while converting file "${file.uri}"`, { cause: error, strategy }); + } }; const convert = async (uri: string, options = {}) => { diff --git a/packages/apidom-converter/test/index.ts b/packages/apidom-converter/test/index.ts index 07287d5892..8bdcaf11cb 100644 --- a/packages/apidom-converter/test/index.ts +++ b/packages/apidom-converter/test/index.ts @@ -1,11 +1,11 @@ import path from 'node:path'; -import { expect } from 'chai'; +import { assert, expect } from 'chai'; import { toJSON } from '@swagger-api/apidom-core'; import { mediaTypes as openAPI30MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-0'; import { mediaTypes as openAPI31MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-1'; import { parse } from '@swagger-api/apidom-reference'; -import convert, { convertApiDOM } from '../src'; +import convert, { convertApiDOM, ConvertError } from '../src'; describe('apidom-converter', function () { context('convert', function () { @@ -30,6 +30,39 @@ describe('apidom-converter', function () { expect(toJSON(convertedParseResult.api!, undefined, 2)).toMatchSnapshot(); }); }); + + context('given convert strategy throws error', function () { + specify('should throw ConvertError', async function () { + const fixturePath = path.join( + __dirname, + 'strategies', + 'openapi-3-1-to-openapi-3-0-3', + 'refractor-plugins', + 'openapi-version', + 'fixtures', + 'openapi-version.json', + ); + + try { + await convert(fixturePath, { + convert: { + strategies: [ + { + canConvert: () => true, + convert: () => { + throw new Error('test'); + }, + }, + ], + }, + }); + assert.fail('should throw ConvertError'); + } catch (error: any) { + assert.instanceOf(error, ConvertError); + assert.strictEqual(error.cause.message, 'test'); + } + }); + }); }); context('convertApiDOM', function () {