Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ class Pointer {
const token = tokens[i];
if (this.value[token] === undefined || this.value[token] === null) {
this.value = null;
throw new MissingPointerError(token, decodeURI(this.originalPath));
throw new MissingPointerError(
token,
decodeURI(this.originalPath),
`In the following schema: ${JSON.stringify(this.value, null, 2)}`,
);
} else {
this.value = this.value[token];
}
Expand Down
13 changes: 9 additions & 4 deletions lib/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,21 @@ export class UnmatchedResolverError extends JSONParserError {
export class MissingPointerError extends JSONParserError {
code = "EUNMATCHEDRESOLVER" as JSONParserErrorType;
name = "MissingPointerError";
constructor(token: any, path: any) {
super(`Token "${token}" does not exist.`, stripHash(path));
constructor(token: any, path: any, additionalMessage?: any) {
super(`Token "${token}" does not exist.${additionalMessage ? `\n\n${additionalMessage}\n` : ""}`, stripHash(path));
}
}

export class InvalidPointerError extends JSONParserError {
code = "EUNMATCHEDRESOLVER" as JSONParserErrorType;
name = "InvalidPointerError";
constructor(pointer: any, path: any) {
super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, stripHash(path));
constructor(pointer: any, path: any, additionalMessage?: any) {
super(
`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"${
additionalMessage ? `\n\n${additionalMessage}\n` : ""
}`,
stripHash(path),
);
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/specs/missing-pointers/missing-pointers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ describe("Schema with missing pointers", () => {
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.message).to.have.string("1 error occurred while reading '");
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.errors.map(({ message }) => message).join()).toContain('Token "baz" does not exist.');
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.errors).to.containSubset([
{
name: MissingPointerError.name,
message: 'Token "baz" does not exist.',
path: ["foo"],
// source: message => message.endsWith("/test/") || message.startsWith("http://localhost"),
},
Expand Down Expand Up @@ -78,10 +79,11 @@ describe("Schema with missing pointers", () => {
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.message).to.have.string("1 error occurred while reading '");
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.errors.map(({ message }) => message).join()).toContain('Token "external" does not exist.');
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.errors).to.containSubset([
{
name: MissingPointerError.name,
message: 'Token "external" does not exist.',
path: ["internal2"],
source: (message: any) =>
message.endsWith("missing-pointers/external-from-internal.yaml") ||
Expand Down
4 changes: 2 additions & 2 deletions test/specs/refs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe("$Refs object", () => {
} catch (err) {
expect(err).to.be.an.instanceOf(Error);
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.message).to.equal('Token "" does not exist.');
expect(err.message).to.contain('Token "" does not exist.');
}
});

Expand Down Expand Up @@ -273,7 +273,7 @@ describe("$Refs object", () => {
} catch (err) {
expect(err).to.be.an.instanceOf(Error);
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.message).to.equal('Token "foo" does not exist.');
expect(err.message).to.contain('Token "foo" does not exist.');
}
});
});
Expand Down