Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/errorMessage.ts
13397 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 * as l10n from '@vscode/l10n';
7
import * as arrays from '../vs/base/common/arrays';
8
import * as types from '../vs/base/common/types';
9
10
/* eslint-disable @typescript-eslint/no-explicit-any */
11
12
function exceptionToErrorMessage(exception: unknown, verbose: boolean): string {
13
if (verbose && ((exception as any).stack || (exception as any).stacktrace)) {
14
const stackStr = stackToString((exception as any).stack) || stackToString((exception as any).stacktrace);
15
return `${detectSystemErrorMessage(exception)}: ${stackStr || ''}`;
16
}
17
18
return detectSystemErrorMessage(exception);
19
}
20
21
function stackToString(stack: string[] | string | undefined): string | undefined {
22
if (Array.isArray(stack)) {
23
return stack.join('\n');
24
}
25
26
return stack;
27
}
28
29
function detectSystemErrorMessage(exception: any): string {
30
31
// Custom node.js error from us
32
if (exception.code === 'ERR_UNC_HOST_NOT_ALLOWED') {
33
return l10n.t("{0}. Please update the '{1}' setting if you want to allow this host.", exception.message, 'security.allowedUNCHosts');
34
}
35
36
// See https://nodejs.org/api/errors.html#errors_class_system_error
37
if (typeof exception.code === 'string' && typeof exception.errno === 'number' && typeof exception.syscall === 'string') {
38
return l10n.t('A system error occurred ({0})', exception.message);
39
}
40
41
return exception.message || l10n.t('An unknown error occurred. Please consult the log for more details.');
42
}
43
44
/**
45
* Tries to generate a human readable error message out of the error. If the verbose parameter
46
* is set to true, the error message will include stacktrace details if provided.
47
*
48
* @returns A string containing the error message.
49
*/
50
export function toErrorMessage(error: any = null, verbose: boolean = false): string {
51
if (!error) {
52
return l10n.t('An unknown error occurred. Please consult the log for more details.');
53
}
54
55
if (Array.isArray(error)) {
56
const errors: any[] = arrays.coalesce(error);
57
const msg = toErrorMessage(errors[0], verbose);
58
59
if (errors.length > 1) {
60
return l10n.t('{0} ({1} errors in total)', msg, errors.length);
61
}
62
63
return msg;
64
}
65
66
if (types.isString(error)) {
67
return error;
68
}
69
70
if (error.detail) {
71
const detail = error.detail;
72
73
if (detail.error) {
74
return exceptionToErrorMessage(detail.error, verbose);
75
}
76
77
if (detail.exception) {
78
return exceptionToErrorMessage(detail.exception, verbose);
79
}
80
}
81
82
if (error.stack) {
83
return exceptionToErrorMessage(error, verbose);
84
}
85
86
if (error.message) {
87
return error.message;
88
}
89
90
return l10n.t('An unknown error occurred. Please consult the log for more details.');
91
}
92
93