diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-06-25 22:19:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-25 22:19:15 +0300 |
commit | 2156cb4cb0d84d7f3905d97ef789c3f9fde22548 (patch) | |
tree | b11c527bbc0a268f0361a4d56f6aff7011960f98 | |
parent | 343a975413d8d3ce8194507017fb4ca01a4faf54 (diff) | |
parent | fd484a217c77dba42c29fae1cfdb2390422da847 (diff) | |
download | which-api-2156cb4cb0d84d7f3905d97ef789c3f9fde22548.tar.gz |
Merge pull request #12 from which-ecosystem/logging
Logging and exception handling
-rw-r--r-- | hooks/handleErrors.ts | 11 | ||||
-rw-r--r-- | hooks/logging.ts | 14 | ||||
-rw-r--r-- | hooks/requireAuth.ts | 5 | ||||
-rw-r--r-- | logger.ts | 22 | ||||
-rw-r--r-- | package-lock.json | 7 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | services/index.ts | 7 |
7 files changed, 62 insertions, 7 deletions
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<HookContext> => { + 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 new file mode 100644 index 0000000..8babe9a --- /dev/null +++ b/hooks/logging.ts @@ -0,0 +1,14 @@ +import { HookContext } from '@feathersjs/feathers'; +import logger from '../logger'; + +export default async (context: HookContext): Promise<HookContext> => { + if (context.params.provider) { + const { method, path, id } = context; + const message = `${method.toUpperCase()}: /${path}/${id || ''}`; + const username = context.params.user?.username || 'anonymous'; + + 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<HookContext> => { - if (!context.params.user) throw new Error('This endpoint requires auth!'); + if (!context.params.authenticated) { + throw new NotAuthenticated('This endpoint requires auth!'); + } return context; }; diff --git a/logger.ts b/logger.ts new file mode 100644 index 0000000..f11b0c8 --- /dev/null +++ b/logger.ts @@ -0,0 +1,22 @@ +type Stream = 'log' | 'error' | 'debug'; + +interface ErrorWithCode extends Error { + code?: number +} + +const dateOpts = { timeStyle: 'medium', dateStyle: 'short' }; +// eslint-disable-next-line +// @ts-ignore +const timestamp = (): string => new Date().toLocaleString('en', dateOpts); + + +const logWithTimestamp = (stream: Stream, message: string): void => { + console[stream](`[${timestamp()}] ${message}`); +}; + +export default { + log: (message: string): void => logWithTimestamp('log', message), + debug: (message: string): void => logWithTimestamp('debug', message), + error: (err: ErrorWithCode): void => logWithTimestamp('error', `${err.code || ''} ${err.name}: ${err.message}`) +}; + diff --git a/package-lock.json b/package-lock.json index 1402c27..6a3838a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3051,10 +3051,9 @@ } }, "which-types": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/which-types/-/which-types-1.4.0.tgz", - "integrity": "sha512-dq5f7AmZeataiQZxko+Dw1xRvWdzSXTwXrjKxtyL5zqLgS29J1PXjTgcAWMfbkZhbd8+iJLkfxKvYZ42itmxeA==", - "dev": true + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/which-types/-/which-types-1.4.1.tgz", + "integrity": "sha512-ZtN3cDwz/fQbJBwrItsZ0jpGafReTd/fIffHNQtFW4THrZqi8z4qnFTbyu1M6LnAmPlwU/FaRLZPfd67ZQ4mFw==" }, "word-wrap": { "version": "1.2.3", diff --git a/package.json b/package.json index 1a21fcf..c6e4038 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@feathersjs/authentication": "^4.5.3", "@feathersjs/authentication-local": "^4.5.4", "@feathersjs/configuration": "^4.5.3", + "@feathersjs/errors": "^4.5.3", "@feathersjs/express": "^4.5.4", "@feathersjs/feathers": "^4.5.3", "@feathersjs/socketio": "^4.5.4", @@ -22,7 +23,7 @@ "feathers-mongoose": "^8.3.0", "lodash": "^4.17.15", "mongoose": "^5.9.18", - "which-types": "^1.4.0" + "which-types": "^1.4.1" }, "repository": { "type": "git", diff --git a/services/index.ts b/services/index.ts index ce20901..bdce04f 100644 --- a/services/index.ts +++ b/services/index.ts @@ -6,6 +6,8 @@ import Votes from './votes/votes.service'; import Auth from './auth/auth.service'; import tryAuthenticate from '../hooks/tryAuthenticate'; +import logging from '../hooks/logging'; +import handleErrors from '../hooks/handleErrors'; export default (app: Application): void => { app.configure(Auth); @@ -16,7 +18,10 @@ export default (app: Application): void => { app.hooks({ before: { - all: tryAuthenticate + all: [tryAuthenticate, logging] + }, + error: { + all: handleErrors } }); }; |