-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathXRayErrorTest.js
More file actions
107 lines (91 loc) · 3.21 KB
/
XRayErrorTest.js
File metadata and controls
107 lines (91 loc) · 3.21 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
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*/
'use strict';
require('should');
const { formatted } = require('lambda-runtime/XRayError.js');
describe('XRayFormattedCause', () => {
it('should handle a basic error with stack trace', () => {
const error = new Error('Something went wrong');
error.name = 'CustomError';
error.stack = `CustomError: Something went wrong
at someFunction (/var/task/handler.js:10:15)`;
const result = JSON.parse(formatted(error));
result.should.have.property('working_directory').which.is.a.String();
result.should.have.property('exceptions').with.length(1);
result.exceptions[0].should.have.property('type', 'CustomError');
result.exceptions[0].should.have.property(
'message',
'Something went wrong',
);
result.exceptions[0].stack.should.deepEqual([
{
path: '/var/task/handler.js',
line: 10,
label: 'someFunction',
},
]);
result.paths.should.deepEqual(['/var/task/handler.js']);
});
it('should handle an error without stack trace', () => {
const error = new Error('No stack here');
error.name = 'NoStackError';
error.stack = null;
const result = JSON.parse(formatted(error));
result.exceptions[0].should.have.property('stack').with.length(0);
result.paths.should.eql([]);
});
it('should handle multiple stack frames', () => {
const error = new Error('Complex error');
error.name = 'ComplexError';
error.stack = `ComplexError: Complex error
at firstFunction (/var/task/one.js:1:100)
at secondFunction (/var/task/two.js:2:200)
at /var/task/three.js:3:300`;
const result = JSON.parse(formatted(error));
result.exceptions[0].stack.should.deepEqual([
{ path: '/var/task/one.js', line: 1, label: 'firstFunction' },
{ path: '/var/task/two.js', line: 2, label: 'secondFunction' },
{ path: '/var/task/three.js', line: 3, label: 'anonymous' },
]);
result.paths.should.eql([
'/var/task/one.js',
'/var/task/two.js',
'/var/task/three.js',
]);
});
it('should encode invalid characters in name and message', () => {
const error = new Error('\x7Fmessage');
error.name = 'Name\x7F';
error.stack = `Name\x7F: \x7Fmessage
at anon (/var/task/bad.js:99:1)`;
const result = JSON.parse(formatted(error));
result.exceptions[0].type.should.equal('Name%7F');
result.exceptions[0].message.should.equal('%7Fmessage');
});
it('should return empty string on circular reference', () => {
class CircularError extends Error {
constructor() {
super('circular');
this.name = 'CircularError';
this.circular = this;
}
toString() {
return 'CircularError: circular';
}
}
const error = new CircularError();
error.stack = `CircularError: circular
at circularFunction (/var/task/circle.js:1:1)`;
// Manually inject the circular object into a field that gets stringified
const originalStack = error.stack;
error.stack = {
toString: () => {
return originalStack;
},
};
error.stack.circular = error.stack;
const result = formatted(error);
result.should.equal('');
});
});