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