diff --git a/src/errors/error-codes.js b/src/errors/error-codes.js new file mode 100644 index 0000000..1491606 --- /dev/null +++ b/src/errors/error-codes.js @@ -0,0 +1,4 @@ +export default { + XML_PARSING_ERROR: 100, + VAST_SCHEMA_VALIDATION_ERROR: 101 +} diff --git a/src/errors/error-handler.js b/src/errors/error-handler.js index 84527eb..5429651 100644 --- a/src/errors/error-handler.js +++ b/src/errors/error-handler.js @@ -1,20 +1,24 @@ +import Errors from './error-codes' import VASTParserError from './vast-parser-error' - export default class ErrorHandler { constructor (strict) { this._strictMode = strict } - fail (error, errorCode = 101) { + failWithErrorCode (error, errorCode) { if (error instanceof VASTParserError) { throw error } throw new VASTParserError(error instanceof Error ? error.message : error, errorCode) } - tryRecover (error, errorCode = 101) { + fail (error) { + this.failWithErrorCode(error, Errors.VAST_SCHEMA_VALIDATION_ERROR) + } + + tryRecover (error) { if (this._strictMode) { - this.fail(error, errorCode) + this.fail(error) } } } diff --git a/src/parse.js b/src/parse.js index 0dcff89..b864b63 100644 --- a/src/parse.js +++ b/src/parse.js @@ -1,6 +1,7 @@ import Unmarshaler from './xml/unmarshaler' import schema from './vast/schema' import createVAST from './factory/vast' +import Errors from './errors/error-codes' import ErrorHandler from './errors/error-handler' const DEFAULT_OPTIONS = { @@ -17,7 +18,7 @@ const toElement = (xml, options) => { xml = xml.documentElement } if (xml.getElementsByTagName('parsererror').length > 0) { - options.errorHandler.fail('XML parsing error', 100) + options.errorHandler.failWithErrorCode('XML parsing error', Errors.XML_PARSING_ERROR) } return xml }