File tree Expand file tree Collapse file tree 6 files changed +44
-6
lines changed Expand file tree Collapse file tree 6 files changed +44
-6
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change 1
1
import { HookContext } from '@feathersjs/feathers' ;
2
+ import logger from '../logger' ;
2
3
3
4
export default async ( context : HookContext ) : Promise < HookContext > => {
4
5
if ( context . params . provider ) {
5
6
const { method, path, id } = context ;
6
- const timestamp = new Date ( ) . toLocaleString ( 'default' , { timeStyle : 'medium' , dateStyle : 'short' } ) ;
7
7
const message = `${ method . toUpperCase ( ) } : /${ path } /${ id || '' } ` ;
8
8
const username = context . params . user ?. username || 'anonymous' ;
9
9
10
- console . log ( `[ ${ timestamp } ] ${ message } ${ username } ` ) ;
10
+ logger . log ( `${ message } ${ username } ` ) ;
11
11
}
12
12
return context ;
13
13
} ;
Original file line number Diff line number Diff line change
1
+ import { NotAuthenticated } from '@feathersjs/errors' ;
1
2
import { HookContext } from '@feathersjs/feathers' ;
2
3
3
4
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
+ }
5
8
return context ;
6
9
} ;
7
10
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change 13
13
"@feathersjs/authentication" : " ^4.5.3" ,
14
14
"@feathersjs/authentication-local" : " ^4.5.4" ,
15
15
"@feathersjs/configuration" : " ^4.5.3" ,
16
+ "@feathersjs/errors" : " ^4.5.3" ,
16
17
"@feathersjs/express" : " ^4.5.4" ,
17
18
"@feathersjs/feathers" : " ^4.5.3" ,
18
19
"@feathersjs/socketio" : " ^4.5.4" ,
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import Auth from './auth/auth.service';
7
7
8
8
import tryAuthenticate from '../hooks/tryAuthenticate' ;
9
9
import logging from '../hooks/logging' ;
10
+ import handleErrors from '../hooks/handleErrors' ;
10
11
11
12
export default ( app : Application ) : void => {
12
13
app . configure ( Auth ) ;
@@ -17,10 +18,10 @@ export default (app: Application): void => {
17
18
18
19
app . hooks ( {
19
20
before : {
20
- all : tryAuthenticate
21
+ all : [ tryAuthenticate , logging ]
21
22
} ,
22
- after : {
23
- all : logging
23
+ error : {
24
+ all : handleErrors
24
25
}
25
26
} ) ;
26
27
} ;
You can’t perform that action at this time.
0 commit comments