Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/test/node/errorMessage.spec.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { describe, expect, it } from 'vitest';
7
import { toErrorMessage } from '../../common/errorMessage';
8
9
describe('errorMessage', () => {
10
describe('toErrorMessage', () => {
11
it('returns default message for null', () => {
12
const result = toErrorMessage(null);
13
// In test environment, l10n.t returns the message itself
14
expect(result).toBe('An unknown error occurred. Please consult the log for more details.');
15
});
16
17
it('returns default message for undefined', () => {
18
const result = toErrorMessage(undefined);
19
expect(result).toBe('An unknown error occurred. Please consult the log for more details.');
20
});
21
22
it('returns the string when error is a string', () => {
23
const result = toErrorMessage('Test error message');
24
expect(result).toBe('Test error message');
25
});
26
27
it('returns error.message for Error objects', () => {
28
const error = new Error('Something went wrong');
29
const result = toErrorMessage(error);
30
expect(result).toBe('Something went wrong');
31
});
32
33
it('handles array of errors and returns first error message', () => {
34
const errors = [new Error('First error'), new Error('Second error')];
35
const result = toErrorMessage(errors);
36
// In test environment, l10n.t returns the formatted message
37
expect(result).toBe('First error (2 errors in total)');
38
});
39
40
it('handles array with single error', () => {
41
const errors = [new Error('Only error')];
42
const result = toErrorMessage(errors);
43
expect(result).toBe('Only error');
44
});
45
46
it('handles error with detail.error property', () => {
47
const error = {
48
detail: {
49
error: new Error('Detail error')
50
}
51
};
52
const result = toErrorMessage(error);
53
expect(result).toBe('Detail error');
54
});
55
56
it('handles error with detail.exception property', () => {
57
const error = {
58
detail: {
59
exception: new Error('Detail exception')
60
}
61
};
62
const result = toErrorMessage(error);
63
expect(result).toBe('Detail exception');
64
});
65
66
it('includes stack trace in verbose mode', () => {
67
const error = new Error('Error with stack');
68
error.stack = 'Error: Error with stack\n at someFunction (file.ts:10:5)';
69
const result = toErrorMessage(error, true);
70
// Now using template string format
71
expect(result).toBe('Error with stack: Error: Error with stack\n at someFunction (file.ts:10:5)');
72
});
73
74
it('does not include stack trace in non-verbose mode', () => {
75
const error = new Error('Error with stack');
76
error.stack = 'Error: Error with stack\n at someFunction (file.ts:10:5)';
77
const result = toErrorMessage(error, false);
78
expect(result).toBe('Error with stack');
79
expect(result).not.toContain('at someFunction');
80
});
81
82
it('handles stack trace as array', () => {
83
const error: any = {
84
message: 'Array stack error',
85
stack: ['at line 1', 'at line 2', 'at line 3']
86
};
87
const result = toErrorMessage(error, true);
88
expect(result).toBe('Array stack error: at line 1\nat line 2\nat line 3');
89
});
90
91
it('handles Node.js system errors', () => {
92
const systemError: any = {
93
code: 'ENOENT',
94
errno: -2,
95
syscall: 'open',
96
message: 'File not found'
97
};
98
const result = toErrorMessage(systemError);
99
// System error detection returns l10n.t result, which in tests returns the key
100
// However, since the error has a .message, toErrorMessage returns that directly (line 86)
101
expect(result).toBe('File not found');
102
});
103
104
it('handles ERR_UNC_HOST_NOT_ALLOWED error', () => {
105
const uncError: any = {
106
code: 'ERR_UNC_HOST_NOT_ALLOWED',
107
message: 'UNC host not allowed',
108
// Need to add stack for the error to go through exceptionToErrorMessage
109
stack: 'Error: UNC host not allowed\n at someFunction'
110
};
111
const result = toErrorMessage(uncError);
112
// With a stack, it goes through exceptionToErrorMessage -> detectSystemErrorMessage
113
expect(result).toContain('UNC host not allowed');
114
expect(result).toContain('security.allowedUNCHosts');
115
});
116
117
it('filters out null/undefined values from error array', () => {
118
const errors = [null, undefined, new Error('Valid error'), null];
119
const result = toErrorMessage(errors);
120
expect(result).toBe('Valid error');
121
});
122
});
123
});
124
125