-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathhandle-errors.ts
More file actions
61 lines (45 loc) · 1.87 KB
/
handle-errors.ts
File metadata and controls
61 lines (45 loc) · 1.87 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
import { describe, expect, test } from 'vitest'
import { shouldLogException, type ErrorWithCode } from '../lib/should-log-exception'
// Helper function to create test errors with code property
function createError(message: string, name: string = 'Error', code: string = ''): ErrorWithCode {
const error = new Error(message) as ErrorWithCode
error.name = name
error.code = code
return error
}
describe('shouldLogException', () => {
describe('ECONNRESET errors', () => {
test('should not log ECONNRESET errors', () => {
const error = createError('Connection reset', 'Error', 'ECONNRESET')
expect(shouldLogException(error)).toBe(false)
})
})
describe('TypeError: terminated filtering', () => {
test('should not log TypeError with exact message "terminated"', () => {
const error = createError('terminated', 'TypeError')
expect(shouldLogException(error)).toBe(false)
})
test('should log TypeError with different message', () => {
const error = createError('Cannot read property', 'TypeError')
expect(shouldLogException(error)).toBe(true)
})
test('should log TypeError with partial "terminated" message', () => {
const error = createError('connection terminated unexpectedly', 'TypeError')
expect(shouldLogException(error)).toBe(true)
})
test('should log non-TypeError with "terminated" message', () => {
const error = createError('terminated', 'Error')
expect(shouldLogException(error)).toBe(true)
})
})
describe('regular errors', () => {
test('should log regular errors', () => {
const error = createError('Something went wrong', 'Error')
expect(shouldLogException(error)).toBe(true)
})
test('should log errors with no code', () => {
const error = createError('Test error', 'Error')
expect(shouldLogException(error)).toBe(true)
})
})
})