Skip to content

Commit

Permalink
feat(converter): create unified error boundary interface (#3999)
Browse files Browse the repository at this point in the history
convert and convertApiDOM functions with now always return
ConverError instances on failure.

Refs #3998
  • Loading branch information
char0n authored Apr 3, 2024
1 parent 4bc1f53 commit 833a918
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/apidom-converter/src/errors/ConvertError.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 5 additions & 1 deletion packages/apidom-converter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) => {
Expand Down
37 changes: 35 additions & 2 deletions packages/apidom-converter/test/index.ts
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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 () {
Expand Down

0 comments on commit 833a918

Please sign in to comment.