Skip to content

Commit 3433560

Browse files
authored
fix: add modelId and used index name to the log (#4186)
1 parent d79172a commit 3433560

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import WebinyError from "@webiny/error";
2+
import { CmsModel } from "@webiny/api-headless-cms/types";
3+
4+
interface LogIgnoredElasticsearchExceptionParams {
5+
error: WebinyError;
6+
model: CmsModel;
7+
indexName: string;
8+
}
9+
10+
export const logIgnoredEsResponseError = (params: LogIgnoredElasticsearchExceptionParams) => {
11+
const { error, indexName, model } = params;
12+
13+
console.log(`Ignoring Elasticsearch response error: ${error.message}`, {
14+
modelId: model.modelId,
15+
usedIndexName: indexName,
16+
error: {
17+
message: error.message,
18+
code: error.code,
19+
data: error.data,
20+
stack: error.stack
21+
}
22+
});
23+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import WebinyError from "@webiny/error";
2+
3+
const IGNORED_ES_SEARCH_EXCEPTIONS = [
4+
"index_not_found_exception",
5+
"search_phase_execution_exception"
6+
];
7+
8+
export const shouldIgnoreEsResponseError = (error: WebinyError) => {
9+
return IGNORED_ES_SEARCH_EXCEPTIONS.includes(error.message);
10+
};

packages/api-headless-cms-ddb-es/src/operations/entry/index.ts

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import {
4040
} from "@webiny/api-elasticsearch/types";
4141
import { CmsEntryStorageOperations, CmsIndexEntry } from "~/types";
4242
import { createElasticsearchBody } from "./elasticsearch/body";
43+
import { logIgnoredEsResponseError } from "./elasticsearch/logIgnoredEsResponseError";
44+
import { shouldIgnoreEsResponseError } from "./elasticsearch/shouldIgnoreEsResponseError";
4345
import { createLatestRecordType, createPublishedRecordType, createRecordType } from "./recordType";
4446
import { StorageOperationsCmsModelPlugin } from "@webiny/api-headless-cms";
4547
import { WriteRequest } from "@webiny/aws-sdk/client-dynamodb";
@@ -63,24 +65,6 @@ export interface CreateEntriesStorageOperationsParams {
6365
plugins: PluginsContainer;
6466
}
6567

66-
const IGNORED_ES_SEARCH_EXCEPTIONS = [
67-
"index_not_found_exception",
68-
"search_phase_execution_exception"
69-
];
70-
71-
const shouldIgnoreElasticsearchException = (ex: WebinyError) => {
72-
if (IGNORED_ES_SEARCH_EXCEPTIONS.includes(ex.message)) {
73-
console.log(`Ignoring Elasticsearch exception: ${ex.message}`);
74-
console.log({
75-
code: ex.code,
76-
data: ex.data,
77-
stack: ex.stack
78-
});
79-
return true;
80-
}
81-
return false;
82-
};
83-
8468
export const createEntriesStorageOperations = (
8569
params: CreateEntriesStorageOperationsParams
8670
): CmsEntryStorageOperations => {
@@ -303,10 +287,6 @@ export const createEntriesStorageOperations = (
303287
})
304288
];
305289

306-
const { index } = configurations.es({
307-
model
308-
});
309-
310290
if (isPublished) {
311291
items.push(
312292
entity.putBatch({
@@ -1096,21 +1076,28 @@ export const createEntriesStorageOperations = (
10961076
index,
10971077
body
10981078
});
1099-
} catch (ex) {
1079+
} catch (error) {
11001080
/**
11011081
* We will silently ignore the `index_not_found_exception` error and return an empty result set.
11021082
* This is because the index might not exist yet, and we don't want to throw an error.
11031083
*/
1104-
if (shouldIgnoreElasticsearchException(ex)) {
1084+
if (shouldIgnoreEsResponseError(error)) {
1085+
logIgnoredEsResponseError({
1086+
error,
1087+
model,
1088+
indexName: index
1089+
});
1090+
11051091
return {
11061092
hasMoreItems: false,
11071093
totalCount: 0,
11081094
cursor: null,
11091095
items: []
11101096
};
11111097
}
1112-
throw new WebinyError(ex.message, ex.code || "ELASTICSEARCH_ERROR", {
1113-
error: ex,
1098+
1099+
throw new WebinyError(error.message, error.code || "ELASTICSEARCH_ERROR", {
1100+
error,
11141101
index,
11151102
body,
11161103
model
@@ -1807,15 +1794,21 @@ export const createEntriesStorageOperations = (
18071794
index,
18081795
body
18091796
});
1810-
} catch (ex) {
1811-
if (shouldIgnoreElasticsearchException(ex)) {
1797+
} catch (error) {
1798+
if (shouldIgnoreEsResponseError(error)) {
1799+
logIgnoredEsResponseError({
1800+
error,
1801+
model,
1802+
indexName: index
1803+
});
18121804
return [];
18131805
}
1806+
18141807
throw new WebinyError(
1815-
ex.message || "Error in the Elasticsearch query.",
1816-
ex.code || "ELASTICSEARCH_ERROR",
1808+
error.message || "Error in the Elasticsearch query.",
1809+
error.code || "ELASTICSEARCH_ERROR",
18171810
{
1818-
error: ex,
1811+
error,
18191812
index,
18201813
model,
18211814
body

0 commit comments

Comments
 (0)