Skip to content

Commit

Permalink
Fixed review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
vrptnc committed Dec 2, 2020
1 parent fa0732f commit efe36c8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/errors/error-codes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
XML_PARSING_ERROR: 100,
VAST_SCHEMA_VALIDATION_ERROR: 101
}
12 changes: 8 additions & 4 deletions src/errors/error-handler.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
3 changes: 2 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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
}
Expand Down

0 comments on commit efe36c8

Please sign in to comment.