Skip to content

Commit fd484a2

Browse files
committed
feat: add logger and handle eveyrthing nicely
1 parent 050d3e0 commit fd484a2

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { HookContext } from '@feathersjs/feathers';
2+
import logger from '../logger';
23

34
export default async (context: HookContext): Promise<HookContext> => {
45
if (context.params.provider) {
56
const { method, path, id } = context;
6-
const timestamp = new Date().toLocaleString('default', { timeStyle: 'medium', dateStyle: 'short' });
77
const message = `${method.toUpperCase()}: /${path}/${id || ''}`;
88
const username = context.params.user?.username || 'anonymous';
99

10-
console.log(`[${timestamp}] ${message} ${username}`);
10+
logger.log(`${message} ${username}`);
1111
}
1212
return context;
1313
};

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.json

Lines changed: 1 addition & 0 deletions
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",

services/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Auth from './auth/auth.service';
77

88
import tryAuthenticate from '../hooks/tryAuthenticate';
99
import logging from '../hooks/logging';
10+
import handleErrors from '../hooks/handleErrors';
1011

1112
export default (app: Application): void => {
1213
app.configure(Auth);
@@ -17,10 +18,10 @@ export default (app: Application): void => {
1718

1819
app.hooks({
1920
before: {
20-
all: tryAuthenticate
21+
all: [tryAuthenticate, logging]
2122
},
22-
after: {
23-
all: logging
23+
error: {
24+
all: handleErrors
2425
}
2526
});
2627
};

0 commit comments

Comments
 (0)