Skip to content

Commit 4d6dbd6

Browse files
committed
Removed all "exitCode !== 0 throw Error" (See #140)
1 parent 03d2531 commit 4d6dbd6

File tree

5 files changed

+80
-76
lines changed

5 files changed

+80
-76
lines changed

src/svn.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ export enum PropStatus {
3232
NORMAL = "normal"
3333
}
3434

35+
export const SvnErrorCodes: { [key: string]: string } = {
36+
RepositoryIsLocked: "E155004",
37+
NotASvnRepository: "E155007"
38+
};
39+
40+
function getSvnErrorCode(stderr: string): string | undefined {
41+
for (const name in SvnErrorCodes) {
42+
const code = SvnErrorCodes[name];
43+
const regex = new RegExp(`svn: ${code}`);
44+
if (regex.test(stderr)) {
45+
return code;
46+
}
47+
}
48+
49+
return void 0;
50+
}
51+
3552
export interface CpOptions {
3653
cwd?: string;
3754
encoding?: string;
@@ -53,6 +70,12 @@ export interface ISvnOptions {
5370
version: string;
5471
}
5572

73+
export interface IExecutionResult {
74+
exitCode: number;
75+
stdout: string;
76+
stderr: string;
77+
}
78+
5679
export function cpErrorHandler(
5780
cb: (reason?: any) => void
5881
): (reason?: any) => void {
@@ -137,7 +160,11 @@ export class Svn {
137160
this._onOutput.emit("log", output);
138161
}
139162

140-
async exec(cwd: string, args: any[], options: CpOptions = {}) {
163+
async exec(
164+
cwd: string,
165+
args: any[],
166+
options: CpOptions = {}
167+
): Promise<IExecutionResult> {
141168
if (cwd) {
142169
this.lastCwd = cwd;
143170
options.cwd = cwd;
@@ -192,6 +219,19 @@ export class Svn {
192219
this.logOutput(`${stderr}\n`);
193220
}
194221

222+
if (exitCode) {
223+
return Promise.reject<IExecutionResult>(
224+
new SvnError({
225+
message: "Failed to execute git",
226+
stdout: stdout,
227+
stderr: stderr,
228+
exitCode: exitCode,
229+
svnErrorCode: getSvnErrorCode(stderr),
230+
svnCommand: args[0]
231+
})
232+
);
233+
}
234+
195235
return { exitCode, stdout, stderr };
196236
}
197237

src/svnRepository.ts

Lines changed: 30 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,18 @@ export class Repository {
5353
): Promise<string> {
5454
const result = await this.svn.show(path, revision, options);
5555

56-
if (result.exitCode !== 0) {
57-
throw new Error(result.stderr);
58-
}
59-
6056
return result.stdout;
6157
}
6258

6359
async commitFiles(message: string, files: any[]) {
6460
const result = await this.svn.commit(message, files);
6561

66-
if (result.exitCode !== 0) {
67-
throw new Error(result.stderr);
62+
const matches = result.stdout.match(/Committed revision (.*)\./i);
63+
if (matches && matches[0]) {
64+
return matches[0];
6865
}
6966

70-
const outputMessage = result.stdout.match(/Committed revision (.*)\./i)[0];
71-
72-
return outputMessage;
67+
return result.stdout;
7368
}
7469

7570
addFile(filePath: string) {
@@ -149,18 +144,18 @@ export class Repository {
149144
if (trunkLayout) {
150145
promises.push(
151146
new Promise<string[]>(async resolve => {
152-
let trunkExists = await this.svn.exec("", [
153-
"ls",
154-
repoUrl + "/" + trunkLayout,
155-
"--depth",
156-
"empty"
157-
]);
158-
159-
if (trunkExists.exitCode === 0) {
147+
try {
148+
let trunkExists = await this.svn.exec("", [
149+
"ls",
150+
repoUrl + "/" + trunkLayout,
151+
"--depth",
152+
"empty"
153+
]);
154+
160155
resolve([trunkLayout]);
161-
return;
156+
} catch (error) {
157+
resolve([]);
162158
}
163-
resolve([]);
164159
})
165160
);
166161
}
@@ -180,21 +175,20 @@ export class Repository {
180175
new Promise<string[]>(async resolve => {
181176
const branchUrl = repoUrl + "/" + tree;
182177

183-
const result = await this.svn.list(branchUrl);
178+
try {
179+
const result = await this.svn.list(branchUrl);
184180

185-
if (result.exitCode !== 0) {
181+
const list = result.stdout
182+
.trim()
183+
.replace(/\/|\\/g, "")
184+
.split(/[\r\n]+/)
185+
.filter((x: string) => !!x)
186+
.map((i: string) => tree + "/" + i);
187+
188+
resolve(list);
189+
} catch (error) {
186190
resolve([]);
187-
return;
188191
}
189-
190-
const list = result.stdout
191-
.trim()
192-
.replace(/\/|\\/g, "")
193-
.split(/[\r\n]+/)
194-
.filter((x: string) => !!x)
195-
.map((i: string) => tree + "/" + i);
196-
197-
resolve(list);
198192
})
199193
);
200194
}
@@ -226,10 +220,6 @@ export class Repository {
226220
newBranch
227221
);
228222

229-
if (switchBranch.exitCode !== 0) {
230-
throw new Error(switchBranch.stderr);
231-
}
232-
233223
this.resetInfo();
234224

235225
return true;
@@ -245,10 +235,6 @@ export class Repository {
245235
branchUrl
246236
);
247237

248-
if (switchBranch.exitCode !== 0) {
249-
throw new Error(switchBranch.stderr);
250-
}
251-
252238
this.resetInfo();
253239

254240
return true;
@@ -257,35 +243,27 @@ export class Repository {
257243
async revert(files: any[]) {
258244
const result = await this.svn.revert(files);
259245

260-
if (result.exitCode !== 0) {
261-
throw new Error(result.stderr);
262-
}
263-
264246
return result.stdout;
265247
}
266248

267-
async update() {
249+
async update(): Promise<string> {
268250
const result = await this.svn.update(this.workspaceRoot);
269251

270-
if (result.exitCode !== 0) {
271-
throw new Error(result.stderr);
272-
}
273-
274252
this.resetInfo();
275253

276254
const message = result.stdout
277255
.trim()
278256
.split(/\r?\n/)
279257
.pop();
280258

281-
return message;
259+
if (message) {
260+
return message;
261+
}
262+
return result.stdout;
282263
}
283264

284265
async patch() {
285266
const result = await this.svn.patch(this.workspaceRoot);
286-
if (result.exitCode !== 0) {
287-
throw new Error(result.stderr);
288-
}
289267

290268
const message = result.stdout;
291269
return message;
@@ -294,20 +272,12 @@ export class Repository {
294272
async removeFiles(files: any[], keepLocal: boolean) {
295273
const result = await this.svn.remove(files, keepLocal);
296274

297-
if (result.exitCode !== 0) {
298-
throw new Error(result.stderr);
299-
}
300-
301275
return result.stdout;
302276
}
303277

304278
async resolve(file: string, action: string) {
305279
const result = await this.svn.resolve(file, action);
306280

307-
if (result.exitCode !== 0) {
308-
throw new Error(result.stderr);
309-
}
310-
311281
return result.stdout;
312282
}
313283

@@ -316,10 +286,6 @@ export class Repository {
316286
const logLength = config.get<string>("log.length") || "50";
317287
const result = await this.svn.log(this.workspaceRoot, logLength);
318288

319-
if (result.exitCode !== 0) {
320-
throw new Error(result.stderr);
321-
}
322-
323289
return result.stdout;
324290
}
325291

@@ -332,10 +298,6 @@ export class Repository {
332298
"--xml"
333299
]);
334300

335-
if (result.exitCode !== 0) {
336-
return 0;
337-
}
338-
339301
const matches = result.stdout.match(/<logentry/g);
340302

341303
if (matches && matches.length > 0) {

src/test/repository.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Svn } from "../svn";
1717
import { Model } from "../model";
1818
import { SvnFinder, ISvn } from "../svnFinder";
1919
import { Repository } from "../repository";
20+
import { timeout } from "../util";
2021

2122
// Defines a Mocha test suite to group tests of similar kind together
2223
suite("Repository Tests", () => {
@@ -104,7 +105,7 @@ suite("Repository Tests", () => {
104105
await repository.addFile(file);
105106

106107
await repository.update();
107-
await testUtil.delay(1500); // Wait the debounce time
108+
await timeout(1500); // Wait the debounce time
108109
assert.equal(repository.changes.resourceStates.length, 1);
109110

110111
const message = await repository.repository.commitFiles("First Commit", [
@@ -113,7 +114,7 @@ suite("Repository Tests", () => {
113114
assert.ok(/Committed revision (.*)\./i.test(message));
114115

115116
await repository.update();
116-
await testUtil.delay(1500); // Wait the debounce time
117+
await timeout(1500); // Wait the debounce time
117118
assert.equal(repository.changes.resourceStates.length, 0);
118119

119120
const remoteContent = await repository.show(file, "HEAD");

src/test/testUtil.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from "fs";
44
import * as os from "os";
55
import { Uri } from "vscode";
66
import { SpawnOptions, ChildProcess } from "child_process";
7+
import { timeout } from "../util";
78

89
const tempDir = os.tmpdir();
910
let tempDirList: string[] = [];
@@ -14,10 +15,6 @@ export function getSvnUrl(uri: Uri) {
1415
return url.replace(/%3A/g, ":");
1516
}
1617

17-
export function delay(ms: number) {
18-
return new Promise(resolve => setTimeout(resolve, ms));
19-
}
20-
2118
export function spawn(
2219
command: string,
2320
args?: string[],
@@ -151,7 +148,7 @@ export async function destroyPath(fullPath: string) {
151148
fs.rmdirSync(fullPath);
152149
break;
153150
} catch (error) {
154-
await delay(3000);
151+
await timeout(3000);
155152
console.error(error);
156153
}
157154
}

src/util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,7 @@ export function camelcase(name: string) {
9797
export function hasSupportToDecorationProvider() {
9898
return typeof window.registerDecorationProvider === "function";
9999
}
100+
101+
export function timeout(ms: number) {
102+
return new Promise(resolve => setTimeout(resolve, ms));
103+
}

0 commit comments

Comments
 (0)