Skip to content

Commit beba882

Browse files
author
Chris Wiechmann
committed
No need to try/catch
1 parent cf4680b commit beba882

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

api-builder-plugin-fn-elasticsearch/src/actions/ElasticsearchClient.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,41 @@ class ElasticsearchClient {
2121

2222
function mockElasticsearchMethod(client, methodName, responeFilename, shouldError) {
2323
var mockedFn = simple.spy(function (params, options, callback) {
24-
const resonse = JSON.parse(fs.readFileSync(responeFilename), null);
24+
var response;
25+
if(responeFilename) {
26+
response = JSON.parse(fs.readFileSync(responeFilename), null);
27+
} else {
28+
response = {dummy:"response"};
29+
}
2530
if(callback != undefined) {
2631
if(shouldError) {
2732
// Return the given response as an error
28-
callback(resonse, null);
33+
callback(response, null);
2934
} else {
3035
// Otherwise return the response as expected
31-
callback(null, resonse);
36+
callback(null, response);
3237
}
3338
} else {
3439
return new Promise((resolve, reject) => {
3540
if(shouldError) {
36-
reject(resonse);
41+
reject(response);
3742
} else {
38-
resolve(resonse);
43+
resolve(response);
3944
}
4045
});
4146
}
4247
});
43-
// Use the extend functionality of the ES-Client to register the mocked method
44-
client.extend(methodName, { force: true }, ({ makeRequest }) => {
45-
return mockedFn;
46-
});
48+
49+
let [namespace, method] = methodName.split('.');
50+
if (method == null) {
51+
method = namespace;
52+
namespace = null;
53+
}
54+
if (namespace != null) {
55+
client[namespace][method] = mockedFn
56+
} else {
57+
client[method] = mockedFn;
58+
}
4759
// Return the mocked function to perform assertions
4860
return mockedFn;
4961
}

api-builder-plugin-fn-elasticsearch/src/actions/search.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,7 @@ async function search(params, options) {
7878
addQueryParam("version");
7979

8080
options.logger.debug(`Using elastic search body: ${JSON.stringify(searchBody)}`);
81-
82-
var queryResult;
83-
try {
84-
queryResult = await client.search( searchBody, { ignore: [404], maxRetries: 3 });
85-
} catch (ex) {
86-
if(ex instanceof Error) throw ex;
87-
throw new Error(JSON.stringify(ex));
88-
}
81+
var queryResult = await client.search( searchBody, { ignore: [404], maxRetries: 3 });
8982

9083
if(queryResult.status === 404 && queryResult.error.type == "index_not_found_exception") {
9184
return options.setOutput('missingIndex', queryResult);

0 commit comments

Comments
 (0)