aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hooks/handleErrors.ts11
-rw-r--r--hooks/logging.ts4
-rw-r--r--hooks/requireAuth.ts5
-rw-r--r--logger.ts22
-rw-r--r--package.json1
-rw-r--r--services/index.ts7
6 files changed, 44 insertions, 6 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
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<HookContext> => {
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<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.json b/package.json
index e21de51..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",
diff --git a/services/index.ts b/services/index.ts
index 8d77429..bdce04f 100644
--- a/services/index.ts
+++ b/services/index.ts
@@ -7,6 +7,7 @@ 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);
@@ -17,10 +18,10 @@ export default (app: Application): void => {
app.hooks({
before: {
- all: tryAuthenticate
+ all: [tryAuthenticate, logging]
},
- after: {
- all: logging
+ error: {
+ all: handleErrors
}
});
};