forked from aws/aws-lambda-nodejs-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrors.js
121 lines (110 loc) · 2.96 KB
/
Errors.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Defines custom error types throwable by the runtime.
*/
'use strict';
const util = require('util');
function _isError(obj) {
return (
obj &&
obj.name &&
obj.message &&
obj.stack &&
typeof obj.name === 'string' &&
typeof obj.message === 'string' &&
typeof obj.stack === 'string'
);
}
function intoError(err) {
if (err instanceof Error) {
return err;
} else {
return new Error(err);
}
}
module.exports.intoError = intoError;
/**
* Attempt to convert an object into a response object.
* This method accounts for failures when serializing the error object.
*/
function toRapidResponse(error) {
try {
if (util.types.isNativeError(error) || _isError(error)) {
return {
errorType: error.name?.replace(/\x7F/g, '%7F'),
errorMessage: error.message?.replace(/\x7F/g, '%7F'),
trace: error.stack.replace(/\x7F/g, '%7F').split('\n'),
};
} else {
return {
errorType: typeof error,
errorMessage: error.toString(),
trace: [],
};
}
} catch (_err) {
return {
errorType: 'handled',
errorMessage:
'callback called with Error argument, but there was a problem while retrieving one or more of its message, name, and stack',
};
}
}
module.exports.toRapidResponse = toRapidResponse;
/**
* Format an error with the expected properties.
* For compatability, the error string always starts with a tab.
*/
module.exports.toFormatted = (error) => {
try {
return (
'\t' + JSON.stringify(error, (_k, v) => _withEnumerableProperties(v))
);
} catch (err) {
return '\t' + JSON.stringify(toRapidResponse(error));
}
};
/**
* Error name, message, code, and stack are all members of the superclass, which
* means they aren't enumerable and don't normally show up in JSON.stringify.
* This method ensures those interesting properties are available along with any
* user-provided enumerable properties.
*/
function _withEnumerableProperties(error) {
if (error instanceof Error) {
let ret = Object.assign(
{
errorType: error.name,
errorMessage: error.message,
code: error.code,
},
error,
);
if (typeof error.stack == 'string') {
ret.stack = error.stack.split('\n');
}
return ret;
} else {
return error;
}
}
const errorClasses = [
class ImportModuleError extends Error {},
class HandlerNotFound extends Error {},
class MalformedHandlerName extends Error {},
class UserCodeSyntaxError extends Error {},
class MalformedStreamingHandler extends Error {},
class InvalidStreamingOperation extends Error {},
class UnhandledPromiseRejection extends Error {
constructor(reason, promise) {
super(reason);
this.reason = reason;
this.promise = promise;
}
},
];
errorClasses.forEach((e) => {
module.exports[e.name] = e;
e.prototype.name = `Runtime.${e.name}`;
});