From fd484a217c77dba42c29fae1cfdb2390422da847 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 21:25:18 +0300 Subject: feat: add logger and handle eveyrthing nicely --- hooks/handleErrors.ts | 11 +++++++++++ hooks/logging.ts | 4 ++-- hooks/requireAuth.ts | 5 ++++- 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 hooks/handleErrors.ts (limited to 'hooks') diff --git a/hooks/handleErrors.ts b/hooks/handleErrors.ts new file mode 100644 index 0000000..2a3c728 --- /dev/null +++ b/hooks/handleErrors.ts @@ -0,0 +1,11 @@ +import { HookContext } from '@feathersjs/feathers'; +import logger from '../logger'; + + +export default async (context: HookContext): Promise => { + context.result = context.error.message; + context.statusCode = context.error.code; + logger.error(context.error); + return context; +}; + diff --git a/hooks/logging.ts b/hooks/logging.ts index 840873c..8babe9a 100644 --- a/hooks/logging.ts +++ b/hooks/logging.ts @@ -1,13 +1,13 @@ import { HookContext } from '@feathersjs/feathers'; +import logger from '../logger'; export default async (context: HookContext): Promise => { if (context.params.provider) { const { method, path, id } = context; - const timestamp = new Date().toLocaleString('default', { timeStyle: 'medium', dateStyle: 'short' }); const message = `${method.toUpperCase()}: /${path}/${id || ''}`; const username = context.params.user?.username || 'anonymous'; - console.log(`[${timestamp}] ${message} ${username}`); + logger.log(`${message} ${username}`); } return context; }; diff --git a/hooks/requireAuth.ts b/hooks/requireAuth.ts index a7b0e96..52e8974 100644 --- a/hooks/requireAuth.ts +++ b/hooks/requireAuth.ts @@ -1,7 +1,10 @@ +import { NotAuthenticated } from '@feathersjs/errors'; import { HookContext } from '@feathersjs/feathers'; export default async (context: HookContext): Promise => { - if (!context.params.user) throw new Error('This endpoint requires auth!'); + if (!context.params.authenticated) { + throw new NotAuthenticated('This endpoint requires auth!'); + } return context; }; -- cgit v1.2.3