Skip to content

Commit

Permalink
2.19.7 - Add default type to EmbeddedSDK.query (#2394)
Browse files Browse the repository at this point in the history
## What does this PR do ?

Add default types to `EmbeddedSDK.query` otherwise this result in an error:

```js
const { result } = await app.sdk.query(...);

result.anything; // error since "result" is of type "unknown"
```

### Other changes

 - fix bug happening when ES cannot find a resource who is not an index
 - fix bug when wrapping an error without stacktrace
  • Loading branch information
Adrien Maret authored Nov 4, 2022
1 parent 38bd409 commit f38bd79
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/core/shared/sdk/embeddedSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ export class EmbeddedSDK extends Kuzzle {
* @param request - API request (https://docs.kuzzle.io/core/2/guides/main-concepts/1-api#other-protocols)
* @param options - Optional arguments
*/
query<TRequest extends BaseRequest, TResult>(
query<
TRequest extends BaseRequest = BaseRequest,
TResult extends JSONObject = JSONObject
>(
request: TRequest,
options: { propagate?: boolean } = {}
): Promise<ResponsePayload<TResult>> {
Expand Down
2 changes: 1 addition & 1 deletion lib/kerror/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export function rawGetFrom(

// If a stacktrace is present, we need to modify the first line because it
// still contains the original error message
if (derivedError.stack && derivedError.stack.length) {
if (derivedError.stack && derivedError.stack.length && source.stack) {
const stackArray = source.stack.split("\n");
stackArray.shift();
derivedError.stack = [
Expand Down
4 changes: 4 additions & 0 deletions lib/service/storage/esWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ class ESWrapper {
_handleNotFoundError(error, message) {
let errorMessage = message;

if (!error.body._index) {
return kerror.get("unexpected_not_found", errorMessage);
}

// _index= "&nyc-open-data.yellow-taxi"
const index = error.body._index.split(".")[0].slice(1);
const collection = error.body._index.split(".")[1];
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kuzzle",
"author": "The Kuzzle Team <[email protected]>",
"version": "2.19.6",
"version": "2.19.7",
"description": "Kuzzle is an open-source solution that handles all the data management through a secured API, with a large choice of protocols.",
"bin": "bin/start-kuzzle-server",
"scripts": {
Expand All @@ -26,6 +26,7 @@
"test:functional:legacy:mqtt": "npx cucumber-js --format progress-bar --profile mqtt ./features-legacy",
"cucumber": "cucumber.js --fail-fast",
"codecov": "codecov",
"prettier": "npx prettier ./lib ./test ./bin ./features ./plugins/available/functional-test-plugin --write",
"test:lint": "npm run test:lint:js && npm run test:lint:ts",
"test:lint:ts": "eslint --max-warnings=0 ./lib --ext .ts --config .eslintc-ts.json",
"test:lint:ts:fix": "eslint --max-warnings=0 --fix ./lib --ext .ts --config .eslintc-ts.json",
Expand Down
20 changes: 20 additions & 0 deletions test/service/storage/esWrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ describe("Test: ElasticSearch Wrapper", () => {
});
});

it("should handle unexpected not found", () => {
const error = new Error("test");
error.meta = { statusCode: 404 };
error.body = {
found: false,
_id: "mehry",
error: {
reason: "foo",
"resource.id": "bar",
},
};

const formatted = esWrapper.formatESError(error);

should(formatted).be.match({
message: "test",
id: "services.storage.unexpected_not_found",
});
});

it("should handle unknown DSL keyword", () => {
const error = new Error("");
error.meta = {
Expand Down

0 comments on commit f38bd79

Please sign in to comment.