@@ -35,13 +35,16 @@ export interface Logger {
3535 fatal ( ...args : Array < { } > ) : void ;
3636}
3737
38+ export type ReportMode = 'production' | 'always' | 'never' ;
39+
3840export interface ConfigurationOptions {
3941 projectId ?: string ;
4042 keyFilename ?: string ;
4143 logLevel ?: string | number ;
4244 key ?: string ;
4345 serviceContext ?: { service ?: string ; version ?: string ; } ;
4446 ignoreEnvironmentCheck ?: boolean ;
47+ reportMode ?: ReportMode ;
4548 credentials ?: { } ;
4649 reportUnhandledRejections ?: boolean ;
4750}
@@ -74,7 +77,7 @@ export interface ServiceContext {
7477 */
7578export class Configuration {
7679 _logger : Logger ;
77- _shouldReportErrorsToAPI : boolean ;
80+ _reportMode : ReportMode ;
7881 _projectId : string | null ;
7982 _key : string | null ;
8083 keyFilename : string | null ;
@@ -89,23 +92,7 @@ export class Configuration {
8992 * for configuration logging purposes.
9093 */
9194 this . _logger = logger ;
92- /**
93- * The _shouldReportErrorsToAPI property is meant to denote whether or not
94- * the Stackdriver error reporting library will actually try to report
95- * Errors to the Stackdriver Error API. The value of this property is
96- * derived from the `NODE_ENV` environmental variable or the value of
97- * ignoreEnvironmentChec property if present in the runtime configuration.
98- * If either the `NODE_ENV` variable is set to 'production' or the
99- * ignoreEnvironmentCheck propery on the runtime configuration is set to
100- * true then the error reporting library attempt to send errors to the Error
101- * API. Otherwise the value will remain false and errors will not be
102- * reported to the API.
103- * @memberof Configuration
104- * @private
105- * @type {Boolean }
106- * @defaultvalue false
107- */
108- this . _shouldReportErrorsToAPI = false ;
95+ this . _reportMode = 'production' ;
10996 /**
11097 * The _projectId property is meant to contain the string project id that
11198 * the hosting application is running under. The project id is a unique
@@ -259,6 +246,12 @@ export class Configuration {
259246 }
260247 }
261248 }
249+ _determineReportMode ( ) {
250+ if ( this . _givenConfiguration . reportMode ) {
251+ this . _reportMode =
252+ this . _givenConfiguration . reportMode . toLowerCase ( ) as ReportMode ;
253+ }
254+ }
262255 /**
263256 * The _gatherLocalConfiguration function is responsible for determining
264257 * directly determing whether the properties `reportUncaughtExceptions` and
@@ -273,23 +266,56 @@ export class Configuration {
273266 * @returns {Undefined } - does not return anything
274267 */
275268 _gatherLocalConfiguration ( ) {
276- if ( this . _givenConfiguration . ignoreEnvironmentCheck === true ) {
277- this . _shouldReportErrorsToAPI = true ;
278- } else if (
279- has ( this . _givenConfiguration , 'ignoreEnvironmentCheck' ) &&
280- ! is . boolean ( this . _givenConfiguration . ignoreEnvironmentCheck ) ) {
281- throw new Error ( 'config.ignoreEnvironmentCheck must be a boolean' ) ;
282- } else {
283- this . _shouldReportErrorsToAPI = env . NODE_ENV === 'production' ;
269+ let isReportModeValid = true ;
270+ if ( has ( this . _givenConfiguration , 'reportMode' ) ) {
271+ const reportMode = this . _givenConfiguration . reportMode ;
272+ isReportModeValid = is . string ( reportMode ) &&
273+ ( reportMode === 'production' || reportMode === 'always' ||
274+ reportMode === 'never' ) ;
275+ }
276+
277+ if ( ! isReportModeValid ) {
278+ throw new Error (
279+ 'config.reportMode must a string that is one ' +
280+ 'of "production", "always", or "never".' ) ;
284281 }
285- if ( ! this . _shouldReportErrorsToAPI ) {
282+
283+ const hasEnvCheck = has ( this . _givenConfiguration , 'ignoreEnvironmentCheck' ) ;
284+ const hasReportMode = has ( this . _givenConfiguration , 'reportMode' ) ;
285+ if ( hasEnvCheck ) {
286+ this . _logger . warn (
287+ 'The "ignoreEnvironmentCheck" config option is deprecated. ' +
288+ 'Use the "reportMode" config option instead.' ) ;
289+ }
290+ if ( hasEnvCheck && hasReportMode ) {
286291 this . _logger . warn ( [
287- 'Stackdriver error reporting client has not been configured to send' ,
288- 'errors, please check the NODE_ENV environment variable and make sure it' ,
289- 'is set to "production" or the ignoreEnvironmentCheck property is set to' ,
290- 'true in the runtime configuration object' ,
292+ 'Both the "ignoreEnvironmentCheck" and "reportMode" configuration options' ,
293+ 'have been specified. The "reportMode" option will take precedence.'
291294 ] . join ( ' ' ) ) ;
295+ this . _determineReportMode ( ) ;
296+ } else if ( hasEnvCheck ) {
297+ if ( this . _givenConfiguration . ignoreEnvironmentCheck === true ) {
298+ this . _reportMode = 'always' ;
299+ } else if (
300+ has ( this . _givenConfiguration , 'ignoreEnvironmentCheck' ) &&
301+ ! is . boolean ( this . _givenConfiguration . ignoreEnvironmentCheck ) ) {
302+ throw new Error ( 'config.ignoreEnvironmentCheck must be a boolean' ) ;
303+ } else {
304+ this . _reportMode = 'production' ;
305+ }
306+ } else if ( hasReportMode ) {
307+ this . _determineReportMode ( ) ;
292308 }
309+
310+ if ( this . isReportingEnabled ( ) && ! this . getShouldReportErrorsToAPI ( ) ) {
311+ this . _logger . warn ( [
312+ 'The stackdriver error reporting client is configured to report errors' ,
313+ 'if and only if the NODE_ENV environment variable is set to "production".' ,
314+ 'Errors will not be reported. To have errors always reported, regardless of the' ,
315+ 'value of NODE_ENV, set the reportMode configuration option to "always".'
316+ ] . join ( ' ' ) ) ;
317+ }
318+
293319 if ( is . string ( this . _givenConfiguration . key ) ) {
294320 this . _key = this . _givenConfiguration . key ! ;
295321 } else if ( has ( this . _givenConfiguration , 'key' ) ) {
@@ -349,14 +375,22 @@ export class Configuration {
349375 return this . _projectId ;
350376 }
351377 /**
352- * Returns the _shouldReportErrorsToAPI property on the instance.
378+ * Returns whether this configuration specifies that errors should be
379+ * reported to the error reporting API. That is, "reportMode" is
380+ * either set to "always" or it is set to "production" and the value
381+ * of the NODE_ENV environment variable is "production".
353382 * @memberof Configuration
354383 * @public
355384 * @function getShouldReportErrorsToAPI
356- * @returns {Boolean } - returns the _shouldReportErrorsToAPI property
385+ * @returns {Boolean } - whether errors should be reported to the API
357386 */
358387 getShouldReportErrorsToAPI ( ) {
359- return this . _shouldReportErrorsToAPI ;
388+ return this . _reportMode === 'always' ||
389+ ( this . _reportMode === 'production' &&
390+ ( process . env . NODE_ENV || '' ) . toLowerCase ( ) === 'production' ) ;
391+ }
392+ isReportingEnabled ( ) {
393+ return this . _reportMode !== 'never' ;
360394 }
361395 /**
362396 * Returns the _projectId property on the instance.
0 commit comments