Skip to content

Commit

Permalink
fix(replicache): Fix error message when cookie is object and out of o…
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Jul 14, 2024
1 parent 408ec25 commit dd0e8ca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
16 changes: 15 additions & 1 deletion packages/replicache/src/sync/pull.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,21 @@ test('begin try pull DD31', async () => {
},
expNewSyncHead: undefined,
expBeginPullResult:
'Received cookie cookie_0 is < than last snapshot cookie cookie_1; ignoring client view',
'Received cookie "cookie_0" is < than last snapshot cookie "cookie_1"; ignoring client view',
},
{
name: 'pulls new state w/lesser cookie using _order -> beginPull errors',
numPendingMutations: 0,
pullResult: {
...goodPullResp,
cookie: {
foo: 'bar',
order: 'cookie_0',
},
},
expNewSyncHead: undefined,
expBeginPullResult:
'Received cookie {"foo":"bar","order":"cookie_0"} is < than last snapshot cookie "cookie_1"; ignoring client view',
},
{
name: 'pulls new state with identical client-lmid-changes in response (identical cookie and no patch)',
Expand Down
36 changes: 24 additions & 12 deletions packages/replicache/src/sync/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ export function handlePullResponseV0(
throw new Error(
badOrderMessage(
`lastMutationID`,
response.lastMutationID,
baseLastMutationID,
String(response.lastMutationID),
String(baseLastMutationID),
),
);
}
Expand All @@ -350,12 +350,16 @@ export function handlePullResponseV0(
if (deepEqual(frozenCookie, baseCookie)) {
if (response.patch.length > 0) {
lc.error?.(
`handlePullResponse: cookie ${baseCookie} did not change, but patch is not empty`,
`handlePullResponse: cookie ${JSON.stringify(
baseCookie,
)} did not change, but patch is not empty`,
);
}
if (response.lastMutationID !== baseLastMutationID) {
lc.error?.(
`handlePullResponse: cookie ${baseCookie} did not change, but lastMutationID did change`,
`handlePullResponse: cookie ${JSON.stringify(
baseCookie,
)} did not change, but lastMutationID did change`,
);
}
return {
Expand Down Expand Up @@ -451,8 +455,8 @@ type HandlePullResponseResult =

function badOrderMessage(
name: string,
receivedValue: unknown,
lastSnapshotValue: unknown,
receivedValue: string,
lastSnapshotValue: string,
) {
return `Received ${name} ${receivedValue} is < than last snapshot ${name} ${lastSnapshotValue}; ignoring client view`;
}
Expand Down Expand Up @@ -485,7 +489,7 @@ export function handlePullResponseV1(
// In DD31 this is expected to happen if a refresh occurs during a pull.
if (!deepEqual(expectedBaseCookie, baseCookie)) {
lc.debug?.(
'handlePullResponse: cookie mismatch, pull response is not applicable',
'handlePullResponse: cookie mismatch, response is not applicable',
);
return {
type: HandlePullResponseResultType.CookieMismatch,
Expand All @@ -501,8 +505,8 @@ export function handlePullResponseV1(
throw new Error(
badOrderMessage(
`${clientID} lastMutationID`,
lmidChange,
lastMutationID,
String(lmidChange),
String(lastMutationID),
),
);
}
Expand All @@ -511,20 +515,28 @@ export function handlePullResponseV1(
const frozenResponseCookie = deepFreeze(response.cookie);
if (compareCookies(frozenResponseCookie, baseCookie) < 0) {
throw new Error(
badOrderMessage('cookie', frozenResponseCookie, baseCookie),
badOrderMessage(
'cookie',
JSON.stringify(frozenResponseCookie),
JSON.stringify(baseCookie),
),
);
}

if (deepEqual(frozenResponseCookie, baseCookie)) {
if (response.patch.length > 0) {
lc.error?.(
`handlePullResponse: cookie ${baseCookie} did not change, but patch is not empty`,
`handlePullResponse: cookie ${JSON.stringify(
baseCookie,
)} did not change, but patch is not empty`,
);
}
if (Object.keys(response.lastMutationIDChanges).length > 0) {
console.log(response.lastMutationIDChanges);
lc.error?.(
`handlePullResponse: cookie ${baseCookie} did not change, but lastMutationIDChanges is not empty`,
`handlePullResponse: cookie ${JSON.stringify(
baseCookie,
)} did not change, but lastMutationIDChanges is not empty`,
);
}
// If the cookie doesn't change, it's a nop.
Expand Down

0 comments on commit dd0e8ca

Please sign in to comment.