src/vs/base/common/errors.ts

357 LOC · 288 covered · 69 uncovered · 69 ranges · 20961 concepts · 24 introducers · 12741 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- map.ts ×97
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 > export interface ErrorListenerCallback {
7 > (error: any): void;
8 > }
9 >
10 > export interface ErrorListenerUnbind {
11 > (): void;
12 > }
13 >
14 > // Avoid circular dependency on EventEmitter by implementing a subset of the interface.
15 > export class ErrorHandler {
16 > private unexpectedErrorHandler: (e: any) => void;
17 > private listeners: ErrorListenerCallback[];
18 >
19 > constructor() {
20 >
21 > this.listeners = [];
22 >
23 > this.unexpectedErrorHandler = function (e: any) {
24 setTimeout(() => {
25 if (e.stack) {
26 if (ErrorNoTelemetry.isErrorNoTelemetry(e)) {
27 throw new ErrorNoTelemetry(e.message + '\n\n' + e.stack);
28 }
29
30 throw new Error(e.message + '\n\n' + e.stack);
31 }
32
33 throw e;
34 }, 0);
35 };
36 > } map.ts ×97
37 >
38 > addListener(listener: ErrorListenerCallback): ErrorListenerUnbind {
39 > this.listeners.push(listener); extensionHostMain.ts ×11
40 >
41 > return () => {
42 this._removeListener(listener);
43 };
46 > private emit(e: any): void {
47 > this.listeners.forEach((listener) => { errors.ts ×3
48 > listener(e); extensionHostMain.ts ×5
49 > }); errors.ts ×3
50 > }
52 > private _removeListener(listener: ErrorListenerCallback): void {
53 this.listeners.splice(this.listeners.indexOf(listener), 1);
54 }
56 > setUnexpectedErrorHandler(newUnexpectedErrorHandler: (e: any) => void): void {
57 > this.unexpectedErrorHandler = newUnexpectedErrorHandler;
58 > }
59 >
60 > getUnexpectedErrorHandler(): (e: any) => void {
61 > return this.unexpectedErrorHandler; errors.ts ×1
62 > }
64 > onUnexpectedError(e: any): void {
65 > this.unexpectedErrorHandler(e); errors.ts ×3
66 > this.emit(e);
67 > }
69 > // For external errors, we don't want the listeners to be called
70 > onUnexpectedExternalError(e: any): void {
71 this.unexpectedErrorHandler(e);
72 }
73 > } map.ts ×97
74 >
75 > export const errorHandler = new ErrorHandler();
76 >
77 > /** @skipMangle */
78 > export function setUnexpectedErrorHandler(newUnexpectedErrorHandler: (e: any) => void): void {
79 > errorHandler.setUnexpectedErrorHandler(newUnexpectedErrorHandler);
80 > }
81 >
82 > /**
83 > * Returns if the error is a SIGPIPE error. SIGPIPE errors should generally be
84 > * logged at most once, to avoid a loop.
85 > *
86 > * @see https://github.com/microsoft/vscode-remote-release/issues/6481
87 > */
88 > export function isSigPipeError(e: unknown): e is Error {
89 if (!e || typeof e !== 'object') {
90 return false;
91 }
92
93 const cast = e as Record<string, string | undefined>;
94 return cast.code === 'EPIPE' && cast.syscall?.toUpperCase() === 'WRITE';
95 }
97 > /**
98 > * This function should only be called with errors that indicate a bug in the product.
99 > * E.g. buggy extensions/invalid user-input/network issues should not be able to trigger this code path.
100 > * If they are, this indicates there is also a bug in the product.
101 > */
102 > export function onBugIndicatingError(e: any): undefined {
103 > errorHandler.onUnexpectedError(e); errors.ts ×1
104 > return undefined;
105 > }
106 > map.ts ×97
107 > export function onUnexpectedError(e: any): undefined {
108 > // ignore errors from cancelled promises errors.ts ×1
109 > if (!isCancellationError(e)) {
110 > errorHandler.onUnexpectedError(e);
111 > }
112 > return undefined;
113 > }
114 > map.ts ×97
115 > export function onUnexpectedExternalError(e: any): undefined {
116 // ignore errors from cancelled promises
117 if (!isCancellationError(e)) {
118 errorHandler.onUnexpectedExternalError(e);
119 }
120 return undefined;
121 }
122 > map.ts ×97
123 > type ObjectWithCode = {
124 > readonly code: unknown;
125 > };
126 >
127 > function hasErrorCode(error: object): error is ObjectWithCode { errors.ts ×3
128 > return Object.hasOwn(error, 'code');
129 > }
130 > map.ts ×97
131 > export function getErrorCode(error: unknown): string | undefined {
132 > if (!error || typeof error !== 'object' || !hasErrorCode(error)) { errors.ts ×3
133 > return undefined;
134 > }
135 > const code = error.code; errors.ts ×1
136 > return typeof code === 'string' || typeof code === 'number' ? String(code) : undefined; errors.ts ×3
137 > }
138 > map.ts ×97
139 > export interface SerializedError {
140 > readonly $isError: true;
141 > readonly name: string;
142 > readonly message: string;
143 > readonly stack: string;
144 > readonly noTelemetry: boolean;
145 > readonly code?: string;
146 > readonly cause?: SerializedError;
147 > }
148 >
149 > type ErrorWithCode = Error & {
150 > code: string | undefined;
151 > };
152 >
153 > export function transformErrorForSerialization(error: Error): SerializedError;
154 > export function transformErrorForSerialization(error: any): any;
155 > export function transformErrorForSerialization(error: any): any {
156 > if (error instanceof Error) { errors.ts ×1
157 > const { name, message, cause } = error; errors.ts ×2
158 > // eslint-disable-next-line local/code-no-any-casts
159 > const stack: string = (<any>error).stacktrace || (<any>error).stack;
160 > return {
161 > $isError: true,
162 > name,
163 > message,
164 > stack,
165 > noTelemetry: ErrorNoTelemetry.isErrorNoTelemetry(error),
166 > cause: cause ? transformErrorForSerialization(cause) : undefined,
167 > code: (<ErrorWithCode>error).code
168 > };
169 > }
171 > // return as is
172 > return error;
173 > }
174 > map.ts ×97
175 > export function transformErrorFromSerialization(data: SerializedError): Error {
176 > let error: Error; errors.ts ×4
177 > if (data.noTelemetry) {
178 error = new ErrorNoTelemetry();
179 > } else { errors.ts ×4
180 > error = new Error();
181 > error.name = data.name;
182 > }
183 > error.message = data.message;
184 > error.stack = data.stack;
185 > if (data.code) {
186 (<ErrorWithCode>error).code = data.code;
187 }
188 > if (data.cause) { errors.ts ×4
189 > error.cause = transformErrorFromSerialization(data.cause); types.ts ×2
190 > }
191 > return error; errors.ts ×4
192 > }
193 > map.ts ×97
194 > // see https://github.com/v8/v8/wiki/Stack%20Trace%20API#basic-stack-traces
195 > export interface V8CallSite {
196 > getThis(): unknown;
197 > getTypeName(): string | null;
198 > getFunction(): Function | undefined;
199 > getFunctionName(): string | null;
200 > getMethodName(): string | null;
201 > getFileName(): string | null;
202 > getLineNumber(): number | null;
203 > getColumnNumber(): number | null;
204 > getEvalOrigin(): string | undefined;
205 > isToplevel(): boolean;
206 > isEval(): boolean;
207 > isNative(): boolean;
208 > isConstructor(): boolean;
209 > toString(): string;
210 > }
211 >
212 > export const canceledName = 'Canceled';
213 >
214 > /**
215 > * Checks if the given error is a promise in canceled state
216 > */
217 > export function isCancellationError(error: any): boolean {
218 > if (error instanceof CancellationError) { errors.ts ×2
219 > return true; errors.ts ×1
220 > }
221 > return error instanceof Error && error.name === canceledName && error.message === canceledName; errors.ts ×2
222 > }
223 > map.ts ×97
224 > // !!!IMPORTANT!!!
225 > // Do NOT change this class because it is also used as an API-type.
226 > export class CancellationError extends Error {
227 > constructor() {
228 > super(canceledName); errors.ts ×1
229 > this.name = this.message;
230 > }
231 > } map.ts ×97
232 >
233 > export class PendingMigrationError extends Error {
234 >
235 > private static readonly _name = 'PendingMigrationError';
236 >
237 > static is(error: unknown): error is PendingMigrationError {
238 > return error instanceof PendingMigrationError || (error instanceof Error && error.name === PendingMigrationError._name); extensionHostMain.ts ×5
239 > }
240 > map.ts ×97
241 > constructor(message: string) {
242 super(message);
243 this.name = PendingMigrationError._name;
244 }
245 > } map.ts ×97
246 >
247 > /**
248 > * @deprecated use {@link CancellationError `new CancellationError()`} instead
249 > */
250 > export function canceled(): Error {
251 > const error = new Error(canceledName); errors.ts ×1
252 > error.name = error.message;
253 > return error;
254 > }
255 > map.ts ×97
256 > export function illegalArgument(name?: string): Error {
257 if (name) {
258 return new Error(`Illegal argument: ${name}`);
259 } else {
260 return new Error('Illegal argument');
261 }
262 }
263 > map.ts ×97
264 > export function illegalState(name?: string): Error {
265 > if (name) { errors.ts ×2
266 > return new Error(`Illegal state: ${name}`);
267 > } else {
268 return new Error('Illegal state');
269 }
270 > } errors.ts ×2
271 > map.ts ×97
272 > export class ReadonlyError extends TypeError {
273 > constructor(name?: string) {
274 > super(name ? `${name} is read-only and cannot be changed` : 'Cannot change read-only property'); errors.ts ×1
275 > }
276 > } map.ts ×97
277 >
278 > export function getErrorMessage(err: any): string {
279 > if (!err) { errors.ts ×2
280 return 'Error';
281 }
283 > if (err.message) {
284 > return err.message;
285 > }
287 > if (err.stack) {
288 return err.stack.split('\n')[0];
289 }
291 > return String(err);
292 > }
293 > map.ts ×97
294 > export class NotImplementedError extends Error {
295 > constructor(message?: string) {
296 super('NotImplemented');
297 if (message) {
298 this.message = message;
299 }
300 }
301 > } map.ts ×97
302 >
303 > export class NotSupportedError extends Error {
304 > constructor(message?: string) {
305 super('NotSupported');
306 if (message) {
307 this.message = message;
308 }
309 }
310 > } map.ts ×97
311 >
312 > export class ExpectedError extends Error {
313 readonly isExpected = true;
314 > } map.ts ×97
315 >
316 > /**
317 > * Error that when thrown won't be logged in telemetry as an unhandled error.
318 > */
319 > export class ErrorNoTelemetry extends Error {
320 > override readonly name: string;
321 >
322 > constructor(msg?: string) {
323 > super(msg); fileService.ts ×2
324 > this.name = 'CodeExpectedError';
325 > }
326 > map.ts ×97
327 > public static fromError(err: Error): ErrorNoTelemetry {
328 if (err instanceof ErrorNoTelemetry) {
329 return err;
330 }
331
332 const result = new ErrorNoTelemetry();
333 result.message = err.message;
334 result.stack = err.stack;
335 return result;
336 }
337 > map.ts ×97
338 > public static isErrorNoTelemetry(err: Error): err is ErrorNoTelemetry {
339 > return err.name === 'CodeExpectedError'; errors.ts ×2
340 > }
341 > } map.ts ×97
342 >
343 > /**
344 > * This error indicates a bug.
345 > * Do not throw this for invalid user input.
346 > * Only catch this error to recover gracefully from bugs.
347 > */
348 > export class BugIndicatingError extends Error {
349 > constructor(message?: string) {
350 > super(message || 'An unexpected bug occurred.'); errors.ts ×1
351 > Object.setPrototypeOf(this, BugIndicatingError.prototype);
352 >
353 > // Because we know for sure only buggy code throws this,
354 > // we definitely want to break here and fix the bug.
355 > // debugger;
356 > }
357 > } map.ts ×97