| 186 | } |
| 187 | |
| 188 | attachContext (error) { |
| 189 | if (!error) return error |
| 190 | |
| 191 | // if pmx attached callsites we can parse them to retrieve the context |
| 192 | if (typeof (error.stackframes) === 'object') { |
| 193 | let result = this.parse(error.stackframes) |
| 194 | // no need to send it since there is already the stacktrace |
| 195 | delete error.stackframes |
| 196 | delete error.__error_callsites |
| 197 | |
| 198 | if (result) { |
| 199 | error.callsite = result.callsite |
| 200 | error.context = result.context |
| 201 | } |
| 202 | } |
| 203 | // if the stack is here we can parse it directly from the stack string |
| 204 | // only if the context has been retrieved from elsewhere |
| 205 | if (typeof error.stack === 'string' && !error.callsite) { |
| 206 | let siteRegex = /(\/[^\\\n]*)/g |
| 207 | let tmp |
| 208 | let stack = [] |
| 209 | |
| 210 | // find matching groups |
| 211 | while ((tmp = siteRegex.exec(error.stack))) { |
| 212 | stack.push(tmp[1]) |
| 213 | } |
| 214 | |
| 215 | // parse each callsite to match the format used by the stackParser |
| 216 | stack = stack.map((callsite) => { |
| 217 | // remove the trailing ) if present |
| 218 | if (callsite[callsite.length - 1] === ')') { |
| 219 | callsite = callsite.substr(0, callsite.length - 1) |
| 220 | } |
| 221 | let location = callsite.split(':') |
| 222 | |
| 223 | return location.length < 3 ? callsite : { |
| 224 | file_name: location[0], |
| 225 | line_number: parseInt(location[1]) |
| 226 | } |
| 227 | }) |
| 228 | |
| 229 | let finalCallsite = this.parse(stack) |
| 230 | if (finalCallsite) { |
| 231 | error.callsite = finalCallsite.callsite |
| 232 | error.context = finalCallsite.context |
| 233 | } |
| 234 | } |
| 235 | return error |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // EWMA = ExponentiallyWeightedMovingAverage from |