Skip to content

Commit

Permalink
fix(src/plugins/utils/findOctetSequencePaths.js): Show path to erring…
Browse files Browse the repository at this point in the history
… element on exception

Recently, I ran across an issue where [if the validator throws an exception in a specific part of
the code, the path to the exception-causing code is
swallowed](#180). After a moderate amount of digging,
I decided that an option for providing more information about this code would be to put the path to
the problematic element or elements into the exception message. This exception message is displayed
whenever the code crashes out. So, for example, the previous error message output by lint-openapi
would say "Cannot read property 'type' of null", whereas the new error would list the full path to
the type that was causing the exception.

re #180
  • Loading branch information
distortedsignal committed Aug 27, 2020
1 parent a7089b8 commit f79dd0b
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/plugins/utils/findOctetSequencePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,36 @@ function arrayOctetSequences(resolvedSchema, path) {
const pathToSchema = Array.isArray(path)
? path.concat('items')
: `${path}.items`;
if (arrayItems.type === 'string' && arrayItems.format === 'binary') {
arrayPathsToOctetSequence.push(pathToSchema);
} else if (arrayItems.type === 'object' || arrayItems.type === 'array') {
arrayPathsToOctetSequence.push(
...findOctetSequencePaths(arrayItems, pathToSchema)
);
try {
if (arrayItems.type === 'string' && arrayItems.format === 'binary') {
arrayPathsToOctetSequence.push(pathToSchema);
} else if (arrayItems.type === 'object' || arrayItems.type === 'array') {
arrayPathsToOctetSequence.push(
...findOctetSequencePaths(arrayItems, pathToSchema)
);
}
} catch(err) {
if (err instanceof TypeError) {
var escapedPaths = [];
const strEscaper = function(strToEscape) {
var newStr = "";
for (i=0;i<strToEscape.length;i++) {
if (strToEscape.charAt(i) == "/") {
newStr = newStr + "\\/";
} else {
newStr = newStr + strToEscape.charAt(i);
}
}
escapedPaths.push(newStr);
};
path.forEach(strEscaper);
var e = new TypeError("items.type and items.format must resolve for the path \"" + escapedPaths.join("/") + "\"");
e.stack = err.stack;
e.original = err;
throw e;
} else {
throw err;
}
}
}
return arrayPathsToOctetSequence;
Expand Down

0 comments on commit f79dd0b

Please sign in to comment.