Skip to content

Commit 2cd48e0

Browse files
committed
Include error codes
1 parent fdb2774 commit 2cd48e0

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

lib/index.d.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,39 @@ declare namespace $RefParser {
411411
set($ref: string, value: JSONSchema4Type | JSONSchema6Type): void
412412
}
413413

414+
export type JSONParserErrorType = "EUNKNOWN" | "EPARSER" | "EUNMATCHEDPARSER" | "ERESOLVER" | "EUNMATCHEDRESOLVER" | "EMISSINGPOINTER" | "EINVALIDPOINTER";
415+
414416
export class JSONParserError extends Error {
417+
readonly name: string;
415418
readonly message: string;
416419
readonly path: Array<string | number>;
417420
readonly source: string;
421+
readonly code: JSONParserErrorType;
418422
}
419423

420-
export class ParserError extends JSONParserError {}
421-
export class UnmatchedParserError extends JSONParserError {}
424+
export class ParserError extends JSONParserError {
425+
readonly name = "ParserError";
426+
readonly code = "EPARSER";
427+
}
428+
export class UnmatchedParserError extends JSONParserError {
429+
readonly name = "UnmatchedParserError";
430+
readonly code ="EUNMATCHEDPARSER";
431+
}
422432
export class ResolverError extends JSONParserError {
423-
readonly code?: string;
433+
readonly name = "ResolverError";
434+
readonly code ="ERESOLVER";
435+
readonly ioErrorCode?: string;
436+
}
437+
export class UnmatchedResolverError extends JSONParserError {
438+
readonly name = "UnmatchedResolverError";
439+
readonly code ="EUNMATCHEDRESOLVER";
440+
}
441+
export class MissingPointerError extends JSONParserError {
442+
readonly name = "MissingPointerError";
443+
readonly code ="EMISSINGPOINTER";
444+
}
445+
export class InvalidPointerError extends JSONParserError {
446+
readonly name = "InvalidPointerError";
447+
readonly code ="EINVALIDPOINTER";
424448
}
425-
export class UnmatchedResolverError extends JSONParserError {}
426-
export class MissingPointerError extends JSONParserError {}
427-
export class InvalidPointerError extends JSONParserError {}
428449
}

lib/util/errors.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const JSONParserError = exports.JSONParserError = class JSONParserError extends
88
constructor (message, source) {
99
super();
1010

11+
this.code = "EUNKNOWN";
1112
this.message = message;
1213
this.source = source;
1314
this.path = [];
@@ -22,6 +23,7 @@ const JSONParserErrorGroup = exports.JSONParserErrorGroup = class JSONParserErro
2223
constructor (errors, source) {
2324
super();
2425

26+
this.code = "EUNKNOWN";
2527
this._path = undefined;
2628
this._source = source;
2729
this.errors = errors;
@@ -62,6 +64,7 @@ exports.StoplightParserError = class StoplightParserError extends JSONParserErro
6264
return parserError;
6365
}));
6466

67+
this.code = "ESTOPLIGHTPARSER";
6568
this.message = `Error parsing ${source}`;
6669
}
6770

@@ -77,6 +80,8 @@ exports.StoplightParserError = class StoplightParserError extends JSONParserErro
7780
const ParserError = exports.ParserError = class ParserError extends JSONParserError {
7881
constructor (message, source) {
7982
super(`Error parsing ${source}: ${message}`, source);
83+
84+
this.code = "EPARSER";
8085
}
8186
};
8287

@@ -85,6 +90,8 @@ setErrorName(ParserError);
8590
const UnmatchedParserError = exports.UnmatchedParserError = class UnmatchedParserError extends JSONParserError {
8691
constructor (source) {
8792
super(`Could not find parser for "${source}"`, source);
93+
94+
this.code = "EUNMATCHEDPARSER";
8895
}
8996
};
9097

@@ -93,8 +100,11 @@ setErrorName(UnmatchedParserError);
93100
const ResolverError = exports.ResolverError = class ResolverError extends JSONParserError {
94101
constructor (ex, source) {
95102
super(ex.message || `Error reading file "${source}"`, source);
103+
104+
this.code = "ERESOLVER";
105+
96106
if ("code" in ex) {
97-
this.code = String(ex.code);
107+
this.ioErrorCode = String(ex.code);
98108
}
99109
}
100110
};
@@ -104,6 +114,8 @@ setErrorName(ResolverError);
104114
const UnmatchedResolverError = exports.UnmatchedResolverError = class UnmatchedResolverError extends JSONParserError {
105115
constructor (source) {
106116
super(`Could not find resolver for "${source}"`, source);
117+
118+
this.code = "EUNMATCHEDRESOLVER";
107119
}
108120
};
109121

@@ -112,6 +124,8 @@ setErrorName(UnmatchedResolverError);
112124
const MissingPointerError = exports.MissingPointerError = class MissingPointerError extends JSONParserError {
113125
constructor (token, path) {
114126
super(`Token "${token}" does not exist.`, stripHash(path));
127+
128+
this.code = "EMISSINGPOINTER";
115129
}
116130
};
117131

@@ -120,6 +134,8 @@ setErrorName(MissingPointerError);
120134
const InvalidPointerError = exports.InvalidPointerError = class InvalidPointerError extends JSONParserError {
121135
constructor (pointer, path) {
122136
super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, stripHash(path));
137+
138+
this.code = "EINVALIDPOINTER";
123139
}
124140
};
125141

test/specs/invalid/invalid.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("Invalid syntax", () => {
2020
catch (err) {
2121
expect(err).to.be.an.instanceOf(ResolverError);
2222
if (host.node) {
23-
expect(err.code).to.equal("ENOENT");
23+
expect(err.ioErrorCode).to.equal("ENOENT");
2424
expect(err.message).to.contain("Error opening file ");
2525
}
2626
}

0 commit comments

Comments
 (0)