From 680dde74e0a1591b9809c17431a6b8b96244dac3 Mon Sep 17 00:00:00 2001 From: Roman Kalyakin Date: Mon, 14 Oct 2024 08:51:49 +0200 Subject: [PATCH] Updated error capture code: added more details (#435) --- .../errors-collector.class.js | 32 ------------------- .../errors-collector.class.ts | 27 ++++++++++++++++ .../errors-collector.hooks.js | 31 ------------------ .../errors-collector.hooks.ts | 9 ++++++ .../errors-collector.service.js | 7 ---- .../errors-collector.service.ts | 8 +++++ src/types.ts | 2 +- 7 files changed, 45 insertions(+), 71 deletions(-) delete mode 100644 src/services/errors-collector/errors-collector.class.js create mode 100644 src/services/errors-collector/errors-collector.class.ts delete mode 100644 src/services/errors-collector/errors-collector.hooks.js create mode 100644 src/services/errors-collector/errors-collector.hooks.ts delete mode 100644 src/services/errors-collector/errors-collector.service.js create mode 100644 src/services/errors-collector/errors-collector.service.ts diff --git a/src/services/errors-collector/errors-collector.class.js b/src/services/errors-collector/errors-collector.class.js deleted file mode 100644 index 2d5b21ad..00000000 --- a/src/services/errors-collector/errors-collector.class.js +++ /dev/null @@ -1,32 +0,0 @@ -/* eslint-disable no-unused-vars */ -class ErrorsCollector { - async create (data = {}) { - const { - type = '', - name = '', - message = '', - stack = '', - uri = 'N/A', - hook = {}, - } = data; - const params = []; - // get params - if (hook.params) { - if (hook.params.user) { - params.push(`user:${hook.params.user.uid}`); - } - if (hook.params.query) { - params.push(`qs:${JSON.stringify(hook.params.query)}`); - } - } - const readableMessage = `[Front End Error] (${type || 'GenericError'}:${name}) at "${uri}": ${message} - ${params.join(' - ')}`; - console.error(readableMessage, stack); - return Promise.resolve({ ok: 1, stderr: readableMessage }); - } -} - -module.exports = function (options) { - return new ErrorsCollector(options); -}; - -module.exports.Service = ErrorsCollector; diff --git a/src/services/errors-collector/errors-collector.class.ts b/src/services/errors-collector/errors-collector.class.ts new file mode 100644 index 00000000..e5a4bfcb --- /dev/null +++ b/src/services/errors-collector/errors-collector.class.ts @@ -0,0 +1,27 @@ +import { Params } from '@feathersjs/feathers' +import { SlimUser } from '../../authentication' + +interface ErrorsCollectorPayload { + id: string + url: string + errorMessage: string + stackTrace?: string + origin?: string + className?: string + type?: string +} + +interface ErrorContext extends ErrorsCollectorPayload { + userId?: string + timestamp: string // ISO 8601 +} + +/* eslint-disable no-unused-vars */ +export default class ErrorsCollector { + async create(data: ErrorsCollectorPayload, params: Params) { + const user: SlimUser | undefined = (params as any).user + const context = { ...data, userId: user?.uid, timestamp: new Date().toISOString() } + const message = `[WebApp Error] ${JSON.stringify(context)}` + console.error(message) + } +} diff --git a/src/services/errors-collector/errors-collector.hooks.js b/src/services/errors-collector/errors-collector.hooks.js deleted file mode 100644 index b54eb1ed..00000000 --- a/src/services/errors-collector/errors-collector.hooks.js +++ /dev/null @@ -1,31 +0,0 @@ -module.exports = { - before: { - all: [], - find: [], - get: [], - create: [], - update: [], - patch: [], - remove: [], - }, - - after: { - all: [], - find: [], - get: [], - create: [], - update: [], - patch: [], - remove: [], - }, - - error: { - all: [], - find: [], - get: [], - create: [], - update: [], - patch: [], - remove: [], - }, -}; diff --git a/src/services/errors-collector/errors-collector.hooks.ts b/src/services/errors-collector/errors-collector.hooks.ts new file mode 100644 index 00000000..d2415524 --- /dev/null +++ b/src/services/errors-collector/errors-collector.hooks.ts @@ -0,0 +1,9 @@ +import type { HookMap } from '@feathersjs/feathers' +import { AppServices, ImpressoApplication } from '../../types' +import { authenticateAround as authenticate } from '../../hooks/authenticate' + +export default { + around: { + all: [authenticate({ allowUnauthenticated: true })], + }, +} satisfies HookMap diff --git a/src/services/errors-collector/errors-collector.service.js b/src/services/errors-collector/errors-collector.service.js deleted file mode 100644 index a14c1381..00000000 --- a/src/services/errors-collector/errors-collector.service.js +++ /dev/null @@ -1,7 +0,0 @@ -const createService = require('./errors-collector.class'); -const hooks = require('./errors-collector.hooks'); - -module.exports = function (app) { - app.use('/errors-collector', createService()); - app.service('errors-collector').hooks(hooks); -}; diff --git a/src/services/errors-collector/errors-collector.service.ts b/src/services/errors-collector/errors-collector.service.ts new file mode 100644 index 00000000..2ee2329c --- /dev/null +++ b/src/services/errors-collector/errors-collector.service.ts @@ -0,0 +1,8 @@ +import { Application } from '@feathersjs/feathers' +import Service from './errors-collector.class' +import hooks from './errors-collector.hooks' + +export default function (app: Application) { + app.use('/errors-collector', new Service()) + app.service('errors-collector').hooks(hooks) +} diff --git a/src/types.ts b/src/types.ts index 5aed983e..ef5497ab 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,7 +6,7 @@ import { CachedSolrClient } from './cachedSolr' import { Service as LogsService } from './services/logs/logs.class' import { AuthenticationService } from '@feathersjs/authentication' -interface AppServices { +export interface AppServices { redisClient?: IRedisClientContainer rateLimiter?: IRateLimiter cachedSolr: CachedSolrClient