Skip to content

Commit

Permalink
Improve missing pointer error message (#360)
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Hudlow <[email protected]>
  • Loading branch information
hudlow authored Dec 5, 2024
1 parent 588197e commit 782cbf7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class Pointer<S extends object = JSONSchema, O extends ParserOptions<S> = Parser
}

if (split[0] !== "") {
throw new InvalidPointerError(split, originalPath === undefined ? path : originalPath);
throw new InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
}

return split.slice(1);
Expand Down
10 changes: 5 additions & 5 deletions lib/util/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ono } from "@jsdevtools/ono";
import { stripHash, toFileSystemPath } from "./url.js";
import { getHash, stripHash, toFileSystemPath } from "./url.js";
import type $RefParser from "../index.js";
import type { ParserOptions } from "../index.js";
import type { JSONSchema } from "../index.js";
Expand Down Expand Up @@ -121,10 +121,10 @@ export class UnmatchedResolverError extends JSONParserError {
}

export class MissingPointerError extends JSONParserError {
code = "EUNMATCHEDRESOLVER" as JSONParserErrorType;
code = "EMISSINGPOINTER" as JSONParserErrorType;
name = "MissingPointerError";
constructor(token: any, path: any) {
super(`Token "${token}" does not exist.`, stripHash(path));
constructor(token: string, path: string) {
super(`Missing $ref pointer "${getHash(path)}". Token "${token}" does not exist.`, stripHash(path));
}
}

Expand All @@ -139,7 +139,7 @@ export class TimeoutError extends JSONParserError {
export class InvalidPointerError extends JSONParserError {
code = "EUNMATCHEDRESOLVER" as JSONParserErrorType;
name = "InvalidPointerError";
constructor(pointer: any, path: any) {
constructor(pointer: string, path: string) {
super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, stripHash(path));
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/specs/missing-pointers/missing-pointers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("Schema with missing pointers", () => {
} catch (err) {
expect(err).to.be.an.instanceOf(MissingPointerError);
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.message).to.contain('Token "external" does not exist.');
expect(err.message).to.contain('Missing $ref pointer "#/external". Token "external" does not exist.');
}
});

Expand All @@ -48,7 +48,7 @@ describe("Schema with missing pointers", () => {
expect(err.errors).to.containSubset([
{
name: MissingPointerError.name,
message: 'Token "baz" does not exist.',
message: 'Missing $ref pointer "#/baz". Token "baz" does not exist.',
path: ["foo"],
// source: message => message.endsWith("/test/") || message.startsWith("http://localhost"),
},
Expand Down Expand Up @@ -81,7 +81,7 @@ describe("Schema with missing pointers", () => {
expect(err.errors).to.containSubset([
{
name: MissingPointerError.name,
message: 'Token "external" does not exist.',
message: 'Missing $ref pointer "#/external". 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 @@ -233,7 +233,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.equal('Missing $ref pointer "#/". Token "" does not exist.');
}
});

Expand Down Expand Up @@ -271,7 +271,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.equal('Missing $ref pointer "#/foo/bar". Token "foo" does not exist.');
}
});
});
Expand Down

0 comments on commit 782cbf7

Please sign in to comment.