Skip to content

Commit

Permalink
refactor(reference): base all errors on apidom-error package (#3220)
Browse files Browse the repository at this point in the history
Refs #3039
  • Loading branch information
char0n authored Oct 4, 2023
1 parent c7b7923 commit 2c35e4e
Show file tree
Hide file tree
Showing 87 changed files with 220 additions and 201 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions packages/apidom-core/src/media-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ class MediaTypes<T> extends Array<T> {

// eslint-disable-next-line class-methods-use-this
filterByFormat() {
throw new NotImplementedError('"filterByFormat" method is not implemented.');
throw new NotImplementedError(
'filterByFormat method in MediaTypes class is not yet implemented.',
);
}

// eslint-disable-next-line class-methods-use-this
findBy() {
throw new NotImplementedError('"findBy" method is not implemented.');
throw new NotImplementedError('findBy method in MediaTypes class is not yet implemented.');
}

// eslint-disable-next-line class-methods-use-this
latest() {
throw new NotImplementedError('"latest" method is not implemented.');
throw new NotImplementedError('latest method in MediaTypes class is not yet implemented.');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ const makeMessage = (
return `"${primitiveValue}" not allowed for subject ${primitiveStandardIdentifier}`;
}

throw new NotImplementedError(
'[Requirement levels] other than "may" are currently not implemented.',
);
throw new NotImplementedError('[Requirement levels] other than "may" are not yet implemented.');
};

// @ts-ignore
Expand Down
12 changes: 6 additions & 6 deletions packages/apidom-reference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@
"types": "./types/dereference/strategies/openapi-3-1/index.d.ts"
},
"./dereference/strategies/openapi-3-1/selectors/$anchor": {
"import": "./es/dereference/strategies/openapi-3-1/selectors/$anchor/index.mjs",
"require": "./cjs/dereference/strategies/openapi-3-1/selectors/$anchor/index.cjs",
"types": "./types/dereference/strategies/openapi-3-1/selectors/$anchor/index.d.ts"
"import": "./es/dereference/strategies/openapi-3-1/selectors/$anchor.mjs",
"require": "./cjs/dereference/strategies/openapi-3-1/selectors/$anchor.cjs",
"types": "./types/dereference/strategies/openapi-3-1/selectors/$anchor.d.ts"
},
"./dereference/strategies/openapi-3-1/selectors/uri": {
"import": "./es/dereference/strategies/openapi-3-1/selectors/uri/index.mjs",
"require": "./cjs/dereference/strategies/openapi-3-1/selectors/uri/index.cjs",
"types": "./types/dereference/strategies/openapi-3-1/selectors/uri/index.d.ts"
"import": "./es/dereference/strategies/openapi-3-1/selectors/uri.mjs",
"require": "./cjs/dereference/strategies/openapi-3-1/selectors/uri.cjs",
"types": "./types/dereference/strategies/openapi-3-1/selectors/uri.d.ts"
}
},
"imports": {
Expand Down
4 changes: 2 additions & 2 deletions packages/apidom-reference/src/dereference/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {

import File from '../util/File';
import * as plugins from '../util/plugins';
import { UnmatchedDereferenceStrategyError } from '../util/errors';
import DereferenceError from '../util/errors/DereferenceError';
import UnmatchedDereferenceStrategyError from '../errors/UnmatchedDereferenceStrategyError';
import DereferenceError from '../errors/DereferenceError';
import { ReferenceOptions as IReferenceOptions } from '../types';
import parse from '../parse';
import { merge as mergeOptions } from '../options/util';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stampit from 'stampit';
import { NotImplementedError } from '@swagger-api/apidom-error';

import { DereferenceStrategy as IDereferenceStrategy } from '../../types';
import { NotImplementedError } from '../../util/errors';

const DereferenceStrategy: stampit.Stamp<IDereferenceStrategy> = stampit({
props: {
Expand All @@ -13,7 +13,9 @@ const DereferenceStrategy: stampit.Stamp<IDereferenceStrategy> = stampit({
},

async dereference(): Promise<never> {
throw new NotImplementedError();
throw new NotImplementedError(
'dereference method in DereferenceStrategy stamp is not yet implemented.',
);
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
visit,
toValue,
} from '@swagger-api/apidom-core';
import { ApiDOMError } from '@swagger-api/apidom-error';
import { evaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
import {
ChannelItemElement,
Expand All @@ -23,7 +24,8 @@ import {
} from '@swagger-api/apidom-ns-asyncapi-2';

import { Reference as IReference } from '../../../types';
import { MaximumDereferenceDepthError, MaximumResolverDepthError } from '../../../util/errors';
import MaximumDereferenceDepthError from '../../../errors/MaximumDereferenceDepthError';
import MaximumResolverDepthError from '../../../errors/MaximumResolverDepthError';
import { AncestorLineage } from '../../util';
import * as url from '../../../util/url';
import parse from '../../../parse';
Expand Down Expand Up @@ -141,7 +143,7 @@ const AsyncApi2DereferenceVisitor = stampit({

// detect direct or circular reference
if (this.indirections.includes(referencedElement)) {
throw new Error('Recursive Reference Object detected');
throw new ApiDOMError('Recursive Reference Object detected');
}

// detect maximum depth of dereferencing
Expand Down Expand Up @@ -242,7 +244,7 @@ const AsyncApi2DereferenceVisitor = stampit({

// detect direct or indirect reference
if (this.indirections.includes(referencedElement)) {
throw new Error('Recursive Channel Item Object reference detected');
throw new ApiDOMError('Recursive Channel Item Object reference detected');
}

// detect maximum depth of dereferencing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
toValue,
isMemberElement,
} from '@swagger-api/apidom-core';
import { ApiDOMError } from '@swagger-api/apidom-error';
import { evaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
import {
getNodeType,
Expand All @@ -30,7 +31,8 @@ import {
} from '@swagger-api/apidom-ns-openapi-3-0';

import { Reference as IReference } from '../../../types';
import { MaximumDereferenceDepthError, MaximumResolverDepthError } from '../../../util/errors';
import MaximumDereferenceDepthError from '../../../errors/MaximumDereferenceDepthError';
import MaximumResolverDepthError from '../../../errors/MaximumResolverDepthError';
import { AncestorLineage } from '../../util';
import * as url from '../../../util/url';
import parse from '../../../parse';
Expand Down Expand Up @@ -149,7 +151,7 @@ const OpenApi3_0DereferenceVisitor = stampit({

// detect direct or circular reference
if (this.indirections.includes(referencedElement)) {
throw new Error('Recursive Reference Object detected');
throw new ApiDOMError('Recursive Reference Object detected');
}

// detect maximum depth of dereferencing
Expand Down Expand Up @@ -251,7 +253,7 @@ const OpenApi3_0DereferenceVisitor = stampit({

// detect direct or indirect reference
if (this.indirections.includes(referencedElement)) {
throw new Error('Recursive Path Item Object reference detected');
throw new ApiDOMError('Recursive Path Item Object reference detected');
}

// detect maximum depth of dereferencing
Expand Down Expand Up @@ -336,7 +338,9 @@ const OpenApi3_0DereferenceVisitor = stampit({

// operationRef and operationId fields are mutually exclusive
if (isStringElement(linkElement.operationRef) && isStringElement(linkElement.operationId)) {
throw new Error('LinkElement operationRef and operationId fields are mutually exclusive.');
throw new ApiDOMError(
'LinkElement operationRef and operationId fields are mutually exclusive.',
);
}

let operationElement: any;
Expand Down Expand Up @@ -369,7 +373,7 @@ const OpenApi3_0DereferenceVisitor = stampit({
);
// OperationElement not found by its operationId
if (isUndefined(operationElement)) {
throw new Error(`OperationElement(operationId=${operationId}) not found.`);
throw new ApiDOMError(`OperationElement(operationId=${operationId}) not found.`);
}

const linkElementCopy = cloneShallow(linkElement);
Expand Down Expand Up @@ -406,7 +410,9 @@ const OpenApi3_0DereferenceVisitor = stampit({

// value and externalValue fields are mutually exclusive
if (exampleElement.hasKey('value') && isStringElement(exampleElement.externalValue)) {
throw new Error('ExampleElement value and externalValue fields are mutually exclusive.');
throw new ApiDOMError(
'ExampleElement value and externalValue fields are mutually exclusive.',
);
}

const reference = await this.toReference(toValue(exampleElement.externalValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { trimCharsStart, isUndefined } from 'ramda-adjunct';
import { Element, find, toValue } from '@swagger-api/apidom-core';
import { isSchemaElement } from '@swagger-api/apidom-ns-openapi-3-1';

import { getHash } from '../../../../../util/url';
import { EvaluationJsonSchema$anchorError, InvalidJsonSchema$anchorError } from './errors';
import { getHash } from '../../../../util/url';
import EvaluationJsonSchema$anchorError from '../../../../errors/EvaluationJsonSchema$anchorError';
import InvalidJsonSchema$anchorError from '../../../../errors/InvalidJsonSchema$anchorError';

// isAnchor :: String -> Boolean
export const isAnchor = (uri: string) => {
Expand Down Expand Up @@ -47,3 +48,4 @@ export const evaluate = <T extends Element>(anchor: string, element: T): Element
};

export { EvaluationJsonSchema$anchorError, InvalidJsonSchema$anchorError };
export { default as JsonSchema$anchorError } from '../../../../errors/JsonSchema$anchorError';

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Element, filter } from '@swagger-api/apidom-core';
import { isSchemaElement, SchemaElement } from '@swagger-api/apidom-ns-openapi-3-1';
import { uriToPointer, evaluate as jsonPointerEvaluate } from '@swagger-api/apidom-json-pointer';

import * as url from '../../../../../util/url';
import { EvaluationJsonSchemaUriError } from './errors';
import { isAnchor, uriToAnchor, evaluate as $anchorEvaluate } from '../$anchor';
import { resolveSchema$idField } from '../../../../../resolve/strategies/openapi-3-1/util';
import * as url from '../../../../util/url';
import EvaluationJsonSchemaUriError from '../../../../errors/EvaluationJsonSchemaUriError';
import { isAnchor, uriToAnchor, evaluate as $anchorEvaluate } from './$anchor';
import { resolveSchema$idField } from '../../../../resolve/strategies/openapi-3-1/util';

/**
* Evaluates JSON Schema $ref containing unknown URI against ApiDOM fragment.
Expand Down Expand Up @@ -50,3 +50,4 @@ export const evaluate = <T extends Element>(uri: string, element: T): Element |
evaluate.cache = new WeakMap();

export { EvaluationJsonSchemaUriError };
export { default as JsonSchemaUriError } from '../../../../errors/JsonSchemaUriError';

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
toValue,
Element,
} from '@swagger-api/apidom-core';
import { ApiDOMError } from '@swagger-api/apidom-error';
import { evaluate as jsonPointerEvaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
import {
getNodeType,
Expand All @@ -35,7 +36,8 @@ import {
import { isAnchor, uriToAnchor, evaluate as $anchorEvaluate } from './selectors/$anchor';
import { evaluate as uriEvaluate } from './selectors/uri';
import { Reference as IReference, Resolver as IResolver } from '../../../types';
import { MaximumDereferenceDepthError, MaximumResolverDepthError } from '../../../util/errors';
import MaximumDereferenceDepthError from '../../../errors/MaximumDereferenceDepthError';
import MaximumResolverDepthError from '../../../errors/MaximumResolverDepthError';
import * as url from '../../../util/url';
import parse from '../../../parse';
import Reference from '../../../Reference';
Expand All @@ -45,7 +47,7 @@ import {
maybeRefractToSchemaElement,
} from '../../../resolve/strategies/openapi-3-1/util';
import { AncestorLineage } from '../../util';
import EvaluationJsonSchemaUriError from './selectors/uri/errors/EvaluationJsonSchemaUriError';
import EvaluationJsonSchemaUriError from '../../../errors/EvaluationJsonSchemaUriError';

// @ts-ignore
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];
Expand Down Expand Up @@ -162,7 +164,7 @@ const OpenApi3_1DereferenceVisitor = stampit({

// detect direct or indirect reference
if (this.indirections.includes(referencedElement)) {
throw new Error('Recursive Reference Object detected');
throw new ApiDOMError('Recursive Reference Object detected');
}

// detect maximum depth of dereferencing
Expand Down Expand Up @@ -283,7 +285,7 @@ const OpenApi3_1DereferenceVisitor = stampit({

// detect direct or indirect reference
if (this.indirections.includes(referencedElement)) {
throw new Error('Recursive Path Item Object reference detected');
throw new ApiDOMError('Recursive Path Item Object reference detected');
}

// detect maximum depth of dereferencing
Expand Down Expand Up @@ -368,7 +370,9 @@ const OpenApi3_1DereferenceVisitor = stampit({

// operationRef and operationId fields are mutually exclusive
if (isStringElement(linkElement.operationRef) && isStringElement(linkElement.operationId)) {
throw new Error('LinkElement operationRef and operationId fields are mutually exclusive.');
throw new ApiDOMError(
'LinkElement operationRef and operationId fields are mutually exclusive.',
);
}

let operationElement: any;
Expand Down Expand Up @@ -401,7 +405,7 @@ const OpenApi3_1DereferenceVisitor = stampit({
);
// OperationElement not found by its operationId
if (isUndefined(operationElement)) {
throw new Error(`OperationElement(operationId=${operationId}) not found.`);
throw new ApiDOMError(`OperationElement(operationId=${operationId}) not found.`);
}

const linkElementCopy = cloneShallow(linkElement);
Expand Down Expand Up @@ -438,7 +442,9 @@ const OpenApi3_1DereferenceVisitor = stampit({

// value and externalValue fields are mutually exclusive
if (exampleElement.hasKey('value') && isStringElement(exampleElement.externalValue)) {
throw new Error('ExampleElement value and externalValue fields are mutually exclusive.');
throw new ApiDOMError(
'ExampleElement value and externalValue fields are mutually exclusive.',
);
}

const reference = await this.toReference(toValue(exampleElement.externalValue));
Expand Down Expand Up @@ -545,7 +551,7 @@ const OpenApi3_1DereferenceVisitor = stampit({

// detect direct or indirect reference
if (this.indirections.includes(referencedElement)) {
throw new Error('Recursive Schema Object reference detected');
throw new ApiDOMError('Recursive Schema Object reference detected');
}

// detect maximum depth of dereferencing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import JsonSchema$anchorError from './JsonSchema$anchorError';

class EvaluationJsonSchema$anchorError extends JsonSchema$anchorError {}

export default EvaluationJsonSchema$anchorError;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import JsonSchemaUriError from './JsonSchemaUriError';

class EvaluationJsonSchemaUriError extends JsonSchemaUriError {}

export default EvaluationJsonSchemaUriError;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import JsonSchema$anchorError from './JsonSchema$anchorError';

class InvalidJsonSchema$anchorError extends JsonSchema$anchorError {
constructor(anchor: string) {
super(`Invalid JSON Schema $anchor "${anchor}".`);
}
}

export default InvalidJsonSchema$anchorError;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ApiDOMError } from '@swagger-api/apidom-error';

class JsonSchema$anchorError extends ApiDOMError {}

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

class JsonSchemaUriError extends ApiDOMError {}

export default JsonSchemaUriError;
Loading

0 comments on commit 2c35e4e

Please sign in to comment.