Skip to content

Commit 7036f13

Browse files
committed
refactor: instanceof comparisons, dont log stack
1 parent 115919f commit 7036f13

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

lib/Auth/index.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class Auth {
149149
* @param {String} opts.cache.key
150150
* @param {String} opts.cache.action
151151
* @param {Boolean} opts.cache.onNotFound
152+
* @param {Object} [opts.error = {}] Error options
152153
* @param {Number} retry Allow retries in case of expired token
153154
*
154155
* @returns {Promise<{} | Array<{}>>}
@@ -166,7 +167,8 @@ class Auth {
166167
isPublic: false,
167168
cache: false,
168169
timeout: 60000,
169-
deadline: 90000
170+
deadline: 90000,
171+
error: {}
170172
},
171173
opts
172174
);
@@ -243,7 +245,10 @@ class Auth {
243245
opts.cache.action === this.cache.actions.get &&
244246
opts.cache.onNotFound &&
245247
error.status === 404 &&
246-
error.typeof !== CoreError.CachedNotFound
248+
!(
249+
error instanceof CoreError &&
250+
error.typeof.name === CoreError.CachedNotFound.name
251+
)
247252
) {
248253
await this.executeCacheOp({
249254
value: false,
@@ -255,7 +260,10 @@ class Auth {
255260
if (
256261
!opts.isPublic &&
257262
error.status === 401 &&
258-
error.typeof !== CoreError.MaxAuthenticatedRequestsReached
263+
!(
264+
error instanceof CoreError &&
265+
error.typeof.name === CoreError.MaxAuthenticatedRequestsReached.name
266+
)
259267
) {
260268
this.access.failed = true;
261269
return this.send(service, method, url, opts, ++retry);
@@ -267,7 +275,8 @@ class Auth {
267275
service,
268276
method,
269277
this.timeDelta(begin),
270-
opts.query
278+
opts.query,
279+
opts.error
271280
);
272281
}
273282
}
@@ -277,28 +286,37 @@ class Auth {
277286
*
278287
* @private
279288
*
280-
* @param {typeof Error} error Error returned
281-
* @param {String} url Endpoint
282-
* @param {String} service Core service requested
283-
* @param {String} method REST method used
284-
* @param {Number} tDelta Request time in ms
285-
* @param {Object} query The query sent
289+
* @param {typeof Error} error Error returned
290+
* @param {String} url Endpoint
291+
* @param {String} service Core service requested
292+
* @param {String} method REST method used
293+
* @param {Number} tDelta Request time in ms
294+
* @param {Object} query The query sent
295+
* @param {Object} opts Additional options
296+
* @param {Boolean} [opts.mute=false] Whether to mute the error log
286297
*
287298
* @returns {CoreError}
288299
*/
289-
getError(error, url, service, method, tDelta, query) {
300+
getError(error, url, service, method, tDelta, query, opts = {}) {
301+
opts = { mute: false, ...opts };
302+
290303
const errorText =
291304
!!error.response && !!error.response.text
292305
? error.response.text
306+
: error instanceof CoreError && (error.isInternal || error.isCache)
307+
? error.typeof.name
293308
: error.stack;
294-
this.log(
295-
`${service} ${method}, code: ${
296-
error.status
297-
}, t: ${tDelta} ms -> url: ${url}, query: ${JSON.stringify(
298-
query || {}
299-
)} -> error response ${errorText}`,
300-
'error'
301-
);
309+
310+
if (!opts.mute) {
311+
this.log(
312+
`${service} ${method}, code: ${
313+
error.isCache ? 'CACHED' : error.status
314+
}, t: ${tDelta} ms -> url: ${url}, query: ${JSON.stringify(
315+
query || {}
316+
)} -> error response ${errorText}`,
317+
'error'
318+
);
319+
}
302320

303321
if (error instanceof CoreError) {
304322
return error;

0 commit comments

Comments
 (0)