@@ -24,9 +24,11 @@ import * as cors from 'cors';
24
24
import * as express from 'express' ;
25
25
import * as firebase from 'firebase-admin' ;
26
26
import * as _ from 'lodash' ;
27
+
27
28
import { apps } from '../apps' ;
28
29
import { HttpsFunction , optionsToTrigger , Runnable } from '../cloud-functions' ;
29
30
import { DeploymentOptions } from '../function-configuration' ;
31
+ import { warn , error } from '../logger' ;
30
32
31
33
/** @hidden */
32
34
export interface Request extends express . Request {
@@ -285,13 +287,13 @@ interface HttpResponseBody {
285
287
function isValidRequest ( req : Request ) : req is HttpRequest {
286
288
// The body must not be empty.
287
289
if ( ! req . body ) {
288
- console . warn ( 'Request is missing body.' ) ;
290
+ warn ( 'Request is missing body.' ) ;
289
291
return false ;
290
292
}
291
293
292
294
// Make sure it's a POST.
293
295
if ( req . method !== 'POST' ) {
294
- console . warn ( 'Request has invalid method.' , req . method ) ;
296
+ warn ( 'Request has invalid method.' , req . method ) ;
295
297
return false ;
296
298
}
297
299
@@ -303,13 +305,13 @@ function isValidRequest(req: Request): req is HttpRequest {
303
305
contentType = contentType . substr ( 0 , semiColon ) . trim ( ) ;
304
306
}
305
307
if ( contentType !== 'application/json' ) {
306
- console . warn ( 'Request has incorrect Content-Type.' , contentType ) ;
308
+ warn ( 'Request has incorrect Content-Type.' , contentType ) ;
307
309
return false ;
308
310
}
309
311
310
312
// The body must have data.
311
313
if ( _ . isUndefined ( req . body . data ) ) {
312
- console . warn ( 'Request body is missing data.' , req . body ) ;
314
+ warn ( 'Request body is missing data.' , req . body ) ;
313
315
return false ;
314
316
}
315
317
@@ -318,7 +320,7 @@ function isValidRequest(req: Request): req is HttpRequest {
318
320
// Verify that the body does not have any extra fields.
319
321
const extras = _ . omit ( req . body , 'data' ) ;
320
322
if ( ! _ . isEmpty ( extras ) ) {
321
- console . warn ( 'Request body has extra fields.' , extras ) ;
323
+ warn ( 'Request body has extra fields.' , extras ) ;
322
324
return false ;
323
325
}
324
326
return true ;
@@ -363,7 +365,7 @@ export function encode(data: any): any {
363
365
return _ . mapValues ( data , encode ) ;
364
366
}
365
367
// If we got this far, the data is not encodable.
366
- console . error ( 'Data cannot be encoded in JSON.' , data ) ;
368
+ error ( 'Data cannot be encoded in JSON.' , data ) ;
367
369
throw new Error ( 'Data cannot be encoded in JSON: ' + data ) ;
368
370
}
369
371
@@ -386,13 +388,13 @@ export function decode(data: any): any {
386
388
// worth all the extra code to detect that case.
387
389
const value = parseFloat ( data . value ) ;
388
390
if ( _ . isNaN ( value ) ) {
389
- console . error ( 'Data cannot be decoded from JSON.' , data ) ;
391
+ error ( 'Data cannot be decoded from JSON.' , data ) ;
390
392
throw new Error ( 'Data cannot be decoded from JSON: ' + data ) ;
391
393
}
392
394
return value ;
393
395
}
394
396
default : {
395
- console . error ( 'Data cannot be decoded from JSON.' , data ) ;
397
+ error ( 'Data cannot be decoded from JSON.' , data ) ;
396
398
throw new Error ( 'Data cannot be decoded from JSON: ' + data ) ;
397
399
}
398
400
}
@@ -420,7 +422,7 @@ export function _onCallWithOptions(
420
422
const func = async ( req : Request , res : express . Response ) => {
421
423
try {
422
424
if ( ! isValidRequest ( req ) ) {
423
- console . error ( 'Invalid request' , req ) ;
425
+ error ( 'Invalid request, unable to process.' ) ;
424
426
throw new HttpsError ( 'invalid-argument' , 'Bad Request' ) ;
425
427
}
426
428
@@ -441,7 +443,7 @@ export function _onCallWithOptions(
441
443
uid : authToken . uid ,
442
444
token : authToken ,
443
445
} ;
444
- } catch ( e ) {
446
+ } catch ( err ) {
445
447
throw new HttpsError ( 'unauthenticated' , 'Unauthenticated' ) ;
446
448
}
447
449
}
@@ -464,15 +466,15 @@ export function _onCallWithOptions(
464
466
// If there was some result, encode it in the body.
465
467
const responseBody : HttpResponseBody = { result } ;
466
468
res . status ( 200 ) . send ( responseBody ) ;
467
- } catch ( error ) {
468
- if ( ! ( error instanceof HttpsError ) ) {
469
+ } catch ( err ) {
470
+ if ( ! ( err instanceof HttpsError ) ) {
469
471
// This doesn't count as an 'explicit' error.
470
- console . error ( 'Unhandled error' , error ) ;
471
- error = new HttpsError ( 'internal' , 'INTERNAL' ) ;
472
+ error ( 'Unhandled error' , error ) ;
473
+ err = new HttpsError ( 'internal' , 'INTERNAL' ) ;
472
474
}
473
475
474
- const { status } = error . httpErrorCode ;
475
- const body = { error : error . toJSON ( ) } ;
476
+ const { status } = err . httpErrorCode ;
477
+ const body = { error : err . toJSON ( ) } ;
476
478
477
479
res . status ( status ) . send ( body ) ;
478
480
}
0 commit comments