Skip to content

Commit

Permalink
fix(mimic-thrift): fixed incorrect exception encoding in response
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-ladnik-ck committed Dec 8, 2018
1 parent 911a69d commit 83e02fd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
5 changes: 1 addition & 4 deletions packages/mimic-thrift/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ export function createClient(service: ThriftFile.IService, ns: any) {
return callback(result[key]);
}
}
if (success !== null) {
return callback(null, success);
}
return callback(new Error("request failed: unknown result"));
return callback(null, success);
};
});
return client;
Expand Down
27 changes: 18 additions & 9 deletions packages/mimic-thrift/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
toCallback,
writeConfig,
} from "@creditkarma/mimic-core";
import { buildServiceAPI, formatThrift, generateThriftResponse, ThriftFile, ThriftParser } from "./index";
import { buildServiceAPI, formatThrift, ThriftFile, ThriftParser } from "./index";

// Thrift Service definition
export interface IThriftServiceJson extends IServiceJson {
Expand Down Expand Up @@ -246,7 +246,6 @@ export class ThriftProvider extends EventEmitter implements IServiceProvider, IC
*/
private handler = (params: IThriftServiceJson) => {
const {id, proxy, remoteHost, remotePort, url} = params;
const def = this.thrift[id];
return (func: ThriftFile.IFunction, args: any, callback: (data: any, excep?: string) => void) => {
if (proxy) {
const request = {id, host: remoteHost!, port: remotePort!, path: url,
Expand All @@ -264,13 +263,7 @@ export class ThriftProvider extends EventEmitter implements IServiceProvider, IC
}
});
} else {
const { data, exception } = this.respManager.find(id)[func.name] || {
data: new Thrift.TApplicationException(
Thrift.TApplicationExceptionType.MISSING_RESULT,
`Mimic: no method found named:${func.name}`),
exception: undefined,
};

const { data, exception } = this.respManager.find(id)[func.name] || this.missingResult(func);
// Return data
callback(data, exception);
// Emit request
Expand All @@ -279,6 +272,22 @@ export class ThriftProvider extends EventEmitter implements IServiceProvider, IC
};
}

/**
* Generate an error when response is expected, but not set
*/
private missingResult = (func: ThriftFile.IFunction) => {
if (func.returnTypeId === "void") {
return {data: null, exception: null};
}
return {
data: new Thrift.TApplicationException(
Thrift.TApplicationExceptionType.MISSING_RESULT,
`Mimic: no data found for function '${func.name}'`,
),
exception: "MISSING_RESULT",
};
}

/**
* Format and emit request
*/
Expand Down
11 changes: 9 additions & 2 deletions packages/mimic-thrift/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,16 @@ function addProcessFunc(f: ThriftFile.IFunction, service: string, ns: any, def:
args.read(input);
input.readMessageEnd();
this._handler(f, args, (data: any, exception?: string) => {
let resultObj: any;
if (f.oneway) { return; }
const resultObj = new ns[`${service}_${f.name}_result`](exception ? ns[exception](data) : {success: data});
output.writeMessageBegin(f.name, Thrift.MessageType.REPLY, seqid);
if (data instanceof Thrift.TApplicationException) {
resultObj = data;
output.writeMessageBegin(f.name, Thrift.MessageType.EXCEPTION, seqid);
} else {
const obj = exception ? new ns[exception](data) : {success: data};
resultObj = new ns[`${service}_${f.name}_result`](obj);
output.writeMessageBegin(f.name, Thrift.MessageType.REPLY, seqid);
}
resultObj.write(output);
output.writeMessageEnd();
output.flush();
Expand Down

0 comments on commit 83e02fd

Please sign in to comment.