Skip to content

Commit

Permalink
refactor(reference): consolidate error hierarchies (#3487)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Dec 1, 2023
1 parent a327752 commit e21794c
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ResolverError from './ResolverError';
import ResolveError from './ResolveError';

class MaximumResolverDepthError extends ResolverError {}
class MaximumResolverDepthError extends ResolveError {}

export default MaximumResolverDepthError;
5 changes: 5 additions & 0 deletions packages/apidom-reference/src/errors/ParseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ApiDOMError } from '@swagger-api/apidom-error';

class ParseError extends ApiDOMError {}

export default ParseError;
4 changes: 2 additions & 2 deletions packages/apidom-reference/src/errors/ParserError.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions packages/apidom-reference/src/errors/ResolveError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ApiDOMError } from '@swagger-api/apidom-error';

class ResolveError extends ApiDOMError {}

export default ResolveError;
4 changes: 2 additions & 2 deletions packages/apidom-reference/src/errors/ResolverError.ts
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ParserError from './ParserError';
import DereferenceError from './DereferenceError';

class UnmatchedDereferenceStrategyError extends ParserError {}
class UnmatchedDereferenceStrategyError extends DereferenceError {}

export default UnmatchedDereferenceStrategyError;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ParserError from './ParserError';
import ResolveError from './ResolveError';

class UnmatchedResolveStrategyError extends ParserError {}
class UnmatchedResolveStrategyError extends ResolveError {}

export default UnmatchedResolveStrategyError;
2 changes: 2 additions & 0 deletions packages/apidom-reference/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
6 changes: 3 additions & 3 deletions packages/apidom-reference/src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -31,13 +31,13 @@ const parseFile = async (file: IFile, options: IReferenceOptions): Promise<Parse
// empty files handling
if (!plugin.allowEmpty && result.isEmpty) {
return Promise.reject(
new ParserError(`Error while parsing file "${file.uri}". File is empty.`),
new ParseError(`Error while parsing file "${file.uri}". File is empty.`),
);
}

return result;
} catch (error: any) {
throw new ParserError(`Error while parsing file "${file.uri}"`, { cause: error });
throw new ParseError(`Error while parsing file "${file.uri}"`, { cause: error });
}
};

Expand Down
4 changes: 2 additions & 2 deletions packages/apidom-reference/src/resolve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ReferenceOptions as IReferenceOptions, ReferenceSet as IReferenceSet }
import parse from '../parse';
import * as plugins from '../util/plugins';
import File from '../util/File';
import ResolverError from '../errors/ResolverError';
import ResolveError from '../errors/ResolverError';
import UnmatchedResolveStrategyError from '../errors/UnmatchedResolveStrategyError';
import * as url from '../util/url';

Expand Down Expand Up @@ -51,7 +51,7 @@ export const resolveApiDOM = async <T extends Element>(
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 });
}
};

Expand Down
4 changes: 2 additions & 2 deletions packages/apidom-reference/src/resolve/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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 });
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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"$/);
}
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down
14 changes: 7 additions & 7 deletions packages/apidom-reference/test/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
}
});
});
Expand All @@ -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\.$/);
}
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down
16 changes: 8 additions & 8 deletions packages/apidom-reference/test/resolve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
});
});
Expand Down Expand Up @@ -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\.$/);
}
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -102,7 +102,7 @@ describe('resolve', function () {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, ResolverError);
assert.instanceOf(e, ResolveError);
}
});
});
Expand All @@ -117,7 +117,7 @@ describe('resolve', function () {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, ResolverError);
assert.instanceOf(e, ResolveError);
}
});
});
Expand All @@ -132,7 +132,7 @@ describe('resolve', function () {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, ResolverError);
assert.instanceOf(e, ResolveError);
}
});
});
Expand All @@ -147,7 +147,7 @@ describe('resolve', function () {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, ResolverError);
assert.instanceOf(e, ResolveError);
}
});
});
Expand All @@ -162,7 +162,7 @@ describe('resolve', function () {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, ResolverError);
assert.instanceOf(e, ResolveError);
}
});
});
Expand All @@ -177,7 +177,7 @@ describe('resolve', function () {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, ResolverError);
assert.instanceOf(e, ResolveError);
}
});
});
Expand Down Expand Up @@ -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"$/);
}
Expand All @@ -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"$/);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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"$/);
}
});
Expand Down Expand Up @@ -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);
}
});
});
Expand Down

0 comments on commit e21794c

Please sign in to comment.