Skip to content

Commit 2156cb4

Browse files
authored
Merge pull request #12 from which-ecosystem/logging
Logging and exception handling
2 parents 343a975 + fd484a2 commit 2156cb4

File tree

7 files changed

+62
-7
lines changed

7 files changed

+62
-7
lines changed

hooks/handleErrors.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { HookContext } from '@feathersjs/feathers';
2+
import logger from '../logger';
3+
4+
5+
export default async (context: HookContext): Promise<HookContext> => {
6+
context.result = context.error.message;
7+
context.statusCode = context.error.code;
8+
logger.error(context.error);
9+
return context;
10+
};
11+

hooks/logging.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { HookContext } from '@feathersjs/feathers';
2+
import logger from '../logger';
3+
4+
export default async (context: HookContext): Promise<HookContext> => {
5+
if (context.params.provider) {
6+
const { method, path, id } = context;
7+
const message = `${method.toUpperCase()}: /${path}/${id || ''}`;
8+
const username = context.params.user?.username || 'anonymous';
9+
10+
logger.log(`${message} ${username}`);
11+
}
12+
return context;
13+
};
14+

hooks/requireAuth.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { NotAuthenticated } from '@feathersjs/errors';
12
import { HookContext } from '@feathersjs/feathers';
23

34
export default async (context: HookContext): Promise<HookContext> => {
4-
if (!context.params.user) throw new Error('This endpoint requires auth!');
5+
if (!context.params.authenticated) {
6+
throw new NotAuthenticated('This endpoint requires auth!');
7+
}
58
return context;
69
};
710

logger.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
type Stream = 'log' | 'error' | 'debug';
2+
3+
interface ErrorWithCode extends Error {
4+
code?: number
5+
}
6+
7+
const dateOpts = { timeStyle: 'medium', dateStyle: 'short' };
8+
// eslint-disable-next-line
9+
// @ts-ignore
10+
const timestamp = (): string => new Date().toLocaleString('en', dateOpts);
11+
12+
13+
const logWithTimestamp = (stream: Stream, message: string): void => {
14+
console[stream](`[${timestamp()}] ${message}`);
15+
};
16+
17+
export default {
18+
log: (message: string): void => logWithTimestamp('log', message),
19+
debug: (message: string): void => logWithTimestamp('debug', message),
20+
error: (err: ErrorWithCode): void => logWithTimestamp('error', `${err.code || ''} ${err.name}: ${err.message}`)
21+
};
22+

package-lock.json

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@feathersjs/authentication": "^4.5.3",
1414
"@feathersjs/authentication-local": "^4.5.4",
1515
"@feathersjs/configuration": "^4.5.3",
16+
"@feathersjs/errors": "^4.5.3",
1617
"@feathersjs/express": "^4.5.4",
1718
"@feathersjs/feathers": "^4.5.3",
1819
"@feathersjs/socketio": "^4.5.4",
@@ -22,7 +23,7 @@
2223
"feathers-mongoose": "^8.3.0",
2324
"lodash": "^4.17.15",
2425
"mongoose": "^5.9.18",
25-
"which-types": "^1.4.0"
26+
"which-types": "^1.4.1"
2627
},
2728
"repository": {
2829
"type": "git",

services/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Votes from './votes/votes.service';
66
import Auth from './auth/auth.service';
77

88
import tryAuthenticate from '../hooks/tryAuthenticate';
9+
import logging from '../hooks/logging';
10+
import handleErrors from '../hooks/handleErrors';
911

1012
export default (app: Application): void => {
1113
app.configure(Auth);
@@ -16,7 +18,10 @@ export default (app: Application): void => {
1618

1719
app.hooks({
1820
before: {
19-
all: tryAuthenticate
21+
all: [tryAuthenticate, logging]
22+
},
23+
error: {
24+
all: handleErrors
2025
}
2126
});
2227
};

0 commit comments

Comments
 (0)