Skip to content

Commit 5f857ec

Browse files
committed
cveListSearch: fix getIdData(), handle missing data in getSearchResults()
1 parent 77f6dab commit 5f857ec

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

src/stores/cveListSearch.js

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
2929
showHelpText: false,
3030
size: 25,
3131
totalExecutingRequests: -1,
32-
totalSearchResultCount: 0,
3332
pagination: {
3433
startWindow: 1,
3534
endWindow: undefined,
@@ -65,6 +64,9 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
6564
},
6665
}
6766
},
67+
getters: {
68+
totalSearchResultCount: (state) => state.searchResults.length
69+
},
6870
actions: {
6971
decrement(field) {
7072
this[field] -= 1;
@@ -74,7 +76,6 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
7476
},
7577
resetResults() {
7678
this.searchResults = [];
77-
this.totalSearchResultCount = 0;
7879
},
7980
async search() {
8081
this.isSearching = true;
@@ -97,9 +98,8 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
9798
// if record is not found, find potential reserved/rejected ID
9899
if (this.isCveIdPattern() && Object.keys(this.recordData).length === 0) {
99100
await this.getIdData();
100-
}
101-
102-
throw new Error('search() >> error with getSearchResults(), getRecordData(), or getIdData()');
101+
} else
102+
throw new Error(`search() >> error with getSearchResults(), getRecordData(): ${error}`);
103103
} finally {
104104
this.isSearching = false;
105105
this.setUpInitialPagination();
@@ -123,7 +123,7 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
123123
cna: cveRecordData?.containers?.cna?.providerMetadata?.shortName || 'No CNA provided',
124124
cnaOrgId: cveRecordData?.containers?.cna?.providerMetadata?.orgId || '',
125125
descriptions: descriptions,
126-
relevancyScore: 'not appliciable'
126+
relevancyScore: 'not applicable'
127127
}
128128
this.recordData = recordDataSummary;
129129

@@ -151,15 +151,15 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
151151
const getIdUrl = `${useGenericGlobalsStore().currentServicesUrl}/api/cve-id/${this.cveId}`;
152152
try {
153153
const response = await axios.get(getIdUrl);
154-
const idData = response || {};
154+
const idData = response.data ?? {};
155155
this.isIdOrRecordFound = true;
156156
this.isAnId = true;
157-
if (idData.status === 200 && idData?.data?.error === undefined) {
158-
this.idData = idData.data;
157+
if (response.status === 200 && response.data?.error === undefined) {
158+
this.idData = idData;
159159
if (idData.state === 'RESERVED') {
160160
this.isReserved = true;
161161
this.isArecord = false;
162-
} else if (this.$store.state.idData.state === 'REJECTED') {
162+
} else if (idData.state === 'REJECTED') {
163163
this.isRejected = true;
164164
this.isArecord = false;
165165
}
@@ -176,32 +176,44 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
176176
this.resetResults();
177177

178178
try {
179+
const ems = useErrorMessageStore();
179180
let payLoad = this.createSearchPayload();
180181
const response = await axios.post(searchUrl, payLoad);
181182

182-
const searchMetaData = response.data.searchMetadata;
183+
if (!response.data) {
184+
185+
// This shouldn't happen, but it has so it's possible. This is
186+
// different from a successful search with no results - the service
187+
// returned nothing in the 'data' field. Rather than ignore it,
188+
// indicate with a message that nothing was returned. The problem may
189+
// be that the service can't handle something in the input string.
190+
191+
ems.pushErrorMessage('No response data from search - check input');
183192

184-
if (searchMetaData.searchStatus === 'ok') {
185-
this.totalSearchResultCount = response.data.resultsTotal;
186-
this.processSearchResults(response.data.data);
187193
} else {
188-
// There's likely something invalid in the search string.
189-
// Error message(s) and note(s) returned in the response will be
190-
// displayed to the user.
191-
//
192-
// We could reset the error message store here, because this is a
193-
// "new" search, but resetting may also mask an issue where the
194-
// search is inadvertently done multiple times (resulting in
195-
// duplicate error messages displayed).
196-
197-
const ems = useErrorMessageStore();
198-
199-
searchMetaData.errors.forEach(e => ems.pushErrorMessage(e.message));
200-
searchMetaData.notes.forEach(n => ems.pushErrorMessage(n));
194+
195+
const searchMetaData = response.data.searchMetadata;
196+
197+
if (searchMetaData.searchStatus === 'ok') {
198+
this.processSearchResults(response.data.data);
199+
} else {
200+
// There's likely something invalid in the search string.
201+
// Error message(s) and note(s) returned in the response will be
202+
// displayed to the user.
203+
//
204+
// We could reset the error message store here, because this is a
205+
// "new" search, but resetting may also mask an issue where the
206+
// search is inadvertently done multiple times (resulting in
207+
// duplicate error messages displayed).
208+
209+
searchMetaData.errors.forEach(e => ems.pushErrorMessage(e.message));
210+
searchMetaData.notes.forEach(n => ems.pushErrorMessage(n));
211+
}
201212
}
202213
} catch (e) {
203214
this.isSearchServerError = true;
204-
throw new Error(`getSearchResults >> : ${e}`)
215+
if (e.code !== 'ERR_NETWORK')
216+
throw new Error(`getSearchResults >> : ${e}`);
205217
} finally {
206218
this.isQueryingSearchService = false;
207219
this.decrement('totalExecutingRequests');
@@ -210,7 +222,7 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
210222
processSearchResults(results){
211223
const parsedResults = [];
212224

213-
if (results.length >> 0) {
225+
if (results.length > 0) {
214226
results.forEach((result) => {
215227
parsedResults.push({
216228
cveId: result._id,
@@ -228,13 +240,17 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
228240
let descriptions = [];
229241
if (recordData.cveMetadata?.state === "PUBLISHED") {
230242
recordData.containers?.cna?.descriptions.forEach((description) => {
231-
if (this.isEnglishLanguage(description.lang)) descriptions.push(this.processShowMoreShowLessDescription(description.value));
243+
if (this.isEnglishLanguage(description.lang))
244+
descriptions.push(this.processShowMoreShowLessDescription(description.value));
232245
});
233246
} else if (recordData.cveMetadata?.state === "REJECTED") {
234247
recordData.containers?.cna?.rejectedReasons.forEach((rejectedReason) => {
235-
if (this.isEnglishLanguage(rejectedReason.lang)) descriptions.push(this.processShowMoreShowLessDescription(rejectedReason.value));
248+
if (this.isEnglishLanguage(rejectedReason.lang))
249+
descriptions.push(this.processShowMoreShowLessDescription(rejectedReason.value));
236250
});
237-
} else { // resevered/rejected ID w/o a CVE record does not have description, and that logic is handled in corresponding Vue file
251+
} else {
252+
// reserved/rejected ID w/o a CVE record does not have description, and
253+
// that logic is handled in corresponding Vue file
238254
descriptions.push('No description provided');
239255
}
240256

0 commit comments

Comments
 (0)