Skip to content

Commit 782cbf7

Browse files
authored
Improve missing pointer error message (#360)
Signed-off-by: Dan Hudlow <dhudlow@us.ibm.com>
1 parent 588197e commit 782cbf7

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

lib/pointer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class Pointer<S extends object = JSONSchema, O extends ParserOptions<S> = Parser
210210
}
211211

212212
if (split[0] !== "") {
213-
throw new InvalidPointerError(split, originalPath === undefined ? path : originalPath);
213+
throw new InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
214214
}
215215

216216
return split.slice(1);

lib/util/errors.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Ono } from "@jsdevtools/ono";
2-
import { stripHash, toFileSystemPath } from "./url.js";
2+
import { getHash, stripHash, toFileSystemPath } from "./url.js";
33
import type $RefParser from "../index.js";
44
import type { ParserOptions } from "../index.js";
55
import type { JSONSchema } from "../index.js";
@@ -121,10 +121,10 @@ export class UnmatchedResolverError extends JSONParserError {
121121
}
122122

123123
export class MissingPointerError extends JSONParserError {
124-
code = "EUNMATCHEDRESOLVER" as JSONParserErrorType;
124+
code = "EMISSINGPOINTER" as JSONParserErrorType;
125125
name = "MissingPointerError";
126-
constructor(token: any, path: any) {
127-
super(`Token "${token}" does not exist.`, stripHash(path));
126+
constructor(token: string, path: string) {
127+
super(`Missing $ref pointer "${getHash(path)}". Token "${token}" does not exist.`, stripHash(path));
128128
}
129129
}
130130

@@ -139,7 +139,7 @@ export class TimeoutError extends JSONParserError {
139139
export class InvalidPointerError extends JSONParserError {
140140
code = "EUNMATCHEDRESOLVER" as JSONParserErrorType;
141141
name = "InvalidPointerError";
142-
constructor(pointer: any, path: any) {
142+
constructor(pointer: string, path: string) {
143143
super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, stripHash(path));
144144
}
145145
}

test/specs/missing-pointers/missing-pointers.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("Schema with missing pointers", () => {
2626
} catch (err) {
2727
expect(err).to.be.an.instanceOf(MissingPointerError);
2828
// @ts-expect-error TS(2571): Object is of type 'unknown'.
29-
expect(err.message).to.contain('Token "external" does not exist.');
29+
expect(err.message).to.contain('Missing $ref pointer "#/external". Token "external" does not exist.');
3030
}
3131
});
3232

@@ -48,7 +48,7 @@ describe("Schema with missing pointers", () => {
4848
expect(err.errors).to.containSubset([
4949
{
5050
name: MissingPointerError.name,
51-
message: 'Token "baz" does not exist.',
51+
message: 'Missing $ref pointer "#/baz". Token "baz" does not exist.',
5252
path: ["foo"],
5353
// source: message => message.endsWith("/test/") || message.startsWith("http://localhost"),
5454
},
@@ -81,7 +81,7 @@ describe("Schema with missing pointers", () => {
8181
expect(err.errors).to.containSubset([
8282
{
8383
name: MissingPointerError.name,
84-
message: 'Token "external" does not exist.',
84+
message: 'Missing $ref pointer "#/external". Token "external" does not exist.',
8585
path: ["internal2"],
8686
source: (message: any) =>
8787
message.endsWith("missing-pointers/external-from-internal.yaml") ||

test/specs/refs.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ describe("$Refs object", () => {
233233
} catch (err) {
234234
expect(err).to.be.an.instanceOf(Error);
235235
// @ts-expect-error TS(2571): Object is of type 'unknown'.
236-
expect(err.message).to.equal('Token "" does not exist.');
236+
expect(err.message).to.equal('Missing $ref pointer "#/". Token "" does not exist.');
237237
}
238238
});
239239

@@ -271,7 +271,7 @@ describe("$Refs object", () => {
271271
} catch (err) {
272272
expect(err).to.be.an.instanceOf(Error);
273273
// @ts-expect-error TS(2571): Object is of type 'unknown'.
274-
expect(err.message).to.equal('Token "foo" does not exist.');
274+
expect(err.message).to.equal('Missing $ref pointer "#/foo/bar". Token "foo" does not exist.');
275275
}
276276
});
277277
});

0 commit comments

Comments
 (0)