Path: blob/main/extensions/copilot/src/util/common/errorMessage.ts
13397 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as l10n from '@vscode/l10n';6import * as arrays from '../vs/base/common/arrays';7import * as types from '../vs/base/common/types';89/* eslint-disable @typescript-eslint/no-explicit-any */1011function exceptionToErrorMessage(exception: unknown, verbose: boolean): string {12if (verbose && ((exception as any).stack || (exception as any).stacktrace)) {13const stackStr = stackToString((exception as any).stack) || stackToString((exception as any).stacktrace);14return `${detectSystemErrorMessage(exception)}: ${stackStr || ''}`;15}1617return detectSystemErrorMessage(exception);18}1920function stackToString(stack: string[] | string | undefined): string | undefined {21if (Array.isArray(stack)) {22return stack.join('\n');23}2425return stack;26}2728function detectSystemErrorMessage(exception: any): string {2930// Custom node.js error from us31if (exception.code === 'ERR_UNC_HOST_NOT_ALLOWED') {32return l10n.t("{0}. Please update the '{1}' setting if you want to allow this host.", exception.message, 'security.allowedUNCHosts');33}3435// See https://nodejs.org/api/errors.html#errors_class_system_error36if (typeof exception.code === 'string' && typeof exception.errno === 'number' && typeof exception.syscall === 'string') {37return l10n.t('A system error occurred ({0})', exception.message);38}3940return exception.message || l10n.t('An unknown error occurred. Please consult the log for more details.');41}4243/**44* Tries to generate a human readable error message out of the error. If the verbose parameter45* is set to true, the error message will include stacktrace details if provided.46*47* @returns A string containing the error message.48*/49export function toErrorMessage(error: any = null, verbose: boolean = false): string {50if (!error) {51return l10n.t('An unknown error occurred. Please consult the log for more details.');52}5354if (Array.isArray(error)) {55const errors: any[] = arrays.coalesce(error);56const msg = toErrorMessage(errors[0], verbose);5758if (errors.length > 1) {59return l10n.t('{0} ({1} errors in total)', msg, errors.length);60}6162return msg;63}6465if (types.isString(error)) {66return error;67}6869if (error.detail) {70const detail = error.detail;7172if (detail.error) {73return exceptionToErrorMessage(detail.error, verbose);74}7576if (detail.exception) {77return exceptionToErrorMessage(detail.exception, verbose);78}79}8081if (error.stack) {82return exceptionToErrorMessage(error, verbose);83}8485if (error.message) {86return error.message;87}8889return l10n.t('An unknown error occurred. Please consult the log for more details.');90}919293