forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
142 lines (124 loc) · 3.92 KB
/
errors.ts
File metadata and controls
142 lines (124 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as vscode from 'vscode';
/*
See:
+ https://nodejs.org/api/errors.html
+ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
+ node_modules/@types/node/globals.d.ts
*/
interface IError {
name: string;
message: string;
toString(): string;
}
interface INodeJSError extends IError {
code: string;
stack?: string;
stackTraceLimit: number;
captureStackTrace(): void;
}
//================================
// "system" errors
namespace vscErrors {
const FILE_NOT_FOUND = vscode.FileSystemError.FileNotFound().name;
const FILE_EXISTS = vscode.FileSystemError.FileExists().name;
const IS_DIR = vscode.FileSystemError.FileIsADirectory().name;
const NOT_DIR = vscode.FileSystemError.FileNotADirectory().name;
const NO_PERM = vscode.FileSystemError.NoPermissions().name;
const known = [
// (order does not matter)
FILE_NOT_FOUND,
FILE_EXISTS,
IS_DIR,
NOT_DIR,
NO_PERM,
];
function errorMatches(err: Error, expectedName: string): boolean | undefined {
if (!known.includes(err.name)) {
return undefined;
}
return err.name === expectedName;
}
export function isFileNotFound(err: Error): boolean | undefined {
return errorMatches(err, FILE_NOT_FOUND);
}
export function isFileExists(err: Error): boolean | undefined {
return errorMatches(err, FILE_EXISTS);
}
export function isFileIsDir(err: Error): boolean | undefined {
return errorMatches(err, IS_DIR);
}
export function isNotDir(err: Error): boolean | undefined {
return errorMatches(err, NOT_DIR);
}
export function isNoPermissions(err: Error): boolean | undefined {
return errorMatches(err, NO_PERM);
}
}
interface ISystemError extends INodeJSError {
errno: number;
syscall: string;
info?: string;
path?: string;
address?: string;
dest?: string;
port?: string;
}
// Return a new error for errno ENOTEMPTY.
export function createDirNotEmptyError(dirname: string): ISystemError {
const err = new Error(`directory "${dirname}" not empty`) as ISystemError;
err.name = 'SystemError';
err.code = 'ENOTEMPTY';
err.path = dirname;
err.syscall = 'rmdir';
return err;
}
function isSystemError(err: Error, expectedCode: string): boolean | undefined {
const code = (err as ISystemError).code;
if (!code) {
return undefined;
}
return code === expectedCode;
}
// Return true if the given error is ENOENT.
export function isFileNotFoundError(err: Error): boolean | undefined {
const matched = vscErrors.isFileNotFound(err);
if (matched !== undefined) {
return matched;
}
return isSystemError(err, 'ENOENT');
}
// Return true if the given error is EEXIST.
export function isFileExistsError(err: Error): boolean | undefined {
const matched = vscErrors.isFileExists(err);
if (matched !== undefined) {
return matched;
}
return isSystemError(err, 'EEXIST');
}
// Return true if the given error is EISDIR.
export function isFileIsDirError(err: Error): boolean | undefined {
const matched = vscErrors.isFileIsDir(err);
if (matched !== undefined) {
return matched;
}
return isSystemError(err, 'EISDIR');
}
// Return true if the given error is ENOTDIR.
export function isNotDirError(err: Error): boolean | undefined {
const matched = vscErrors.isNotDir(err);
if (matched !== undefined) {
return matched;
}
return isSystemError(err, 'ENOTDIR');
}
// Return true if the given error is EACCES.
export function isNoPermissionsError(err: Error): boolean | undefined {
const matched = vscErrors.isNoPermissions(err);
if (matched !== undefined) {
return matched;
}
return isSystemError(err, 'EACCES');
}