@@ -40,35 +40,56 @@ let wrappedHandler;
40
40
function wrapHandler ( ) {
41
41
const iopipe = require ( '@iopipe/core' ) ( ) ;
42
42
43
- const { LAMBDA_TASK_ROOT = '.' } = process . env ;
43
+ const { IOPIPE_HANDLER , LAMBDA_TASK_ROOT = '.' } = process . env ;
44
44
45
- if ( ! process . env . IOPIPE_HANDLER ) {
45
+ if ( ! IOPIPE_HANDLER ) {
46
46
throw new Error ( 'No IOPIPE_HANDLER environment variable set.' ) ;
47
47
}
48
48
49
- if ( process . env . IOPIPE_HANDLER . indexOf ( '.' ) === - 1 ) {
49
+ const parts = IOPIPE_HANDLER . split ( '.' ) ;
50
+
51
+ if ( parts . length !== 2 ) {
50
52
throw new Error (
51
- `Improperly formatted IOPIPE_HANDLER environment variable: ${
52
- process . env . IOPIPE_HANDLER
53
- } `
53
+ `Improperly formatted IOPIPE_HANDLER environment variable: ${ IOPIPE_HANDLER } `
54
54
) ;
55
55
}
56
56
57
- const [ moduleToImport , handlerToWrap ] = process . env . IOPIPE_HANDLER . split (
58
- '.' ,
59
- 1
60
- ) ;
57
+ const [ moduleToImport , handlerToWrap ] = parts ;
58
+
59
+ let importedModule ;
60
+
61
+ try {
62
+ /*eslint-disable import/no-dynamic-require*/
63
+ importedModule = require ( `${ LAMBDA_TASK_ROOT } /${ moduleToImport } ` ) ;
64
+ } catch ( e ) {
65
+ if ( e . code === 'MODULE_NOT_FOUND' ) {
66
+ throw new Error ( `Unable to import module '${ moduleToImport } '` ) ;
67
+ }
68
+
69
+ throw e ;
70
+ }
71
+
72
+ const userHandler = importedModule [ handlerToWrap ] ;
61
73
62
- /*eslint-disable import/no-dynamic-require*/
63
- const importedModule = require ( `${ LAMBDA_TASK_ROOT } /${ moduleToImport } ` ) ;
74
+ if ( typeof userHandler === 'undefined' ) {
75
+ throw new Error (
76
+ `Handler '${ handlerToWrap } ' missing on module '${ moduleToImport } '`
77
+ ) ;
78
+ }
79
+
80
+ if ( typeof userHandler !== 'function' ) {
81
+ throw new Error (
82
+ `Handler '${ handlerToWrap } ' from '${ moduleToImport } ' is not a function`
83
+ ) ;
84
+ }
64
85
65
- return iopipe ( importedModule [ handlerToWrap ] ) ;
86
+ return iopipe ( userHandler ) ;
66
87
}
67
88
68
- module . exports . handler = function handler ( ... args ) {
89
+ module . exports . handler = function handler ( event , context , callback ) {
69
90
if ( ! wrappedHandler ) {
70
91
wrappedHandler = wrapHandler ( ) ;
71
92
}
72
93
73
- return wrappedHandler . apply ( this , args ) ;
94
+ return wrappedHandler . call ( this , event , context , callback ) ;
74
95
} ;
0 commit comments