Path: blob/main/extensions/copilot/src/util/test/node/errorMessage.spec.ts
13401 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 { describe, expect, it } from 'vitest';6import { toErrorMessage } from '../../common/errorMessage';78describe('errorMessage', () => {9describe('toErrorMessage', () => {10it('returns default message for null', () => {11const result = toErrorMessage(null);12// In test environment, l10n.t returns the message itself13expect(result).toBe('An unknown error occurred. Please consult the log for more details.');14});1516it('returns default message for undefined', () => {17const result = toErrorMessage(undefined);18expect(result).toBe('An unknown error occurred. Please consult the log for more details.');19});2021it('returns the string when error is a string', () => {22const result = toErrorMessage('Test error message');23expect(result).toBe('Test error message');24});2526it('returns error.message for Error objects', () => {27const error = new Error('Something went wrong');28const result = toErrorMessage(error);29expect(result).toBe('Something went wrong');30});3132it('handles array of errors and returns first error message', () => {33const errors = [new Error('First error'), new Error('Second error')];34const result = toErrorMessage(errors);35// In test environment, l10n.t returns the formatted message36expect(result).toBe('First error (2 errors in total)');37});3839it('handles array with single error', () => {40const errors = [new Error('Only error')];41const result = toErrorMessage(errors);42expect(result).toBe('Only error');43});4445it('handles error with detail.error property', () => {46const error = {47detail: {48error: new Error('Detail error')49}50};51const result = toErrorMessage(error);52expect(result).toBe('Detail error');53});5455it('handles error with detail.exception property', () => {56const error = {57detail: {58exception: new Error('Detail exception')59}60};61const result = toErrorMessage(error);62expect(result).toBe('Detail exception');63});6465it('includes stack trace in verbose mode', () => {66const error = new Error('Error with stack');67error.stack = 'Error: Error with stack\n at someFunction (file.ts:10:5)';68const result = toErrorMessage(error, true);69// Now using template string format70expect(result).toBe('Error with stack: Error: Error with stack\n at someFunction (file.ts:10:5)');71});7273it('does not include stack trace in non-verbose mode', () => {74const error = new Error('Error with stack');75error.stack = 'Error: Error with stack\n at someFunction (file.ts:10:5)';76const result = toErrorMessage(error, false);77expect(result).toBe('Error with stack');78expect(result).not.toContain('at someFunction');79});8081it('handles stack trace as array', () => {82const error: any = {83message: 'Array stack error',84stack: ['at line 1', 'at line 2', 'at line 3']85};86const result = toErrorMessage(error, true);87expect(result).toBe('Array stack error: at line 1\nat line 2\nat line 3');88});8990it('handles Node.js system errors', () => {91const systemError: any = {92code: 'ENOENT',93errno: -2,94syscall: 'open',95message: 'File not found'96};97const result = toErrorMessage(systemError);98// System error detection returns l10n.t result, which in tests returns the key99// However, since the error has a .message, toErrorMessage returns that directly (line 86)100expect(result).toBe('File not found');101});102103it('handles ERR_UNC_HOST_NOT_ALLOWED error', () => {104const uncError: any = {105code: 'ERR_UNC_HOST_NOT_ALLOWED',106message: 'UNC host not allowed',107// Need to add stack for the error to go through exceptionToErrorMessage108stack: 'Error: UNC host not allowed\n at someFunction'109};110const result = toErrorMessage(uncError);111// With a stack, it goes through exceptionToErrorMessage -> detectSystemErrorMessage112expect(result).toContain('UNC host not allowed');113expect(result).toContain('security.allowedUNCHosts');114});115116it('filters out null/undefined values from error array', () => {117const errors = [null, undefined, new Error('Valid error'), null];118const result = toErrorMessage(errors);119expect(result).toBe('Valid error');120});121});122});123124125