@@ -11,6 +11,8 @@ import {
11
11
Response as ExpressResponse ,
12
12
} from 'express' ;
13
13
import twilio , { twiml } from 'twilio' ;
14
+ import { checkForValidAccountSid } from '../checks/check-account-sid' ;
15
+ import { wrapErrorInHtml } from '../utils/error-html' ;
14
16
import { StartCliConfig } from './cli/config' ;
15
17
import { Response } from './internal/response' ;
16
18
import * as Runtime from './internal/runtime' ;
@@ -32,6 +34,12 @@ export function constructContext<T extends {} = {}>({
32
34
[ key : string ] : string | undefined | Function ;
33
35
} > {
34
36
function getTwilioClient ( ) : twilio . Twilio {
37
+ checkForValidAccountSid ( env . ACCOUNT_SID , {
38
+ shouldPrintMessage : true ,
39
+ shouldThrowError : true ,
40
+ functionName : 'context.getTwilioClient()' ,
41
+ } ) ;
42
+
35
43
return twilio ( env . ACCOUNT_SID , env . AUTH_TOKEN ) ;
36
44
}
37
45
const DOMAIN_NAME = url . replace ( / ^ h t t p s ? : \/ \/ / , '' ) ;
@@ -45,17 +53,25 @@ export function constructGlobalScope(config: StartCliConfig): void {
45
53
( global as any ) [ 'Functions' ] = GlobalRuntime . getFunctions ( ) ;
46
54
( global as any ) [ 'Response' ] = Response ;
47
55
48
- if ( config . env . ACCOUNT_SID && config . env . AUTH_TOKEN ) {
56
+ if (
57
+ checkForValidAccountSid ( config . env . ACCOUNT_SID ) &&
58
+ config . env . AUTH_TOKEN
59
+ ) {
49
60
( global as any ) [ 'twilioClient' ] = twilio (
50
61
config . env . ACCOUNT_SID ,
51
62
config . env . AUTH_TOKEN
52
63
) ;
53
64
}
54
65
}
55
66
56
- export function handleError ( err : Error , res : ExpressResponse ) {
67
+ export function handleError (
68
+ err : Error ,
69
+ res : ExpressResponse ,
70
+ functionFilePath ?: string
71
+ ) {
57
72
res . status ( 500 ) ;
58
- res . send ( err . stack ) ;
73
+ res . type ( 'text/html' ) ;
74
+ res . send ( wrapErrorInHtml ( err , functionFilePath ) ) ;
59
75
}
60
76
61
77
export function isTwiml ( obj : object ) : boolean {
@@ -94,7 +110,8 @@ export function handleSuccess(
94
110
95
111
export function functionToRoute (
96
112
fn : ServerlessFunctionSignature ,
97
- config : StartCliConfig
113
+ config : StartCliConfig ,
114
+ functionFilePath ?: string
98
115
) : ExpressRequestHandler {
99
116
constructGlobalScope ( config ) ;
100
117
@@ -114,7 +131,7 @@ export function functionToRoute(
114
131
) {
115
132
log ( 'Function execution %s finished' , req . path ) ;
116
133
if ( err ) {
117
- handleError ( err , res ) ;
134
+ handleError ( err , res , functionFilePath ) ;
118
135
return ;
119
136
}
120
137
handleSuccess ( responseObject , res ) ;
@@ -124,7 +141,7 @@ export function functionToRoute(
124
141
try {
125
142
fn ( context , event , callback ) ;
126
143
} catch ( err ) {
127
- callback ( err . message ) ;
144
+ callback ( err ) ;
128
145
}
129
146
} ;
130
147
}
0 commit comments