-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (86 loc) · 2.8 KB
/
index.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
/**
* IMPORTANT: This app isn't an example of how to correctly
* set up a Financial Times Serverless application – it's
* used to illustrate how to integrate Reliability Kit into
* a serverless app with as little boilerplate code as
* possible.
*/
const logger = require('@dotcom-reliability-kit/logger');
const { logHandledError, logRecoverableError } = require('@dotcom-reliability-kit/log-error');
const { OperationalError } = require('@dotcom-reliability-kit/errors');
const registerCrashHandler = require('@dotcom-reliability-kit/crash-handler');
/**
* Reliability Kit method to ensure that fatal exceptions are logged.
* The earlier this is called in your app, the more likely it is to
* catch errors.
*
* @see https://github.com/Financial-Times/dotcom-reliability-kit/tree/main/packages/crash-handler#readme
*/
registerCrashHandler();
/**
* Define and export a Lambda handler. This is just a demo
* function to show how you can improve error throwing and
* logging with Reliability Kit.
*/
module.exports.example = async (event) => {
// We wrap everything in an outer try/catch so that we
// don't trigger unhandled rejections
try {
// If the event status property is bad, we throw
// an error which will get caught and logged with
// Reliability Kit
if (event?.status === 'bad') {
/**
* @see https://github.com/Financial-Times/dotcom-reliability-kit/tree/main/packages/errors#readme
*/
throw new OperationalError({
code: 'MALFORMED_EVENT',
message: 'Malformed event data'
});
}
// If an event stats property is questionable,
// we log a recoverable error (a warning-level log)
// and then continue on with our day
if (event.status === 'questionable') {
/**
* @see https://github.com/Financial-Times/dotcom-reliability-kit/tree/main/packages/log-error#readme
*/
logRecoverableError({
error: new OperationalError({
code: 'UNEXPECTED_EVENT',
message: 'Unexpected event data',
event
}),
// Note that you need to pass in a logger here, because
// otherwise log-error will use n-logger, which in a Lambda
// context does not support JSON-formatted logs.
logger
});
}
/**
* @see https://github.com/Financial-Times/dotcom-reliability-kit/tree/main/packages/logger#readme
*/
logger.debug('Everything is looking good');
// ...Continue processing here...
return {
message: 'Function executed successfully',
event
};
} catch (error) {
/**
* Log any errors here as "handled" - as in we've caught
* them and logged them so there's nothing further for
* the app to do.
*
* @see https://github.com/Financial-Times/dotcom-reliability-kit/tree/main/packages/log-error#readme
*/
logHandledError({
error,
logger
});
return {
message: 'Function failed to execute',
event
};
}
};