Skip to content

Commit

Permalink
Merge pull request #43 from stoplightio/improve-MissingPointerError-m…
Browse files Browse the repository at this point in the history
…essage

improve MissingPointerError message
  • Loading branch information
ksoviero-stoplight authored May 31, 2023
2 parents 5c9b100 + 70e087e commit fc3f606
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 26 deletions.
33 changes: 22 additions & 11 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,34 @@
// ${cwd}: the current working directory of the spawned process

{
"version": "0.1.0",
"version": "2.0.0",
"command": "npm",
"isShellCommand": true,
"suppressTaskName": true,
"tasks": [
{
"taskName": "lint",
"args": ["run", "lint"],
"showOutput": "always",
"label": "lint",
"type": "shell",
"args": [
"run",
"lint"
],
"problemMatcher": "$eslint-stylish",
"isBuildCommand": true
"group": {
"_id": "build",
"isDefault": false
}
},
{
"taskName": "test",
"args": ["run", "mocha"],
"showOutput": "always",
"isTestCommand": true
"label": "test",
"type": "shell",
"args": [
"run",
"mocha"
],
"problemMatcher": [],
"group": {
"_id": "test",
"isDefault": false
}
}
]
}
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ declare namespace $RefParser {
public readonly code ="EUNMATCHEDRESOLVER";
}
export class MissingPointerError extends JSONParserError {
public constructor(token: string | number, source: string);
public constructor(token: string | number, source: string, unresolvableRefValue?: string, pathToUnresolvableRef?: string);

public readonly name = "MissingPointerError";
public readonly code ="EMISSINGPOINTER";
Expand Down
2 changes: 1 addition & 1 deletion lib/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Pointer.prototype.resolve = function (obj, options, pathFromRoot) {
let token = tokens[i];
if (this.value[token] === undefined || this.value[token] === null) {
this.value = null;
throw new MissingPointerError(token, this.originalPath);
throw new MissingPointerError(token, this.originalPath, this.path, pathFromRoot);
}
else {
this.value = this.value[token];
Expand Down
6 changes: 3 additions & 3 deletions lib/util/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Ono } = require("@jsdevtools/ono");

const { stripHash, toFileSystemPath } = require("./url");
const { getHash, stripHash, toFileSystemPath } = require("./url");

const JSONParserError = exports.JSONParserError = class JSONParserError extends Error {
constructor (message, source) {
Expand Down Expand Up @@ -93,8 +93,8 @@ const UnmatchedResolverError = exports.UnmatchedResolverError = class UnmatchedR
setErrorName(UnmatchedResolverError);

const MissingPointerError = exports.MissingPointerError = class MissingPointerError extends JSONParserError {
constructor (token, path) {
super(`Token "${token}" does not exist.`, stripHash(path));
constructor (unresolvableTokenInRef, path, unresolvableRefValue, pathToUnresolvableRef) {
super(`at "${getHash(pathToUnresolvableRef)}", token "${unresolvableTokenInRef}" in "${getHash(unresolvableRefValue)}" does not exist`, stripHash(path));

this.code = "EMISSINGPOINTER";
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/json-schema-ref-parser",
"version": "9.2.4",
"version": "9.2.5",
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
"keywords": [
"json",
Expand Down
6 changes: 4 additions & 2 deletions test/specs/invalid/invalid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ describe("Invalid syntax", () => {
message.includes("invalid.json: JSON.parse: end of data while reading object contents") || // Firefox
message.includes("invalid.json: JSON Parse error: Expected '}'") || // Safari
message.includes("invalid.json: JSON.parse Error: Invalid character") || // Edge
message.includes("invalid.json: Syntax error") // IE
message.includes("invalid.json: Syntax error") || // IE
message.includes("Expected property name or \'}\' in JSON at position 2")
),
path: [],
source: message => message.endsWith("test/specs/invalid/invalid.json"),
Expand Down Expand Up @@ -318,7 +319,8 @@ describe("Invalid syntax", () => {
message.includes("invalid.json: JSON.parse: end of data while reading object contents") || // Firefox
message.includes("invalid.json: JSON Parse error: Expected '}'") || // Safari
message.includes("invalid.json: JSON.parse Error: Invalid character") || // Edge
message.includes("invalid.json: Syntax error") // IE
message.includes("invalid.json: Syntax error") || // IE
message.includes("Expected property name or \'}\' in JSON at position 2") // Chrome
),
path: ["foo"],
source: message => message.endsWith("/test/") || message.startsWith("http://localhost"),
Expand Down
7 changes: 4 additions & 3 deletions test/specs/missing-pointers/missing-pointers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Schema with missing pointers", () => {
}
catch (err) {
expect(err).to.be.an.instanceOf(MissingPointerError);
expect(err.message).to.contain("Token \"baz\" does not exist.");
expect(err.message).to.equal('at "#/foo", token "baz" in "#/baz" does not exist');
}
});

Expand All @@ -31,12 +31,13 @@ describe("Schema with missing pointers", () => {
expect(err.files).to.equal(parser);
expect(err.files.$refs._root$Ref.value).to.deep.equal({ foo: null });
expect(err.message).to.have.string("1 error occurred while reading '");
console.log(err.errors[0].source);

expect(err.errors).to.containSubset([
{
name: MissingPointerError.name,
message: "Token \"baz\" does not exist.",
message: 'at "#/foo", token "baz" in "#/baz" does not exist',
path: ["foo"],
source: message => message.endsWith("/test/") || message.startsWith("http://localhost"),
}
]);
}
Expand Down
4 changes: 2 additions & 2 deletions test/specs/refs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe("$Refs object", () => {
}
catch (err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('Token "" does not exist.');
expect(err.message).to.equal('at "#", token "" in "#/" does not exist');
}
});

Expand Down Expand Up @@ -254,7 +254,7 @@ describe("$Refs object", () => {
}
catch (err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('Token "foo" does not exist.');
expect(err.message).to.equal('at "#", token "foo" in "#/foo/bar" does not exist');
}
});
});
Expand Down

0 comments on commit fc3f606

Please sign in to comment.