-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js.es6
70 lines (61 loc) · 1.63 KB
/
index.js.es6
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
58
59
60
61
62
63
64
65
66
67
68
69
70
export const decycle = (obj) => {
let cache = []
const stringified = JSON.stringify(obj, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
return
}
cache.push(value)
}
return value
})
cache = null
return stringified
}
export const setToValue = (obj, value, path) => {
if (!obj || typeof obj !== 'object') {
return
}
path = path.split('.')
var tmp = obj;
for (var i = 0, len = path.length; i < len - 1; i++) {
obj = Object.assign({}, obj[path[i]])
tmp[path[i]] = obj
tmp = obj
}
obj[path[i]] = value
}
export const sanitize = (state, keyPaths) => {
if (typeof keyPaths === 'function') {
return keyPaths(state)
}
const replacement = '********'
var updatedState = Object.assign({}, state)
for (var i = 0, len = keyPaths.length; i < len; i++) {
setToValue(updatedState, replacement, keyPaths[i])
}
return updatedState
}
const isError = (action) => action.error === true
const stateForError = (s, ks) => ks ? decycle(sanitize(s, ks)) : decycle(s)
export default function createMiddleware(Rollbar, keyPaths, wrapAction) {
return store => next => action => {
if (!isError(action)) {
if (wrapAction) {
try {
return next(action)
} catch (err) {
return Rollbar.error(err, {
action: decycle(action),
state: stateForError(store.getState(), keyPaths)
})
}
}
return next(action)
}
Rollbar.error(action.payload, {
state: stateForError(store.getState(), keyPaths)
})
return next(action)
}
}