Skip to content

Commit 040e2ca

Browse files
committed
Ensure schema failures log for users without need for a callback
1 parent 3c9d9ed commit 040e2ca

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

.changeset/tender-seas-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@triplit/client': patch
3+
---
4+
5+
Ensure schema failures log for users without need for a callback

packages/client/src/client/triplit-client.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ export class TriplitClient<M extends Models<M> = Models> {
118118
kv: storage,
119119
clientId: Math.random().toString(36).substring(7),
120120
}).then(async ({ db, event }) => {
121+
this.logger = options.logger ?? LOGGER;
122+
this.logger.registerHandler(clientLogHandler());
123+
if (options.logLevel) {
124+
this.logger.setLogLevel(options.logLevel);
125+
}
126+
// With debug logging, store logs for access
127+
if (options.logLevel === 'debug') {
128+
this.logger.registerHandler(new MemoryHandler());
129+
}
130+
121131
// If we have a session set up at this point, use that info
122132
const decoded = this.token
123133
? decodeToken(this.token, this.claimsPath)
@@ -167,6 +177,21 @@ export class TriplitClient<M extends Models<M> = Models> {
167177
}
168178

169179
// Wait for a valid db
180+
if (event.type !== 'SUCCESS') {
181+
// TODO: add test for logging
182+
if (event.type === 'SCHEMA_UPDATE_FAILED') {
183+
this.logger.error(
184+
'Schema update failed during initialization. The schema will fallback to the value saved in the database (change.oldSchema). For more control, set a callback in experimental.onDatabaseInit.',
185+
event
186+
);
187+
} else {
188+
this.logger.error(
189+
'An error occurred during database initialization',
190+
event
191+
);
192+
}
193+
}
194+
170195
if (options.experimental?.onDatabaseInit) {
171196
await options.experimental?.onDatabaseInit(this.db, event);
172197
}
@@ -176,16 +201,6 @@ export class TriplitClient<M extends Models<M> = Models> {
176201
});
177202
});
178203

179-
this.logger = options.logger ?? LOGGER;
180-
this.logger.registerHandler(clientLogHandler());
181-
if (options.logLevel) {
182-
this.logger.setLogLevel(options.logLevel);
183-
}
184-
// With debug logging, store logs for access
185-
if (options.logLevel === 'debug') {
186-
this.logger.registerHandler(new MemoryHandler());
187-
}
188-
189204
this.claimsPath = options.claimsPath;
190205

191206
this.defaultFetchOptions = {

0 commit comments

Comments
 (0)