From 1430c81f748c37488080874c6c451a335f48e0cd Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Fri, 1 Dec 2023 09:45:50 +0100 Subject: [PATCH] refactor(reference): consolidate error hierarchies --- .../src/errors/MaximumResolverDepthError.ts | 4 ++-- .../apidom-reference/src/errors/ParseError.ts | 5 +++++ .../apidom-reference/src/errors/ParserError.ts | 4 ++-- .../src/errors/ResolveError.ts | 5 +++++ .../src/errors/ResolverError.ts | 4 ++-- .../UnmatchedDereferenceStrategyError.ts | 4 ++-- .../errors/UnmatchedResolveStrategyError.ts | 4 ++-- packages/apidom-reference/src/index.ts | 2 ++ packages/apidom-reference/src/parse/index.ts | 6 +++--- packages/apidom-reference/src/resolve/index.ts | 4 ++-- packages/apidom-reference/src/resolve/util.ts | 4 ++-- .../openapi-3-1/schema-object/index.ts | 6 +++--- packages/apidom-reference/test/parse/index.ts | 14 +++++++------- .../apidom-reference/test/resolve/index.ts | 16 ++++++++-------- .../openapi-2/reference-object/index.ts | 18 +++++++++--------- .../openapi-3-1/schema-object/index.ts | 11 ++++++----- 16 files changed, 62 insertions(+), 49 deletions(-) create mode 100644 packages/apidom-reference/src/errors/ParseError.ts create mode 100644 packages/apidom-reference/src/errors/ResolveError.ts diff --git a/packages/apidom-reference/src/errors/MaximumResolverDepthError.ts b/packages/apidom-reference/src/errors/MaximumResolverDepthError.ts index 2f3ebbc18d..adf8f63c27 100644 --- a/packages/apidom-reference/src/errors/MaximumResolverDepthError.ts +++ b/packages/apidom-reference/src/errors/MaximumResolverDepthError.ts @@ -1,5 +1,5 @@ -import ResolverError from './ResolverError'; +import ResolveError from './ResolveError'; -class MaximumResolverDepthError extends ResolverError {} +class MaximumResolverDepthError extends ResolveError {} export default MaximumResolverDepthError; diff --git a/packages/apidom-reference/src/errors/ParseError.ts b/packages/apidom-reference/src/errors/ParseError.ts new file mode 100644 index 0000000000..7db180a8c4 --- /dev/null +++ b/packages/apidom-reference/src/errors/ParseError.ts @@ -0,0 +1,5 @@ +import { ApiDOMError } from '@swagger-api/apidom-error'; + +class ParseError extends ApiDOMError {} + +export default ParseError; diff --git a/packages/apidom-reference/src/errors/ParserError.ts b/packages/apidom-reference/src/errors/ParserError.ts index 973d60c095..69d558b37d 100644 --- a/packages/apidom-reference/src/errors/ParserError.ts +++ b/packages/apidom-reference/src/errors/ParserError.ts @@ -1,5 +1,5 @@ -import { ApiDOMError } from '@swagger-api/apidom-error'; +import ParseError from './ParseError'; -class ParserError extends ApiDOMError {} +class ParserError extends ParseError {} export default ParserError; diff --git a/packages/apidom-reference/src/errors/ResolveError.ts b/packages/apidom-reference/src/errors/ResolveError.ts new file mode 100644 index 0000000000..f89d0f91d6 --- /dev/null +++ b/packages/apidom-reference/src/errors/ResolveError.ts @@ -0,0 +1,5 @@ +import { ApiDOMError } from '@swagger-api/apidom-error'; + +class ResolveError extends ApiDOMError {} + +export default ResolveError; diff --git a/packages/apidom-reference/src/errors/ResolverError.ts b/packages/apidom-reference/src/errors/ResolverError.ts index 0d4b1efa91..b60dabe202 100644 --- a/packages/apidom-reference/src/errors/ResolverError.ts +++ b/packages/apidom-reference/src/errors/ResolverError.ts @@ -1,5 +1,5 @@ -import { ApiDOMError } from '@swagger-api/apidom-error'; +import ResolveError from './ResolveError'; -class ResolverError extends ApiDOMError {} +class ResolverError extends ResolveError {} export default ResolverError; diff --git a/packages/apidom-reference/src/errors/UnmatchedDereferenceStrategyError.ts b/packages/apidom-reference/src/errors/UnmatchedDereferenceStrategyError.ts index 815af9eaba..033b242621 100644 --- a/packages/apidom-reference/src/errors/UnmatchedDereferenceStrategyError.ts +++ b/packages/apidom-reference/src/errors/UnmatchedDereferenceStrategyError.ts @@ -1,5 +1,5 @@ -import ParserError from './ParserError'; +import DereferenceError from './DereferenceError'; -class UnmatchedDereferenceStrategyError extends ParserError {} +class UnmatchedDereferenceStrategyError extends DereferenceError {} export default UnmatchedDereferenceStrategyError; diff --git a/packages/apidom-reference/src/errors/UnmatchedResolveStrategyError.ts b/packages/apidom-reference/src/errors/UnmatchedResolveStrategyError.ts index f3798e9395..44eb112081 100644 --- a/packages/apidom-reference/src/errors/UnmatchedResolveStrategyError.ts +++ b/packages/apidom-reference/src/errors/UnmatchedResolveStrategyError.ts @@ -1,5 +1,5 @@ -import ParserError from './ParserError'; +import ResolveError from './ResolveError'; -class UnmatchedResolveStrategyError extends ParserError {} +class UnmatchedResolveStrategyError extends ResolveError {} export default UnmatchedResolveStrategyError; diff --git a/packages/apidom-reference/src/index.ts b/packages/apidom-reference/src/index.ts index 7671bf3e09..5a90626e10 100644 --- a/packages/apidom-reference/src/index.ts +++ b/packages/apidom-reference/src/index.ts @@ -35,8 +35,10 @@ export { default as JsonSchema$anchorError } from './errors/JsonSchema$anchorErr export { default as JsonSchemaURIError } from './errors/JsonSchemaUriError'; export { default as MaximumDereferenceDepthError } from './errors/MaximumDereferenceDepthError'; export { default as MaximumResolverDepthError } from './errors/MaximumResolverDepthError'; +export { default as ParseError } from './errors/ParseError'; export { default as ParserError } from './errors/ParserError'; export { default as PluginError } from './errors/PluginError'; +export { default as ResolveError } from './errors/ResolveError'; export { default as ResolverError } from './errors/ResolverError'; export { default as UnmatchedDereferenceStrategyError } from './errors/UnmatchedDereferenceStrategyError'; export { default as UnmatchedResolveStrategyError } from './errors/UnmatchedResolveStrategyError'; diff --git a/packages/apidom-reference/src/parse/index.ts b/packages/apidom-reference/src/parse/index.ts index 93d0dc9ad5..46c1dcd544 100644 --- a/packages/apidom-reference/src/parse/index.ts +++ b/packages/apidom-reference/src/parse/index.ts @@ -5,7 +5,7 @@ import * as url from '../util/url'; import File from '../util/File'; import * as plugins from '../util/plugins'; import { ReferenceOptions as IReferenceOptions, File as IFile, Parser as IParser } from '../types'; -import ParserError from '../errors/ParserError'; +import ParseError from '../errors/ParseError'; import UnmatchedResolverError from '../errors/UnmatchedResolverError'; import { readFile } from '../resolve/util'; @@ -31,13 +31,13 @@ const parseFile = async (file: IFile, options: IReferenceOptions): Promise( const { result } = await plugins.run('resolve', [file, options], resolveStrategies); return result; } catch (error: any) { - throw new ResolverError(`Error while resolving file "${file.uri}"`, { cause: error }); + throw new ResolveError(`Error while resolving file "${file.uri}"`, { cause: error }); } }; diff --git a/packages/apidom-reference/src/resolve/util.ts b/packages/apidom-reference/src/resolve/util.ts index 0a825d3cfd..c302a65c03 100644 --- a/packages/apidom-reference/src/resolve/util.ts +++ b/packages/apidom-reference/src/resolve/util.ts @@ -6,7 +6,7 @@ import { Resolver as IResolver, } from '../types'; import * as plugins from '../util/plugins'; -import ResolverError from '../errors/ResolverError'; +import ResolveError from '../errors/ResolveError'; import UnmatchedResolverError from '../errors/UnmatchedResolverError'; /** @@ -30,6 +30,6 @@ export const readFile = async (file: IFile, options: IReferenceOptions): Promise const { result } = await plugins.run('read', [file], resolvers); return result; } catch (error: any) { - throw new ResolverError(`Error while reading file "${file.uri}"`, { cause: error }); + throw new ResolveError(`Error while reading file "${file.uri}"`, { cause: error }); } }; diff --git a/packages/apidom-reference/test/dereference/strategies/openapi-3-1/schema-object/index.ts b/packages/apidom-reference/test/dereference/strategies/openapi-3-1/schema-object/index.ts index 590e319d1a..f1836ce2ab 100644 --- a/packages/apidom-reference/test/dereference/strategies/openapi-3-1/schema-object/index.ts +++ b/packages/apidom-reference/test/dereference/strategies/openapi-3-1/schema-object/index.ts @@ -8,7 +8,7 @@ import { dereference, parse, Reference, ReferenceSet } from '../../../../../src' import DereferenceError from '../../../../../src/errors/DereferenceError'; import MaximumDereferenceDepthError from '../../../../../src/errors/MaximumDereferenceDepthError'; import MaximumResolverDepthError from '../../../../../src/errors/MaximumResolverDepthError'; -import ResolverError from '../../../../../src/errors/ResolverError'; +import ResolveError from '../../../../../src/errors/ResolveError'; import EvaluationJsonSchema$anchorError from '../../../../../src/errors/EvaluationJsonSchema$anchorError'; import EvaluationJsonSchemaUriError from '../../../../../src/errors/EvaluationJsonSchemaUriError'; import { loadJsonFile } from '../../../../helpers'; @@ -466,7 +466,7 @@ describe('dereference', function () { assert.fail('should throw DereferenceError'); } catch (error: any) { assert.instanceOf(error, DereferenceError); - assert.instanceOf(error.cause.cause, ResolverError); + assert.instanceOf(error.cause.cause, ResolveError); assert.match(error.cause.cause.message, /\/schemas\/nested\/ex\.json"$/); } }); @@ -583,7 +583,7 @@ describe('dereference', function () { assert.fail('should throw DereferenceError'); } catch (error: any) { assert.instanceOf(error, DereferenceError); - assert.instanceOf(error.cause.cause, ResolverError); + assert.instanceOf(error.cause.cause, ResolveError); } }); }); diff --git a/packages/apidom-reference/test/parse/index.ts b/packages/apidom-reference/test/parse/index.ts index 2bb7858775..21fce456e9 100644 --- a/packages/apidom-reference/test/parse/index.ts +++ b/packages/apidom-reference/test/parse/index.ts @@ -7,8 +7,8 @@ import { mediaTypes } from '@swagger-api/apidom-ns-openapi-3-1'; import defaultOptions from '../../src/options'; import { merge as mergeOptions } from '../../src/options/util'; import parse from '../../src/parse'; -import ParserError from '../../src/errors/ParserError'; -import ResolverError from '../../src/errors/ResolverError'; +import ParseError from '../../src/errors/ParseError'; +import ResolveError from '../../src/errors/ResolveError'; import UnmatchedResolverError from '../../src/errors/UnmatchedResolverError'; import OpenApiJson3_1Parser from '../../src/parse/parsers/openapi-json-3-1'; @@ -95,9 +95,9 @@ describe('parse', function () { try { await parse(uri, options); - assert.fail('Should throw ResolverError'); + assert.fail('Should throw ResolveError'); } catch (error) { - assert.instanceOf(error, ResolverError); + assert.instanceOf(error, ResolveError); } }); }); @@ -116,7 +116,7 @@ describe('parse', function () { await parse(uri, options); assert.fail('Should throw ParserError'); } catch (error: any) { - assert.instanceOf(error, ParserError); + assert.instanceOf(error, ParseError); assert.match(error.message, /File is empty\.$/); } }); @@ -145,9 +145,9 @@ describe('parse', function () { try { await parse(uri, options); - assert.fail('Should throw ParserError'); + assert.fail('Should throw ParseError'); } catch (error) { - assert.instanceOf(error, ParserError); + assert.instanceOf(error, ParseError); } }); }); diff --git a/packages/apidom-reference/test/resolve/index.ts b/packages/apidom-reference/test/resolve/index.ts index 2d8c4fbf5e..9722fbe7de 100644 --- a/packages/apidom-reference/test/resolve/index.ts +++ b/packages/apidom-reference/test/resolve/index.ts @@ -5,8 +5,8 @@ import { mediaTypes } from '@swagger-api/apidom-ns-openapi-3-1'; import { resolve, resolveApiDOM, parse } from '../../src'; import FileResolver from '../../src/resolve/resolvers/file/index-node'; import UnmatchedResolveStrategyError from '../../src/errors/UnmatchedResolveStrategyError'; -import ResolverError from '../../src/errors/ResolverError'; -import ParserError from '../../src/errors/ParserError'; +import ResolveError from '../../src/errors/ResolveError'; +import ParseError from '../../src/errors/ParseError'; import OpenApiJson3_1Parser from '../../src/parse/parsers/openapi-json-3-1'; const fixturePath = path.join( @@ -133,9 +133,9 @@ describe('resolve', function () { try { await resolve(uri, options); - assert.fail('Should throw ResolverError'); + assert.fail('Should throw ResolveError'); } catch (error) { - assert.instanceOf(error, ResolverError); + assert.instanceOf(error, ResolveError); } }); }); @@ -177,9 +177,9 @@ describe('resolve', function () { try { await resolve(uri, options); - assert.fail('Should throw ParserError'); + assert.fail('Should throw ParseError'); } catch (error: any) { - assert.instanceOf(error, ParserError); + assert.instanceOf(error, ParseError); assert.match(error.message, /File is empty\.$/); } }); @@ -208,9 +208,9 @@ describe('resolve', function () { try { await resolve(uri, options); - assert.fail('Should throw ParserError'); + assert.fail('Should throw ParseError'); } catch (error) { - assert.instanceOf(error, ParserError); + assert.instanceOf(error, ParseError); } }); }); diff --git a/packages/apidom-reference/test/resolve/strategies/openapi-2/reference-object/index.ts b/packages/apidom-reference/test/resolve/strategies/openapi-2/reference-object/index.ts index 183b08af15..4eea5465e0 100644 --- a/packages/apidom-reference/test/resolve/strategies/openapi-2/reference-object/index.ts +++ b/packages/apidom-reference/test/resolve/strategies/openapi-2/reference-object/index.ts @@ -5,7 +5,7 @@ import { mediaTypes } from '@swagger-api/apidom-ns-openapi-2'; import { resolve } from '../../../../../src'; import MaximumResolverDepthError from '../../../../../src/errors/MaximumResolverDepthError'; import MaximumDereferenceDepthError from '../../../../../src/errors/MaximumDereferenceDepthError'; -import ResolverError from '../../../../../src/errors/ResolverError'; +import ResolveError from '../../../../../src/errors/ResolveError'; const rootFixturePath = path.join(__dirname, 'fixtures'); @@ -102,7 +102,7 @@ describe('resolve', function () { parse: { mediaType: mediaTypes.latest('json') }, }); } catch (e) { - assert.instanceOf(e, ResolverError); + assert.instanceOf(e, ResolveError); } }); }); @@ -117,7 +117,7 @@ describe('resolve', function () { parse: { mediaType: mediaTypes.latest('json') }, }); } catch (e) { - assert.instanceOf(e, ResolverError); + assert.instanceOf(e, ResolveError); } }); }); @@ -132,7 +132,7 @@ describe('resolve', function () { parse: { mediaType: mediaTypes.latest('json') }, }); } catch (e) { - assert.instanceOf(e, ResolverError); + assert.instanceOf(e, ResolveError); } }); }); @@ -147,7 +147,7 @@ describe('resolve', function () { parse: { mediaType: mediaTypes.latest('json') }, }); } catch (e) { - assert.instanceOf(e, ResolverError); + assert.instanceOf(e, ResolveError); } }); }); @@ -162,7 +162,7 @@ describe('resolve', function () { parse: { mediaType: mediaTypes.latest('json') }, }); } catch (e) { - assert.instanceOf(e, ResolverError); + assert.instanceOf(e, ResolveError); } }); }); @@ -177,7 +177,7 @@ describe('resolve', function () { parse: { mediaType: mediaTypes.latest('json') }, }); } catch (e) { - assert.instanceOf(e, ResolverError); + assert.instanceOf(e, ResolveError); } }); }); @@ -208,7 +208,7 @@ describe('resolve', function () { }); assert.fail('should throw MaximumDereferenceDepthError'); } catch (error: any) { - assert.instanceOf(error, ResolverError); + assert.instanceOf(error, ResolveError); assert.instanceOf(error.cause.cause, MaximumDereferenceDepthError); assert.match(error.cause.cause.message, /fixtures\/max-depth\/ex2.json"$/); } @@ -228,7 +228,7 @@ describe('resolve', function () { }); assert.fail('should throw MaximumResolverDepthError'); } catch (error: any) { - assert.instanceOf(error, ResolverError); + assert.instanceOf(error, ResolveError); assert.instanceOf(error.cause.cause, MaximumResolverDepthError); assert.match(error.cause.cause.message, /fixtures\/max-depth\/ex2.json"$/); } diff --git a/packages/apidom-reference/test/resolve/strategies/openapi-3-1/schema-object/index.ts b/packages/apidom-reference/test/resolve/strategies/openapi-3-1/schema-object/index.ts index 3ef21ab9f4..a28cdad329 100644 --- a/packages/apidom-reference/test/resolve/strategies/openapi-3-1/schema-object/index.ts +++ b/packages/apidom-reference/test/resolve/strategies/openapi-3-1/schema-object/index.ts @@ -5,6 +5,7 @@ import { mediaTypes } from '@swagger-api/apidom-ns-openapi-3-1'; import { resolve } from '../../../../../src'; import MaximumDereferenceDepthError from '../../../../../src/errors/MaximumDereferenceDepthError'; import MaximumResolverDepthError from '../../../../../src/errors/MaximumResolverDepthError'; +import ResolveError from '../../../../../src/errors/ResolveError'; import ResolverError from '../../../../../src/errors/ResolverError'; import EvaluationJsonSchema$anchorError from '../../../../../src/errors/EvaluationJsonSchema$anchorError'; @@ -252,10 +253,10 @@ describe('resolve', function () { await resolve(rootFilePath, { parse: { mediaType: mediaTypes.latest('json') }, }); - assert.fail('should throw ResolverError'); + assert.fail('should throw ResolveError'); } catch (error: any) { - assert.instanceOf(error, ResolverError); - assert.instanceOf(error.cause.cause, ResolverError); + assert.instanceOf(error, ResolveError); + assert.instanceOf(error.cause.cause, ResolveError); assert.match(error.cause.cause.message, /\/schemas\/nested\/ex\.json"$/); } }); @@ -542,9 +543,9 @@ describe('resolve', function () { await resolve(rootFilePath, { parse: { mediaType: mediaTypes.latest('json') }, }); - assert.fail('should throw ResolverError'); + assert.fail('should throw ResolveError'); } catch (e) { - assert.instanceOf(e, ResolverError); + assert.instanceOf(e, ResolveError); } }); });