Skip to content

Commit 88eb4fe

Browse files
Adding fix for response JSON parse error (#114)
* Adding fix for response JSON parse error Signed-off-by: Prashant Shahi <[email protected]>
1 parent a4bd726 commit 88eb4fe

File tree

8 files changed

+417
-199
lines changed

8 files changed

+417
-199
lines changed

generated/api_pb.js

Lines changed: 384 additions & 175 deletions
Large diffs are not rendered by default.

lib/txn.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export declare class Txn {
1818
queryWithVars(q: string, vars?: {
1919
[k: string]: any;
2020
}, metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<types.Response>;
21-
mutate(mu: types.Mutation, metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<messages.Response>;
22-
doRequest(req: messages.Request, metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<messages.Response>;
21+
mutate(mu: types.Mutation, metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<types.Response>;
22+
doRequest(req: messages.Request, metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<types.Response>;
2323
commit(metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<void>;
2424
discard(metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<void>;
2525
private mergeContext;

lib/types.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ var Payload = (function (_super) {
3030
else {
3131
jsonStr = util_1.b64ToStr(value);
3232
}
33-
try {
34-
return JSON.parse(jsonStr);
35-
}
36-
catch (e) {
37-
return jsonStr;
38-
}
33+
return util_1.strToJson(jsonStr);
3934
};
4035
Payload.prototype.getData_asB64 = function () {
4136
var value = _super.prototype.getData.call(this);
@@ -84,7 +79,7 @@ var Response = (function (_super) {
8479
else {
8580
jsonStr = util_1.b64ToStr(value);
8681
}
87-
return JSON.parse(jsonStr);
82+
return util_1.strToJson(jsonStr);
8883
};
8984
Response.prototype.getJson_asB64 = function () {
9085
var value = _super.prototype.getJson.call(this);
@@ -133,7 +128,7 @@ var Mutation = (function (_super) {
133128
else {
134129
jsonStr = util_1.b64ToStr(value);
135130
}
136-
return JSON.parse(jsonStr);
131+
return util_1.strToJson(jsonStr);
137132
};
138133
Mutation.prototype.getSetJson_asB64 = function () {
139134
var value = _super.prototype.getSetJson.call(this);
@@ -170,7 +165,7 @@ var Mutation = (function (_super) {
170165
else {
171166
jsonStr = util_1.b64ToStr(value);
172167
}
173-
return JSON.parse(jsonStr);
168+
return util_1.strToJson(jsonStr);
174169
};
175170
Mutation.prototype.getDeleteJson_asB64 = function () {
176171
var value = _super.prototype.getDeleteJson.call(this);

lib/util.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export declare function strToB64(str: string): string;
1414
export declare function strToU8(str: string): Uint8Array;
1515
export declare function b64ToStr(b64Str: string): string;
1616
export declare function u8ToStr(arr: Uint8Array): string;
17+
export declare function strToJson(jsonStr: string): any;

lib/util.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@ function u8ToStr(arr) {
7474
return buf.toString();
7575
}
7676
exports.u8ToStr = u8ToStr;
77+
function strToJson(jsonStr) {
78+
try {
79+
return JSON.parse(jsonStr);
80+
}
81+
catch (e) {
82+
return {};
83+
}
84+
}
85+
exports.strToJson = strToJson;

src/txn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class Txn {
106106
* operations on it will fail.
107107
*/
108108
public async mutate(
109-
mu: types.Mutation, metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<messages.Response> {
109+
mu: types.Mutation, metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<types.Response> {
110110

111111
const req = new messages.Request();
112112
req.setStartTs(this.ctx.getStartTs());
@@ -117,7 +117,7 @@ export class Txn {
117117
}
118118

119119
public async doRequest(
120-
req: messages.Request, metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<messages.Response> {
120+
req: messages.Request, metadata?: grpc.Metadata, options?: grpc.CallOptions): Promise<types.Response> {
121121
const mutationList = req.getMutationsList();
122122
if (this.finished) {
123123
this.dc.debug(`Do request (ERR_FINISHED):\nquery = ${req.getQuery()}\nvars = ${req.getVarsMap()}`);

src/types.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as jspb from "google-protobuf";
22

33
import * as messages from "../generated/api_pb";
44

5-
import { b64ToStr, isBase64, strToB64, strToU8, u8ToStr } from "./util";
5+
import { b64ToStr, isBase64, strToB64, strToJson, strToU8, u8ToStr } from "./util";
66

77
// tslint:disable max-classes-per-file
88

@@ -21,11 +21,7 @@ export class Payload extends messages.Payload {
2121
jsonStr = b64ToStr(value);
2222
}
2323

24-
try {
25-
return JSON.parse(jsonStr);
26-
} catch (e) {
27-
return jsonStr;
28-
}
24+
return strToJson(jsonStr);
2925
}
3026

3127
public getData_asB64(): string {
@@ -68,10 +64,10 @@ export function createPayload(oldPayload: messages.Payload): Payload {
6864
);
6965
}
7066

71-
// Query classes.
67+
// Mutation and Query classes.
7268

7369
/**
74-
* Response represents the return value of a query operation.
70+
* Response represents the return value of a mutation or query operations.
7571
*/
7672
export class Response extends messages.Response {
7773
public getJson(): any { // tslint:disable-line no-any
@@ -83,7 +79,7 @@ export class Response extends messages.Response {
8379
jsonStr = b64ToStr(value);
8480
}
8581

86-
return JSON.parse(jsonStr);
82+
return strToJson(jsonStr);
8783
}
8884

8985
public getJson_asB64(): string {
@@ -141,7 +137,7 @@ export class Mutation extends messages.Mutation {
141137
jsonStr = b64ToStr(value);
142138
}
143139

144-
return JSON.parse(jsonStr);
140+
return strToJson(jsonStr);
145141
}
146142

147143
public getSetJson_asB64(): string {
@@ -185,7 +181,7 @@ export class Mutation extends messages.Mutation {
185181
jsonStr = b64ToStr(value);
186182
}
187183

188-
return JSON.parse(jsonStr);
184+
return strToJson(jsonStr);
189185
}
190186

191187
public getDeleteJson_asB64(): string {

src/util.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,11 @@ export function u8ToStr(arr: Uint8Array): string {
9696

9797
return buf.toString();
9898
}
99+
100+
export function strToJson(jsonStr: string): any { // tslint:disable-line no-any
101+
try {
102+
return JSON.parse(jsonStr);
103+
} catch (e) {
104+
return {};
105+
}
106+
}

0 commit comments

Comments
 (0)