Skip to content

Commit fc3f606

Browse files
Merge pull request #43 from stoplightio/improve-MissingPointerError-message
improve MissingPointerError message
2 parents 5c9b100 + 70e087e commit fc3f606

File tree

9 files changed

+40
-26
lines changed

9 files changed

+40
-26
lines changed

.vscode/tasks.json

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,34 @@
77
// ${cwd}: the current working directory of the spawned process
88

99
{
10-
"version": "0.1.0",
10+
"version": "2.0.0",
1111
"command": "npm",
12-
"isShellCommand": true,
13-
"suppressTaskName": true,
1412
"tasks": [
1513
{
16-
"taskName": "lint",
17-
"args": ["run", "lint"],
18-
"showOutput": "always",
14+
"label": "lint",
15+
"type": "shell",
16+
"args": [
17+
"run",
18+
"lint"
19+
],
1920
"problemMatcher": "$eslint-stylish",
20-
"isBuildCommand": true
21+
"group": {
22+
"_id": "build",
23+
"isDefault": false
24+
}
2125
},
2226
{
23-
"taskName": "test",
24-
"args": ["run", "mocha"],
25-
"showOutput": "always",
26-
"isTestCommand": true
27+
"label": "test",
28+
"type": "shell",
29+
"args": [
30+
"run",
31+
"mocha"
32+
],
33+
"problemMatcher": [],
34+
"group": {
35+
"_id": "test",
36+
"isDefault": false
37+
}
2738
}
2839
]
2940
}

lib/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ declare namespace $RefParser {
490490
public readonly code ="EUNMATCHEDRESOLVER";
491491
}
492492
export class MissingPointerError extends JSONParserError {
493-
public constructor(token: string | number, source: string);
493+
public constructor(token: string | number, source: string, unresolvableRefValue?: string, pathToUnresolvableRef?: string);
494494

495495
public readonly name = "MissingPointerError";
496496
public readonly code ="EMISSINGPOINTER";

lib/pointer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Pointer.prototype.resolve = function (obj, options, pathFromRoot) {
9797
let token = tokens[i];
9898
if (this.value[token] === undefined || this.value[token] === null) {
9999
this.value = null;
100-
throw new MissingPointerError(token, this.originalPath);
100+
throw new MissingPointerError(token, this.originalPath, this.path, pathFromRoot);
101101
}
102102
else {
103103
this.value = this.value[token];

lib/util/errors.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

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

77
const JSONParserError = exports.JSONParserError = class JSONParserError extends Error {
88
constructor (message, source) {
@@ -93,8 +93,8 @@ const UnmatchedResolverError = exports.UnmatchedResolverError = class UnmatchedR
9393
setErrorName(UnmatchedResolverError);
9494

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

9999
this.code = "EMISSINGPOINTER";
100100
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stoplight/json-schema-ref-parser",
3-
"version": "9.2.4",
3+
"version": "9.2.5",
44
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
55
"keywords": [
66
"json",

test/specs/invalid/invalid.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ describe("Invalid syntax", () => {
164164
message.includes("invalid.json: JSON.parse: end of data while reading object contents") || // Firefox
165165
message.includes("invalid.json: JSON Parse error: Expected '}'") || // Safari
166166
message.includes("invalid.json: JSON.parse Error: Invalid character") || // Edge
167-
message.includes("invalid.json: Syntax error") // IE
167+
message.includes("invalid.json: Syntax error") || // IE
168+
message.includes("Expected property name or \'}\' in JSON at position 2")
168169
),
169170
path: [],
170171
source: message => message.endsWith("test/specs/invalid/invalid.json"),
@@ -318,7 +319,8 @@ describe("Invalid syntax", () => {
318319
message.includes("invalid.json: JSON.parse: end of data while reading object contents") || // Firefox
319320
message.includes("invalid.json: JSON Parse error: Expected '}'") || // Safari
320321
message.includes("invalid.json: JSON.parse Error: Invalid character") || // Edge
321-
message.includes("invalid.json: Syntax error") // IE
322+
message.includes("invalid.json: Syntax error") || // IE
323+
message.includes("Expected property name or \'}\' in JSON at position 2") // Chrome
322324
),
323325
path: ["foo"],
324326
source: message => message.endsWith("/test/") || message.startsWith("http://localhost"),

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe("Schema with missing pointers", () => {
1616
}
1717
catch (err) {
1818
expect(err).to.be.an.instanceOf(MissingPointerError);
19-
expect(err.message).to.contain("Token \"baz\" does not exist.");
19+
expect(err.message).to.equal('at "#/foo", token "baz" in "#/baz" does not exist');
2020
}
2121
});
2222

@@ -31,12 +31,13 @@ describe("Schema with missing pointers", () => {
3131
expect(err.files).to.equal(parser);
3232
expect(err.files.$refs._root$Ref.value).to.deep.equal({ foo: null });
3333
expect(err.message).to.have.string("1 error occurred while reading '");
34+
console.log(err.errors[0].source);
35+
3436
expect(err.errors).to.containSubset([
3537
{
3638
name: MissingPointerError.name,
37-
message: "Token \"baz\" does not exist.",
39+
message: 'at "#/foo", token "baz" in "#/baz" does not exist',
3840
path: ["foo"],
39-
source: message => message.endsWith("/test/") || message.startsWith("http://localhost"),
4041
}
4142
]);
4243
}

test/specs/refs.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ describe("$Refs object", () => {
216216
}
217217
catch (err) {
218218
expect(err).to.be.an.instanceOf(Error);
219-
expect(err.message).to.equal('Token "" does not exist.');
219+
expect(err.message).to.equal('at "#", token "" in "#/" does not exist');
220220
}
221221
});
222222

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

0 commit comments

Comments
 (0)