Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 7f5e2d1

Browse files
author
tkostuch
committed
add header X-VS-Cache-Tags and error handling
1 parent 3cf056f commit 7f5e2d1

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

src/api/catalog.js

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ProcessorFactory from '../processor/factory';
44
import { adjustBackendProxyUrl } from '../lib/elastic'
55
import cache from '../lib/cache-instance'
66
import { sha3_224 } from 'js-sha3'
7+
import { apiError } from '../lib/util';
78

89
function _cacheStorageHandler (config, result, hash, tags) {
910
if (config.server.useOutputCache && cache) {
@@ -86,43 +87,41 @@ export default ({config, db}) => function (req, res, body) {
8687
body: requestBody,
8788
json: true,
8889
auth: auth
89-
}, (_err, _res, _resBody) => { // TODO: add caching layer to speed up SSR? How to invalidate products (checksum on the response BEFORE processing it)
90-
if (_resBody && _resBody.hits && _resBody.hits.hits) { // we're signing up all objects returned to the client to be able to validate them when (for example order)
91-
const factory = new ProcessorFactory(config)
92-
const tagsArray = []
93-
if (config.server.useOutputCache && cache) {
94-
const tagPrefix = entityType[0].toUpperCase() // first letter of entity name: P, T, A ...
95-
tagsArray.push(entityType)
96-
_resBody.hits.hits.map(item => {
97-
if (item._source.id) { // has common identifier
98-
tagsArray.push(`${tagPrefix}${item._source.id}`)
99-
}
100-
})
101-
}
102-
103-
let resultProcessor = factory.getAdapter(entityType, indexName, req, res)
104-
105-
if (!resultProcessor) { resultProcessor = factory.getAdapter('default', indexName, req, res) } // get the default processor
106-
107-
if (entityType === 'product') {
108-
resultProcessor.process(_resBody.hits.hits, groupId).then((result) => {
109-
_resBody.hits.hits = result
110-
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
111-
res.json(_resBody);
112-
}).catch((err) => {
113-
console.error(err)
114-
})
115-
} else {
116-
resultProcessor.process(_resBody.hits.hits).then((result) => {
117-
_resBody.hits.hits = result
118-
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
119-
res.json(_resBody);
120-
}).catch((err) => {
121-
console.error(err)
122-
})
90+
}, async (_err, _res, _resBody) => { // TODO: add caching layer to speed up SSR? How to invalidate products (checksum on the response BEFORE processing it)
91+
if (_err || _resBody.error) {
92+
apiError(res, _err || _resBody.error);
93+
return
94+
}
95+
try {
96+
if (_resBody && _resBody.hits && _resBody.hits.hits) { // we're signing up all objects returned to the client to be able to validate them when (for example order)
97+
const factory = new ProcessorFactory(config)
98+
const tagsArray = []
99+
if (config.server.useOutputCache && cache) {
100+
const tagPrefix = entityType[0].toUpperCase() // first letter of entity name: P, T, A ...
101+
tagsArray.push(entityType)
102+
_resBody.hits.hits.map(item => {
103+
if (item._source.id) { // has common identifier
104+
tagsArray.push(`${tagPrefix}${item._source.id}`)
105+
}
106+
})
107+
const cacheTags = tagsArray.join(' ')
108+
res.setHeader('X-VS-Cache-Tags', cacheTags)
109+
}
110+
111+
let resultProcessor = factory.getAdapter(entityType, indexName, req, res)
112+
113+
if (!resultProcessor) { resultProcessor = factory.getAdapter('default', indexName, req, res) } // get the default processor
114+
115+
const productGroupId = entityType === 'product' ? groupId : undefined
116+
const result = await resultProcessor.process(_resBody.hits.hits, productGroupId)
117+
_resBody.hits.hits = result
118+
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
119+
res.json(_resBody);
120+
} else { // no cache storage if no results from Elastic
121+
res.json(_resBody);
123122
}
124-
} else { // no cache storage if no results from Elastic
125-
res.json(_resBody);
123+
} catch (err) {
124+
apiError(res, err);
126125
}
127126
});
128127
}

0 commit comments

Comments
 (0)