-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve and add some errors (#61)
* feat: improve errors * Unify errors and make errors aware of the original document * Fix typo * Update docs * Change upgrade for update * Add toJS method to the error class * Add docs for toJS method * Add info about all the errors in the README * Change error message and fix tests
- Loading branch information
Showing
14 changed files
with
956 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,62 @@ | ||
class ParserError extends Error { | ||
constructor(e, json, errors) { | ||
super(e); | ||
|
||
let msg; | ||
|
||
if (typeof e === 'string') { | ||
msg = e; | ||
} | ||
if (typeof e.message === 'string') { | ||
msg = e.message; | ||
} | ||
|
||
if (json) { | ||
this.parsedJSON = json; | ||
} | ||
const ERROR_URL_PREFIX = 'https://github.com/asyncapi/parser-js/'; | ||
|
||
if (errors) { | ||
this.errors = errors; | ||
} | ||
/** | ||
* Represents an error while trying to parse an AsyncAPI document. | ||
*/ | ||
class ParserError extends Error { | ||
/** | ||
* Instantiates an error | ||
* @param {Object} definition | ||
* @param {String} definition.type The type of the error. | ||
* @param {String} definition.title The message of the error. | ||
* @param {String} [definition.detail] A string containing more detailed information about the error. | ||
* @param {Object} [definition.parsedJSON] The resulting JSON after YAML transformation. Or the JSON object if the this was the initial format. | ||
* @param {Object[]} [definition.validationErrors] The errors resulting from the validation. For more information, see https://www.npmjs.com/package/better-ajv-errors. | ||
* @param {String} definition.validationErrors.title A validation error message. | ||
* @param {String} definition.validationErrors.jsonPointer The path to the field that contains the error. Uses JSON Pointer format. | ||
* @param {Number} definition.validationErrors.startLine The line where the error starts in the AsyncAPI document. | ||
* @param {Number} definition.validationErrors.startColumn The column where the error starts in the AsyncAPI document. | ||
* @param {Number} definition.validationErrors.startOffset The offset (starting from the beginning of the document) where the error starts in the AsyncAPI document. | ||
* @param {Number} definition.validationErrors.endLine The line where the error ends in the AsyncAPI document. | ||
* @param {Number} definition.validationErrors.endColumn The column where the error ends in the AsyncAPI document. | ||
* @param {Number} definition.validationErrors.endOffset The offset (starting from the beginning of the document) where the error ends in the AsyncAPI document. | ||
* @param {Object} [definition.location] Error location details after trying to parse an invalid JSON or YAML document. | ||
* @param {Number} definition.location.startLine The line of the YAML/JSON document where the error starts. | ||
* @param {Number} definition.location.startColumn The column of the YAML/JSON document where the error starts. | ||
* @param {Number} definition.location.startOffset The offset (starting from the beginning of the document) where the error starts in the YAML/JSON AsyncAPI document. | ||
* @param {Object[]} [definition.refs] Error details after trying to resolve $ref's. | ||
* @param {String} definition.refs.title A validation error message. | ||
* @param {String} definition.refs.jsonPointer The path to the field that contains the error. Uses JSON Pointer format. | ||
* @param {Number} definition.refs.startLine The line where the error starts in the AsyncAPI document. | ||
* @param {Number} definition.refs.startColumn The column where the error starts in the AsyncAPI document. | ||
* @param {Number} definition.refs.startOffset The offset (starting from the beginning of the document) where the error starts in the AsyncAPI document. | ||
* @param {Number} definition.refs.endLine The line where the error ends in the AsyncAPI document. | ||
* @param {Number} definition.refs.endColumn The column where the error ends in the AsyncAPI document. | ||
* @param {Number} definition.refs.endOffset The offset (starting from the beginning of the document) where the error ends in the AsyncAPI document. | ||
*/ | ||
constructor(def) { | ||
super(); | ||
buildError(def, this); | ||
this.message = def.title; | ||
} | ||
|
||
this.message = msg; | ||
/** | ||
* Returns a JS object representation of the error. | ||
*/ | ||
toJS() { | ||
return buildError(this, {}); | ||
} | ||
} | ||
|
||
const buildError = (from, to) => { | ||
to.type = from.type.startsWith(ERROR_URL_PREFIX) ? from.type : `${ERROR_URL_PREFIX}${from.type}`; | ||
to.title = from.title; | ||
if (from.detail) to.detail = from.detail; | ||
if (from.validationErrors) to.validationErrors = from.validationErrors; | ||
if (from.parsedJSON) to.parsedJSON = from.parsedJSON; | ||
if (from.location) to.location = from.location; | ||
if (from.refs) to.refs = from.refs; | ||
return to; | ||
}; | ||
|
||
module.exports = ParserError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module.exports = (txt, reviver, context = 20) => { | ||
try { | ||
return JSON.parse(txt, reviver) | ||
} catch (e) { | ||
if (typeof txt !== 'string') { | ||
const isEmptyArray = Array.isArray(txt) && txt.length === 0 | ||
const errorMessage = 'Cannot parse ' + | ||
(isEmptyArray ? 'an empty array' : String(txt)) | ||
throw new TypeError(errorMessage) | ||
} | ||
const syntaxErr = e.message.match(/^Unexpected token.*position\s+(\d+)/i) | ||
const errIdx = syntaxErr | ||
? +syntaxErr[1] | ||
: e.message.match(/^Unexpected end of JSON.*/i) | ||
? txt.length - 1 | ||
: null | ||
if (errIdx != null) { | ||
const start = errIdx <= context | ||
? 0 | ||
: errIdx - context | ||
const end = errIdx + context >= txt.length | ||
? txt.length | ||
: errIdx + context | ||
e.message += ` while parsing near '${ | ||
start === 0 ? '' : '...' | ||
}${txt.slice(start, end)}${ | ||
end === txt.length ? '' : '...' | ||
}'` | ||
} else { | ||
e.message += ` while parsing '${txt.slice(0, context * 2)}'` | ||
} | ||
e.offset = errIdx | ||
const lines = txt.substr(0, errIdx).split('\n'); | ||
e.startLine = lines.length; | ||
e.startColumn = lines[lines.length - 1].length; | ||
throw e | ||
} | ||
} |
Oops, something went wrong.