From 782cbf7295700a852f443255a7af8617d06780a5 Mon Sep 17 00:00:00 2001 From: Dan Hudlow Date: Thu, 5 Dec 2024 17:00:17 -0600 Subject: [PATCH] Improve missing pointer error message (#360) Signed-off-by: Dan Hudlow --- lib/pointer.ts | 2 +- lib/util/errors.ts | 10 +++++----- test/specs/missing-pointers/missing-pointers.spec.ts | 6 +++--- test/specs/refs.spec.ts | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/pointer.ts b/lib/pointer.ts index f851590b..c86ac67c 100644 --- a/lib/pointer.ts +++ b/lib/pointer.ts @@ -210,7 +210,7 @@ class Pointer = Parser } if (split[0] !== "") { - throw new InvalidPointerError(split, originalPath === undefined ? path : originalPath); + throw new InvalidPointerError(pointer, originalPath === undefined ? path : originalPath); } return split.slice(1); diff --git a/lib/util/errors.ts b/lib/util/errors.ts index 85ed8212..256c8a1c 100644 --- a/lib/util/errors.ts +++ b/lib/util/errors.ts @@ -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"; @@ -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)); } } @@ -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)); } } diff --git a/test/specs/missing-pointers/missing-pointers.spec.ts b/test/specs/missing-pointers/missing-pointers.spec.ts index 12b8dd9c..f3bc5156 100644 --- a/test/specs/missing-pointers/missing-pointers.spec.ts +++ b/test/specs/missing-pointers/missing-pointers.spec.ts @@ -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.'); } }); @@ -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"), }, @@ -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") || diff --git a/test/specs/refs.spec.ts b/test/specs/refs.spec.ts index 7817a5b9..25246565 100644 --- a/test/specs/refs.spec.ts +++ b/test/specs/refs.spec.ts @@ -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.'); } }); @@ -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.'); } }); });