-
Notifications
You must be signed in to change notification settings - Fork 0
/
log-error-in-promises.ts
44 lines (41 loc) · 1.85 KB
/
log-error-in-promises.ts
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
import { createRule } from '../utils/create-rule';
import { createLoggerCallTracker } from '../utils/logger-call-tracker';
import { parseSettings } from '../utils/settings';
export const logErrorInPromises = createRule({
meta: {
type: 'problem',
docs: {
category: 'Best Practices',
description: 'Suggest logging an error in every branch inside a catch block',
recommended: true,
// todo: add url
// url: ''
},
messages: {
'error-not-handled':
'In the catch block, you should either re-throw the original error, throw a new error, or log the error.',
},
schema: [],
},
create(context) {
const settings = parseSettings(context.settings);
const tracker = createLoggerCallTracker({
settings,
context,
messageId: 'error-not-handled',
});
const catchCall = 'CallExpression[callee.property.name="catch"]';
const catchCallWithArg = `${catchCall}[arguments.length=1]`;
return {
[`${catchCall}`]: tracker.onScopeEnter,
[`${catchCall}:exit`]: tracker.onScopeExit,
[`${catchCall} > :function`]: tracker.setScopeBoundary,
[`${catchCallWithArg} > :function BlockStatement`]: tracker.onBlockScopeEnter,
[`${catchCallWithArg} > :function BlockStatement:exit`]: tracker.onBlockScopeExit,
[`${catchCallWithArg} > :function ReturnStatement`]: tracker.onReturnStatement,
[`${catchCallWithArg} > :function ThrowStatement`]: tracker.onThrowStatement,
[`${catchCallWithArg} > :function CallExpression > .callee`]: tracker.assertLoggerReference,
[`${catchCallWithArg} > .arguments:matches(Identifier, MemberExpression)`]: tracker.assertLoggerReference,
};
},
});