Skip to content

Commit 5c77473

Browse files
committed
fix: not found cache handling
1 parent 41edec7 commit 5c77473

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

lib/Auth/index.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class Auth {
4949
drop: 'drop',
5050
get: 'get',
5151
set: 'set'
52-
}
52+
},
53+
status: 'cached'
5354
};
5455

5556
/** @type CoreError */
@@ -145,7 +146,7 @@ class Auth {
145146
* @param {Object} [opts.cache=false]
146147
* @param {String} opts.cache.key
147148
* @param {String} opts.cache.action
148-
* @param {Boolean} opts.cache.onError
149+
* @param {Boolean} opts.cache.onNotFound
149150
* @param {Number} retry Allow retries in case of expired token
150151
*
151152
* @returns {Promise<{} | Array<{}>>}
@@ -174,25 +175,23 @@ class Auth {
174175

175176
let status;
176177
let body;
177-
178178
if (this.isCacheEnabled && opts.cache) {
179179
if (opts.cache.action === this.cache.actions.drop) {
180180
await this.executeCacheOp(opts.cache);
181181
}
182182
if (opts.cache.action === this.cache.actions.get) {
183183
body = await this.executeCacheOp(opts.cache);
184-
status = body || body === false ? 'cached' : null;
184+
status = body || body === false ? this.cache.status : null;
185185
}
186186
}
187187

188-
if (!opts.isPublic) {
189-
const clientToken = await this.getApplicationToken();
190-
opts.headers.authorization = `Bearer ${clientToken}`;
191-
}
192-
193-
begin = new Date().getTime();
194-
195188
if (!body && body !== false) {
189+
if (!opts.isPublic) {
190+
const clientToken = await this.getApplicationToken();
191+
opts.headers.authorization = `Bearer ${clientToken}`;
192+
}
193+
194+
begin = new Date().getTime();
196195
({ status, body } = await superagent(method, url)
197196
.set(opts.headers)
198197
.query(opts.query)
@@ -211,27 +210,31 @@ class Auth {
211210
}
212211
}
213212

213+
if (this.isCacheEnabled && body === false) {
214+
throw CoreError.CachedNotFound();
215+
}
216+
214217
this.log(
215218
`${service} ${method}, code: ${status}, t: ${this.timeDelta(
216219
begin
217220
)} ms -> url: ${url}, query: ${JSON.stringify(opts.query || {})}`
218221
);
219222

220-
// cached as false, throw an error
221-
if (this.isCacheEnabled && body === false) {
222-
throw new CoreError(new Error('Not found'), { statusCode: 404 });
223-
}
224-
225223
return body;
226224
} catch (error) {
227225
if (
228226
this.isCacheEnabled &&
229227
opts.cache &&
230228
opts.cache.action === this.cache.actions.get &&
231-
opts.cache.onError &&
232-
error.status === 404
229+
opts.cache.onNotFound &&
230+
error.status === 404 &&
231+
error.typeof !== CoreError.CachedNotFound
233232
) {
234-
await this.executeCacheOp({ value: false, ...opts.cache });
233+
await this.executeCacheOp({
234+
value: false,
235+
...opts.cache,
236+
...{ action: this.cache.actions.set }
237+
});
235238
}
236239

237240
if (

lib/CoreError.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class CoreError extends Boom {
5959
});
6060
}
6161

62+
static CachedNotFound() {
63+
return this.notFound(this.CachedNotFound.name, {
64+
ctr: this.CachedNotFound
65+
});
66+
}
67+
6268
/**
6369
* Overrides Boom static to create under Core instance
6470
* @param {String} fnName
@@ -80,5 +86,6 @@ class CoreError extends Boom {
8086
}
8187

8288
CoreError.override('unauthorized');
89+
CoreError.override('notFound');
8390

8491
module.exports = CoreError;

lib/Services/cayenne.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,12 @@ class Cayenne {
309309
/**
310310
* Gets a device's meta data by device type id
311311
* @param {String} deviceTypeId
312-
* @param {Objet} [opts={}] Additional options
313-
* @param {Boolean} [opts.useCache=false] If cached result should be retrieved
312+
* @param {Objet} [opts={}] Additional options
313+
* @param {Boolean} [opts.useCache=false] If cached result should be retrieved
314+
* @param {Boolean} [opts.cacheOnNotFound=false] Whether to cache if a 404 error is returned
314315
*/
315316
getDeviceTypeMeta(deviceTypeId, opts) {
316-
opts = { useCache: false, ...opts };
317+
opts = { useCache: false, cacheOnNotFound: false, ...opts };
317318

318319
const url = `${this.url}/things/types/${deviceTypeId}/meta`;
319320
return this.auth.send(this.service, 'GET', url, {
@@ -325,7 +326,7 @@ class Cayenne {
325326
...arguments
326327
),
327328
action: this.auth.cache.actions.get,
328-
onError: false
329+
onNotFound: opts.cacheOnNotFound
329330
}
330331
: false
331332
});
@@ -335,11 +336,12 @@ class Cayenne {
335336
* Gets a device's meta data by device type id
336337
* @param {String} deviceTypeId
337338
* @param {String} key
338-
* @param {Objet} [opts={}] Additional options
339-
* @param {Boolean} [opts.useCache=false] If cached result should be retrieved
339+
* @param {Objet} [opts={}] Additional options
340+
* @param {Boolean} [opts.useCache=false] If cached result should be retrieved
341+
* @param {Boolean} [opts.cacheOnNotFound=false] Whether to cache if a 404 error is returned
340342
*/
341343
getDeviceTypeMetaByKey(deviceTypeId, key, opts = {}) {
342-
opts = { useCache: false, ...opts };
344+
opts = { useCache: false, cacheOnNotFound: false, ...opts };
343345

344346
const url = `${this.url}/things/types/${deviceTypeId}/meta/${key}`;
345347

@@ -352,7 +354,7 @@ class Cayenne {
352354
...arguments
353355
),
354356
action: this.auth.cache.actions.get,
355-
onError: false
357+
onNotFound: opts.cacheOnNotFound
356358
}
357359
: false
358360
});

0 commit comments

Comments
 (0)