Skip to content

Commit 2fbfba2

Browse files
committed
Added debug logs
1 parent cc9e33b commit 2fbfba2

File tree

13 files changed

+44
-44
lines changed

13 files changed

+44
-44
lines changed

packages/data-connect/src/api/DataConnect.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@
1717

1818
import {
1919
FirebaseApp,
20-
FirebaseError,
2120
_getProvider,
2221
_removeServiceInstance,
2322
getApp
2423
} from '@firebase/app';
2524
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
2625
import { Provider } from '@firebase/component';
2726

28-
import { AuthTokenProvider, EmulatorTokenProvider, FirebaseAuthProvider } from '../core/FirebaseAuthProvider';
27+
import {
28+
AuthTokenProvider,
29+
EmulatorTokenProvider,
30+
FirebaseAuthProvider
31+
} from '../core/FirebaseAuthProvider';
2932
import { QueryManager } from '../core/QueryManager';
3033
import { DataConnectTransport, TransportClass } from '../network';
3134
import { RESTTransport } from '../network/transport/rest';
3235

3336
import { MutationManager } from './Mutation';
3437
import { Code, DataConnectError } from '../core/error';
38+
import { logger } from '../logger';
3539

3640
export interface ProjectOptions {
3741
location: string;
@@ -83,6 +87,7 @@ export class DataConnect {
8387
if (typeof process !== 'undefined' && process.env) {
8488
const host = process.env[FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR];
8589
if (host) {
90+
logger.info("Found custom host. Using emulator");
8691
this.isEmulator = true;
8792
this.transportOptions = parseOptions(host);
8893
}
@@ -108,10 +113,10 @@ export class DataConnect {
108113
return;
109114
}
110115
if (this.transportClass === undefined) {
116+
logger.info("transportClass not provided. Defaulting to RESTTransport.");
111117
this.transportClass = RESTTransport;
112118
}
113119

114-
115120
if (this.authProvider) {
116121
this.authTokenProvider = this.isEmulator
117122
? new EmulatorTokenProvider(EmulatorTokenProvider.OWNER)
@@ -121,6 +126,7 @@ export class DataConnect {
121126
this.authProvider
122127
);
123128
this.authTokenProvider.addTokenChangeListener(token => {
129+
logger.info(`New Token Available: ${token}`);
124130
this._transport.onTokenChanged(token);
125131
});
126132
}
@@ -144,6 +150,7 @@ export class DataConnect {
144150

145151
enableEmulator(transportOptions: TransportOptions) {
146152
if (this.initialized) {
153+
logger.error("enableEmulator called without initializing");
147154
throw new DataConnectError(
148155
Code.ALREADY_INITIALIZED,
149156
'DataConnect instance already initialized!'
@@ -152,7 +159,6 @@ export class DataConnect {
152159
this.transportOptions = transportOptions;
153160
this.isEmulator = true;
154161
}
155-
156162
}
157163

158164
export function connectDataConnectEmulator(
@@ -193,12 +199,14 @@ export function getDataConnect(
193199
const options = provider.getOptions(identifier);
194200
const optionsValid = Object.keys(options).length > 0;
195201
if (optionsValid) {
202+
logger.debug("Re-using cached instance");
196203
return dcInstance;
197204
}
198205
}
199206
if (!dcOptions) {
200207
throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
201208
}
209+
logger.debug("Creating new DataConnect instance");
202210
// Initialize with options.
203211
return provider.initialize({
204212
instanceIdentifier: identifier,

packages/data-connect/src/api/Mutation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { logger } from '../logger';
1819
import { DataConnectTransport } from '../network/transport';
1920

2021
import { DataConnect } from './DataConnect';

packages/data-connect/src/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export * from './DataConnect';
2020
export * from './Reference';
2121
export * from './Mutation';
2222
export * from './query';
23+
export * from '../logger';

packages/data-connect/src/core/FirebaseAuthProvider.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import {
2222
FirebaseAuthTokenData
2323
} from '@firebase/auth-interop-types';
2424
import { Provider } from '@firebase/component';
25+
import { logger } from '../logger';
2526

2627
export interface AuthTokenProvider {
2728
getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData | null>;
2829
addTokenChangeListener(listener: AuthTokenListener): void;
2930
}
3031
export type AuthTokenListener = (token: string | null) => void;
3132

32-
// Mostly borrowed from packages/database/src/core/AuthTokenProvider.ts
3333
export class FirebaseAuthProvider implements AuthTokenProvider {
3434
private auth_: FirebaseAuthInternal;
3535
constructor(
@@ -56,12 +56,14 @@ export class FirebaseAuthProvider implements AuthTokenProvider {
5656
}
5757
return this.auth_.getToken(forceRefresh).catch(error => {
5858
if (error && error.code === 'auth/token-not-initialized') {
59-
// TODO(mtewani): Implement logging mechanism
60-
// log(
61-
// 'Got auth/token-not-initialized error. Treating as null token.'
62-
// );
59+
logger.debug(
60+
'Got auth/token-not-initialized error. Treating as null token.'
61+
);
6362
return null;
6463
} else {
64+
logger.error(
65+
'Error received when attempting to retrieve token: ' + JSON.stringify(error)
66+
);
6567
return Promise.reject(error);
6668
}
6769
});

packages/data-connect/src/core/QueryManager.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
DataSource,
3333
SOURCE_CACHE
3434
} from '../api/Reference';
35+
import { logger } from '../logger';
3536
import { DataConnectTransport } from '../network';
3637
import { encoderImpl } from '../util/encoder';
3738
import { setIfNotExists } from '../util/map';
@@ -118,6 +119,7 @@ export class QueryManager {
118119
);
119120
};
120121
if (initialCache && trackedQuery.currentCache !== initialCache) {
122+
logger.debug('Initial cache found. Comparing dates.');
121123
if (
122124
!trackedQuery.currentCache ||
123125
(trackedQuery.currentCache &&
@@ -153,6 +155,7 @@ export class QueryManager {
153155
unsubscribe
154156
});
155157
if (!trackedQuery.currentCache) {
158+
logger.info(`No cache available for query ${queryRef.name} with variables ${JSON.stringify(queryRef.variables)}. Calling executeQuery.`);
156159
const promise = this.executeQuery(
157160
queryRef as QueryRef<Response, Variables>
158161
);

packages/data-connect/src/core/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const Code = {
3636
PARTIAL_ERROR: 'partial-error' as DataConnectErrorCode
3737
};
3838

39-
/** An error returned by a Firestore operation. */
39+
/** An error returned by a DataConnect operation. */
4040
export class DataConnectError extends FirebaseError {
4141
/** The stack of the error. */
4242
readonly stack?: string;

packages/data-connect/src/network/fetch.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import { Code, DataConnectError } from '../core/error';
19+
import { logger } from '../logger';
1920

2021
let connectFetch: typeof fetch | null = globalThis.fetch;
2122
export function initializeFetch(fetchImpl: typeof fetch) {
@@ -36,8 +37,10 @@ export function dcFetch<T, U>(
3637
if (accessToken) {
3738
headers['X-Firebase-Auth-Token'] = accessToken;
3839
}
40+
const bodyStr = JSON.stringify(body);
41+
logger.info(`Making request out to ${url} with body: ${bodyStr}`);
3942
return connectFetch(url, {
40-
body: JSON.stringify(body),
43+
body: bodyStr,
4144
method: 'POST',
4245
headers,
4346
signal
@@ -50,13 +53,16 @@ export function dcFetch<T, U>(
5053
throw new DataConnectError(Code.OTHER, JSON.stringify(e));
5154
}
5255
if (response.status >= 400) {
56+
logger.error("Error while performing request: " + JSON.stringify(jsonResponse));
5357
throw new DataConnectError(Code.OTHER, JSON.stringify(jsonResponse));
5458
}
5559
return jsonResponse;
5660
})
5761
.then(res => {
5862
if (res.errors && res.errors.length) {
59-
throw new DataConnectError(Code.OTHER, JSON.stringify(res.errors));
63+
const stringified = JSON.stringify(res.errors);
64+
logger.error("DataConnect error while performing request: " + stringified);
65+
throw new DataConnectError(Code.OTHER, stringified);
6066
}
6167
return res as { data: T; errors: Error[] };
6268
});

packages/data-connect/src/network/transport/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*/
1717

1818
import { DataConnectOptions, TransportOptions } from '../../api/DataConnect';
19-
import { AuthTokenProvider, FirebaseAuthProvider } from '../../core/FirebaseAuthProvider';
19+
import {
20+
AuthTokenProvider,
21+
} from '../../core/FirebaseAuthProvider';
2022

2123
// Change this to only specify specific args.
2224
export interface DataConnectTransport {

packages/data-connect/src/network/transport/rest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import { DataConnectTransport } from '.';
1919
import { DataConnectOptions, TransportOptions } from '../../api/DataConnect';
2020
import { DataConnectError, Code } from '../../core/error';
21-
import { AuthTokenProvider, FirebaseAuthProvider } from '../../core/FirebaseAuthProvider';
21+
import {
22+
AuthTokenProvider,
23+
} from '../../core/FirebaseAuthProvider';
2224
import { addToken, urlBuilder } from '../../util/url';
2325
import { dcFetch } from '../fetch';
2426

packages/data-connect/src/register.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2021 Google LLC
3+
* Copyright 2024 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -32,10 +32,7 @@ export function registerDataConnect(variant?: string): void {
3232
_registerComponent(
3333
new Component(
3434
'data-connect',
35-
(
36-
container,
37-
{ instanceIdentifier: settings, options }
38-
) => {
35+
(container, { instanceIdentifier: settings, options }) => {
3936
const app = container.getProvider('app').getImmediate()!;
4037
const authProvider = container.getProvider('auth-internal');
4138
let newOpts = options as ConnectorConfig;

0 commit comments

Comments
 (0)