Skip to content

Commit 02543a7

Browse files
alexander-fensterJustinBeckwith
authored andcommitted
fix: do not use console.warn or console.error (#426)
1 parent 04f9157 commit 02543a7

File tree

6 files changed

+109
-6
lines changed

6 files changed

+109
-6
lines changed

packages/packages/google-gax/src/bundling.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {status} from 'grpc';
3838
import {NormalApiCaller, APICall, PromiseCanceller, APICallback} from './apiCallable';
3939
import {GoogleError} from './GoogleError';
4040
import {CallSettings} from './gax';
41+
import {warn} from './warnings';
4142

4243
/**
4344
* A function which does nothing. Used for an empty cancellation funciton.
@@ -361,11 +362,12 @@ export class BundleExecutor {
361362
computeBundleId(request, this._descriptor.requestDiscriminatorFields);
362363
callback = (callback || noop) as TaskCallback;
363364
if (bundleId === undefined) {
364-
console.warn(
365+
warn(
366+
'bundling_schedule_bundleid_undefined',
365367
'The request does not have enough information for request bundling. ' +
366-
'Invoking immediately. Request: ' + JSON.stringify(request) +
367-
' discriminator fields: ' +
368-
this._descriptor.requestDiscriminatorFields);
368+
`Invoking immediately. Request: ${JSON.stringify(request)} ` +
369+
`discriminator fields: ${
370+
this._descriptor.requestDiscriminatorFields}`);
369371
return apiCall(request, callback);
370372
}
371373

@@ -490,7 +492,7 @@ export class BundleExecutor {
490492
*/
491493
_runNow(bundleId: string) {
492494
if (!(bundleId in this._tasks)) {
493-
console.warn('no such bundleid: ' + bundleId);
495+
warn('bundle_runnow_bundleid_unknown', `No such bundleid: ${bundleId}`);
494496
return;
495497
}
496498
this._maybeClearTimeout(bundleId);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2019 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export function isBrowser(): boolean {
18+
return typeof (window) !== 'undefined';
19+
}

packages/packages/google-gax/src/streaming.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import * as Duplexify from 'duplexify';
3535
import {Duplex, DuplexOptions, Stream} from 'stream';
3636
import {APICall, APICallback} from './apiCallable';
37+
import {warn} from './warnings';
3738

3839
const retryRequest = require('retry-request');
3940

@@ -190,7 +191,9 @@ export class GrpcStreamable {
190191
return func(metadata, options);
191192
};
192193
default:
193-
console.error('Unknown stream type', this.descriptor.type);
194+
warn(
195+
'streaming_wrap_unknown_stream_type',
196+
`Unknown stream type: ${this.descriptor.type}`);
194197
}
195198
return func;
196199
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2019 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as semver from 'semver';
18+
import {isBrowser} from './isbrowser';
19+
20+
const emittedWarnings = new Set<string>();
21+
22+
export function warn(code: string, message: string) {
23+
// Only show a given warning once
24+
if (emittedWarnings.has(code)) {
25+
return;
26+
}
27+
emittedWarnings.add(code);
28+
29+
if (isBrowser()) {
30+
console.warn(message);
31+
} else {
32+
process.emitWarning(message);
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2019 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {assert} from 'chai';
18+
import * as sinon from 'sinon';
19+
20+
import {warn} from '../src/warnings';
21+
22+
describe('warnings', () => {
23+
it('should warn the given code once with the first message', (done) => {
24+
const stub = sinon.stub(process, 'emitWarning');
25+
warn('code1', 'message1-1');
26+
warn('code1', 'message1-2');
27+
warn('code1', 'message1-3');
28+
assert(stub.calledOnceWith('message1-1'));
29+
stub.restore();
30+
done();
31+
});
32+
it('should warn each code once', (done) => {
33+
const stub = sinon.stub(process, 'emitWarning');
34+
warn('codeA', 'messageA-1');
35+
warn('codeB', 'messageB-1');
36+
warn('codeA', 'messageA-2');
37+
warn('codeB', 'messageB-2');
38+
warn('codeC', 'messageC-1');
39+
warn('codeA', 'messageA-3');
40+
assert.strictEqual(stub.callCount, 3);
41+
stub.restore();
42+
done();
43+
});
44+
});

packages/packages/google-gax/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "./node_modules/gts/tsconfig-google.json",
33
"compilerOptions": {
4+
"lib": ["es2015", "dom"],
45
"rootDir": ".",
56
"outDir": "build",
67
"noImplicitAny": false

0 commit comments

Comments
 (0)