@@ -24,9 +24,11 @@ import * as cors from 'cors';
2424import * as express from 'express' ;
2525import * as firebase from 'firebase-admin' ;
2626import * as _ from 'lodash' ;
27+
2728import { apps } from '../apps' ;
2829import { HttpsFunction , optionsToTrigger , Runnable } from '../cloud-functions' ;
2930import { DeploymentOptions } from '../function-configuration' ;
31+ import { warn , error } from '../logger' ;
3032
3133/** @hidden */
3234export interface Request extends express . Request {
@@ -285,13 +287,13 @@ interface HttpResponseBody {
285287function isValidRequest ( req : Request ) : req is HttpRequest {
286288 // The body must not be empty.
287289 if ( ! req . body ) {
288- console . warn ( 'Request is missing body.' ) ;
290+ warn ( 'Request is missing body.' ) ;
289291 return false ;
290292 }
291293
292294 // Make sure it's a POST.
293295 if ( req . method !== 'POST' ) {
294- console . warn ( 'Request has invalid method.' , req . method ) ;
296+ warn ( 'Request has invalid method.' , req . method ) ;
295297 return false ;
296298 }
297299
@@ -303,13 +305,13 @@ function isValidRequest(req: Request): req is HttpRequest {
303305 contentType = contentType . substr ( 0 , semiColon ) . trim ( ) ;
304306 }
305307 if ( contentType !== 'application/json' ) {
306- console . warn ( 'Request has incorrect Content-Type.' , contentType ) ;
308+ warn ( 'Request has incorrect Content-Type.' , contentType ) ;
307309 return false ;
308310 }
309311
310312 // The body must have data.
311313 if ( _ . isUndefined ( req . body . data ) ) {
312- console . warn ( 'Request body is missing data.' , req . body ) ;
314+ warn ( 'Request body is missing data.' , req . body ) ;
313315 return false ;
314316 }
315317
@@ -318,7 +320,7 @@ function isValidRequest(req: Request): req is HttpRequest {
318320 // Verify that the body does not have any extra fields.
319321 const extras = _ . omit ( req . body , 'data' ) ;
320322 if ( ! _ . isEmpty ( extras ) ) {
321- console . warn ( 'Request body has extra fields.' , extras ) ;
323+ warn ( 'Request body has extra fields.' , extras ) ;
322324 return false ;
323325 }
324326 return true ;
@@ -363,7 +365,7 @@ export function encode(data: any): any {
363365 return _ . mapValues ( data , encode ) ;
364366 }
365367 // 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 ) ;
367369 throw new Error ( 'Data cannot be encoded in JSON: ' + data ) ;
368370}
369371
@@ -386,13 +388,13 @@ export function decode(data: any): any {
386388 // worth all the extra code to detect that case.
387389 const value = parseFloat ( data . value ) ;
388390 if ( _ . isNaN ( value ) ) {
389- console . error ( 'Data cannot be decoded from JSON.' , data ) ;
391+ error ( 'Data cannot be decoded from JSON.' , data ) ;
390392 throw new Error ( 'Data cannot be decoded from JSON: ' + data ) ;
391393 }
392394 return value ;
393395 }
394396 default : {
395- console . error ( 'Data cannot be decoded from JSON.' , data ) ;
397+ error ( 'Data cannot be decoded from JSON.' , data ) ;
396398 throw new Error ( 'Data cannot be decoded from JSON: ' + data ) ;
397399 }
398400 }
@@ -420,7 +422,7 @@ export function _onCallWithOptions(
420422 const func = async ( req : Request , res : express . Response ) => {
421423 try {
422424 if ( ! isValidRequest ( req ) ) {
423- console . error ( 'Invalid request' , req ) ;
425+ error ( 'Invalid request, unable to process.' ) ;
424426 throw new HttpsError ( 'invalid-argument' , 'Bad Request' ) ;
425427 }
426428
@@ -441,7 +443,7 @@ export function _onCallWithOptions(
441443 uid : authToken . uid ,
442444 token : authToken ,
443445 } ;
444- } catch ( e ) {
446+ } catch ( err ) {
445447 throw new HttpsError ( 'unauthenticated' , 'Unauthenticated' ) ;
446448 }
447449 }
@@ -464,15 +466,15 @@ export function _onCallWithOptions(
464466 // If there was some result, encode it in the body.
465467 const responseBody : HttpResponseBody = { result } ;
466468 res . status ( 200 ) . send ( responseBody ) ;
467- } catch ( error ) {
468- if ( ! ( error instanceof HttpsError ) ) {
469+ } catch ( err ) {
470+ if ( ! ( err instanceof HttpsError ) ) {
469471 // 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' ) ;
472474 }
473475
474- const { status } = error . httpErrorCode ;
475- const body = { error : error . toJSON ( ) } ;
476+ const { status } = err . httpErrorCode ;
477+ const body = { error : err . toJSON ( ) } ;
476478
477479 res . status ( status ) . send ( body ) ;
478480 }
0 commit comments