Skip to content

Commit d8efcfc

Browse files
committed
improve error logs for json based patches.
1 parent 67e2a57 commit d8efcfc

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

Common/Node/jsonPatcher.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import patch = require('./patch');
2-
2+
import tl = require('vsts-task-lib/task');
33
import jsonPatch = require('fast-json-patch');
44

55
export class JsonPatcher implements patch.IPatcher {
@@ -21,6 +21,10 @@ export class JsonPatcher implements patch.IPatcher {
2121
var patchError = jsonPatch.validate(this.patches, json);
2222

2323
if(patchError){
24+
tl.warning("Invalid patch at index `" + String(patchError.index) + "`");
25+
tl.warning(patch.SlickPatchParser.stringify(patchError.operation));
26+
tl.warning(patchError.name);
27+
tl.warning(patchError.message);
2428
throw new Error("Invalid patch at index `" + String(patchError.index) + "`: " + patchError.name
2529
+ ", " + patchError.message);
2630
}

Common/Node/patch.ts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,69 @@ export interface IPatcher {
77
}
88

99
export class SlickPatchParser {
10+
11+
static stringify(operation: Operation): string {
12+
if (operation.op == "add") {
13+
return "+ " + operation.path + " => " + JSON.stringify(operation.value);
14+
} else if (operation.op == "remove") {
15+
return "- " + operation.path;
16+
} else if (operation.op == "replace") {
17+
return "= " + operation.path + " => " + JSON.stringify(operation.value);
18+
} else if (operation.op == "copy") {
19+
return "+ " + operation.from + " => " + operation.path;
20+
} else if (operation.op == "move") {
21+
return "+ " + operation.from + " => " + operation.path;
22+
} else if (operation.op == "test") {
23+
return "+ " + operation.path + " => " + JSON.stringify(operation.value);
24+
}
25+
return "Unknown operation";
26+
}
27+
1028
parse(sourcePatch: string): Operation[] {
1129
var result: Operation[] = [];
1230

13-
XRegExp.forEach(sourcePatch, XRegExp('^\\s*(?<op>\\+|-|=|&|>|\\?)\\s*(?<path>.*?)\\s*(=>\\s*(?<value>.*))?$','gm'), (match) => {
31+
XRegExp.forEach(sourcePatch, XRegExp('^\\s*(?<op>\\+|-|=|&|>|\\?)\\s*(?<path>.*?)\\s*(=>\\s*(?<value>.*))?$', 'gm'), (match) => {
1432
var op = (<any>match).op;
15-
if (op == "+"){
33+
if (op == "+") {
1634
result.push({
17-
op : "add",
18-
path : (<any>match).path,
19-
value : JSON.parse((<any>match).value)
35+
op: "add",
36+
path: (<any>match).path,
37+
value: JSON.parse((<any>match).value)
2038
});
21-
} else if (op == "-") {
39+
} else if (op == "-") {
2240
result.push({
23-
op : "remove",
24-
path : (<any>match).path
41+
op: "remove",
42+
path: (<any>match).path
2543
});
26-
} else if (op == "=") {
44+
} else if (op == "=") {
2745
result.push({
28-
op : "replace",
29-
path : (<any>match).path,
30-
value : JSON.parse((<any>match).value)
46+
op: "replace",
47+
path: (<any>match).path,
48+
value: JSON.parse((<any>match).value)
3149
});
32-
} else if (op == "&") {
50+
} else if (op == "&") {
3351
result.push({
34-
op : "copy",
35-
path : (<any>match).value,
36-
from : (<any>match).path
52+
op: "copy",
53+
path: (<any>match).value,
54+
from: (<any>match).path
3755
});
38-
} else if (op == ">") {
56+
} else if (op == ">") {
3957
result.push({
40-
op : "move",
41-
path : (<any>match).value,
42-
from : (<any>match).path
58+
op: "move",
59+
path: (<any>match).value,
60+
from: (<any>match).path
4361
});
44-
} else if (op == "?") {
62+
} else if (op == "?") {
4563
result.push({
46-
op : "test",
47-
path : (<any>match).path,
48-
value : JSON.parse((<any>match).value)
64+
op: "test",
65+
path: (<any>match).path,
66+
value: JSON.parse((<any>match).value)
4967
});
5068
} else {
5169
throw new Error("operator " + op + " is no supported.");
5270
}
5371
});
5472

55-
return result;
73+
return result;
5674
}
5775
}

Common/Node/patchProcess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function apply(patcher: patch.IPatcher, workingDirectory: string, filters
6161
filePatched++;
6262
}
6363
catch (err) {
64-
tl.warning(String(err));
64+
tl.debug(String(err));
6565
tl.warning("Couldn't apply patch to file: " + file);
6666
errors++;
6767
}

0 commit comments

Comments
 (0)