-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvastError.js
57 lines (45 loc) · 1.49 KB
/
vastError.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import vastErrorCodes from './vastErrorCodes'
import { getArrayFromObjectPath as read, pluckNodeValue, isDefined, flatten } from './util/objectUtil'
function getErrorMessageFromCode (code) {
var errorName,
error
for (errorName in vastErrorCodes) {
if (vastErrorCodes.hasOwnProperty(errorName)) {
error = vastErrorCodes[errorName]
if (error.code === code) {
return error.message
}
}
}
return 'Unknown error code'
}
function extractErrorURIs (vastResponse) {
var errorURIs = []
var wrapperErrorArrays
if (vastResponse) {
wrapperErrorArrays = read(vastResponse, 'wrappers').map(function getWrapperErrors (wrapper) {
return []
.concat(read(wrapper, 'VAST.Error'))
.concat(read(wrapper, 'VAST.Ad.Wrapper.Error'))
})
errorURIs = flatten(wrapperErrorArrays)
.concat(read(vastResponse, 'inline.VAST.Error'))
.concat(read(vastResponse, 'inline.VAST.Ad.InLine.Error'))
.map(pluckNodeValue)
.filter(isDefined)
}
return errorURIs
}
export default function VastError (code, vastResponse, message) {
this.name = 'VastError'
this.code = code
this.message = 'VAST Error: [' + code + '] - ' + (message || getErrorMessageFromCode(code))
this.stack = (new Error()).stack
this.errorURIs = extractErrorURIs(vastResponse)
this.vastResponse = vastResponse
}
VastError.prototype = new Error()
VastError.prototype.constructor = VastError
VastError.prototype.getErrorURIs = function () {
return this.errorURIs
}