generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathXRayError.js
More file actions
94 lines (84 loc) · 2.47 KB
/
XRayError.js
File metadata and controls
94 lines (84 loc) · 2.47 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*/
'use strict';
module.exports.formatted = (err) => {
try {
return JSON.stringify(new XRayFormattedCause(err));
} catch (err) {
return '';
}
};
/**
* prepare an exception blob for sending to AWS X-Ray
* adapted from https://code.amazon.com/packages/AWSTracingSDKNode/blobs/c917508ca4fce6a795f95dc30c91b70c6bc6c617/--/core/lib/segments/attributes/captured_exception.js
* transform an Error, or Error-like, into an exception parseable by X-Ray's service.
* {
* "name": "CustomException",
* "message": "Something bad happend!",
* "stack": [
* "exports.handler (/var/task/node_modules/event_invoke.js:3:502)
* ]
* }
* =>
* {
* "working_directory": "/var/task",
* "exceptions": [
* {
* "type": "CustomException",
* "message": "Something bad happend!",
* "stack": [
* {
* "path": "/var/task/event_invoke.js",
* "line": 502,
* "label": "exports.throw_custom_exception"
* }
* ]
* }
* ],
* "paths": [
* "/var/task/event_invoke.js"
* ]
* }
*/
class XRayFormattedCause {
constructor(err) {
this.working_directory = process.cwd(); // eslint-disable-line
let stack = [];
if (err.stack) {
let stackLines = err.stack.replace(/\x7F/g, '%7F').split('\n');
stackLines.shift();
stackLines.forEach((stackLine) => {
let line = stackLine.trim().replace(/\(|\)/g, '');
line = line.substring(line.indexOf(' ') + 1);
let label =
line.lastIndexOf(' ') >= 0
? line.slice(0, line.lastIndexOf(' '))
: null;
let path =
label == undefined || label == null || label.length === 0
? line
: line.slice(line.lastIndexOf(' ') + 1);
path = path.split(':');
let entry = {
path: path[0],
line: parseInt(path[1]),
label: label || 'anonymous',
};
stack.push(entry);
});
}
this.exceptions = [
{
type: err.name?.replace(/\x7F/g, '%7F'),
message: err.message?.replace(/\x7F/g, '%7F'),
stack: stack,
},
];
let paths = new Set();
stack.forEach((entry) => {
paths.add(entry.path);
});
this.paths = Array.from(paths);
}
}