@har-sdk/core
Version:
The base package can be used to import specification files (i.e. HAR, OAS and Postman Collection) and detect their type.
48 lines • 2.06 kB
JavaScript
import { BaseSyntaxErrorDetailsExtractor } from './BaseSyntaxErrorDetailsExtractor';
export class JsonSyntaxErrorDetailsExtractor extends BaseSyntaxErrorDetailsExtractor {
constructor() {
super(...arguments);
// error message from JSON.parse() are different in Firefox and Chrome/node
// see https://gist.github.com/pmstss/0cf3a26dd3c805389ef583c0279532e7 for details
this.LOCATION_PATTERNS = [
{
pattern: /at position (\d+)$/,
positionExtractor: ([, position]) => +position
},
{
pattern: /at line (\d+) column (\d+)/,
positionExtractor: ([, line, column]) => ({
lineNumber: +line,
columnNumber: +column
})
}
];
}
extractOffset(error) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/lineNumber
const position = 'lineNumber' in error && 'columnNumber' in error
? error
: this.extractByLocationPatterns(error.message);
return typeof position === 'number'
? position
: position &&
this.calculateOffset(position.lineNumber, position.columnNumber);
}
extractMessage(error) {
return error.message
.replace(/^JSON.parse: /, '')
.replace(/ at line \d+ column \d+ of the JSON data$/, '')
.replace(/ at position \d+$/, '');
}
extractByLocationPatterns(message) {
for (let idx = 0; idx < this.LOCATION_PATTERNS.length; ++idx) {
const locationPattern = this.LOCATION_PATTERNS[idx];
const matchRes = message.match(locationPattern.pattern);
if (matchRes) {
return locationPattern.positionExtractor(matchRes);
}
}
}
}
//# sourceMappingURL=JsonSyntaxErrorDetailsExtractor.js.map