[codex] Structure relay Live Activity errors (#3314)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
5f860439e9
commit
e06c33c38d
2 files changed
+241
-74
No files matched your search
@@ -197,4 +197,92 @@ describe("LiveActivities", () => {
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("preserves correlation context and causes for persistence failures", () => {
|
||||
const cause = new Error("database unavailable");
|
||||
const registration: RelayLiveActivityRegistrationRequest = {
|
||||
deviceId: "device-1" as RelayLiveActivityRegistrationRequest["deviceId"],
|
||||
activityPushToken:
|
||||
"activity-push-token" as RelayLiveActivityRegistrationRequest["activityPushToken"],
|
||||
};
|
||||
const fakeDb = {
|
||||
update: () => ({
|
||||
set: () => ({ where: () => Effect.fail(cause) }),
|
||||
}),
|
||||
insert: () => ({
|
||||
values: () => ({ onConflictDoUpdate: () => Effect.fail(cause) }),
|
||||
}),
|
||||
select: () => ({
|
||||
from: () => ({
|
||||
leftJoin: () => ({ where: () => Effect.fail(cause) }),
|
||||
}),
|
||||
}),
|
||||
} as unknown as RelayDb.RelayDb["Service"];
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const liveActivities = yield* LiveActivities.LiveActivities;
|
||||
const registrationError = yield* Effect.flip(
|
||||
liveActivities.register({ userId: "user-1", registration }),
|
||||
);
|
||||
const targetListError = yield* Effect.flip(liveActivities.listTargets({ userId: "user-1" }));
|
||||
const deliveryErrors = yield* Effect.all(
|
||||
[
|
||||
liveActivities.markDelivery({
|
||||
userId: "user-1",
|
||||
deviceId: "device-1",
|
||||
kind: "live_activity_update",
|
||||
aggregate: null,
|
||||
deliveredAt: "2026-05-25T00:00:10.000Z",
|
||||
}),
|
||||
liveActivities.markStartQueued({
|
||||
userId: "user-1",
|
||||
deviceId: "device-1",
|
||||
queuedAt: "2026-05-25T00:00:10.000Z",
|
||||
}),
|
||||
liveActivities.clearStartQueued({ userId: "user-1", deviceId: "device-1" }),
|
||||
liveActivities.invalidateDeliveryToken({
|
||||
userId: "user-1",
|
||||
deviceId: "device-1",
|
||||
kind: "push_notification",
|
||||
invalidatedAt: "2026-05-25T00:00:10.000Z",
|
||||
}),
|
||||
].map(Effect.flip),
|
||||
{ concurrency: 1 },
|
||||
);
|
||||
|
||||
expect(registrationError).toMatchObject({
|
||||
userId: "user-1",
|
||||
deviceId: "device-1",
|
||||
cause,
|
||||
message:
|
||||
"Failed to persist Live Activity registration for user user-1 and device device-1.",
|
||||
});
|
||||
expect(targetListError).toMatchObject({
|
||||
userId: "user-1",
|
||||
cause,
|
||||
message: "Failed to list Live Activity delivery targets for user user-1.",
|
||||
});
|
||||
|
||||
const expectedDeliveryContext = [
|
||||
["mark-delivery", "live_activity_update"],
|
||||
["mark-start-queued", null],
|
||||
["clear-start-queued", null],
|
||||
["invalidate-delivery-token", "push_notification"],
|
||||
] as const;
|
||||
for (const [index, [operation, kind]] of expectedDeliveryContext.entries()) {
|
||||
expect(deliveryErrors[index]).toMatchObject({
|
||||
operation,
|
||||
userId: "user-1",
|
||||
deviceId: "device-1",
|
||||
kind,
|
||||
cause,
|
||||
message: `Failed to persist Live Activity state during ${operation} for user user-1 and device device-1.`,
|
||||
});
|
||||
}
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
LiveActivities.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -3,7 +3,10 @@ import type {
|
||||
RelayDeliveryKind,
|
||||
RelayLiveActivityRegistrationRequest,
|
||||
} from "@t3tools/contracts/relay";
|
||||
import { RelayAgentActivityAggregateState as RelayAgentActivityAggregateStateSchema } from "@t3tools/contracts/relay";
|
||||
import {
|
||||
RelayAgentActivityAggregateState as RelayAgentActivityAggregateStateSchema,
|
||||
RelayDeliveryKind as RelayDeliveryKindSchema,
|
||||
} from "@t3tools/contracts/relay";
|
||||
import * as Context from "effect/Context";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Effect from "effect/Effect";
|
||||
@@ -17,28 +20,46 @@ import { relayLiveActivities, relayMobileDevices } from "../persistence/schema.t
|
||||
|
||||
export class LiveActivityRegistrationPersistenceError extends Schema.TaggedErrorClass<LiveActivityRegistrationPersistenceError>()(
|
||||
"LiveActivityRegistrationPersistenceError",
|
||||
{ cause: Schema.Defect() },
|
||||
{
|
||||
userId: Schema.String,
|
||||
deviceId: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return "Failed to persist Live Activity registration";
|
||||
return `Failed to persist Live Activity registration for user ${this.userId} and device ${this.deviceId}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class LiveActivityTargetListPersistenceError extends Schema.TaggedErrorClass<LiveActivityTargetListPersistenceError>()(
|
||||
"LiveActivityTargetListPersistenceError",
|
||||
{ cause: Schema.Defect() },
|
||||
{
|
||||
userId: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return "Failed to list Live Activity delivery targets";
|
||||
return `Failed to list Live Activity delivery targets for user ${this.userId}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class LiveActivityDeliveryMarkPersistenceError extends Schema.TaggedErrorClass<LiveActivityDeliveryMarkPersistenceError>()(
|
||||
"LiveActivityDeliveryMarkPersistenceError",
|
||||
{ cause: Schema.Defect() },
|
||||
{
|
||||
operation: Schema.Literals([
|
||||
"mark-delivery",
|
||||
"mark-start-queued",
|
||||
"clear-start-queued",
|
||||
"invalidate-delivery-token",
|
||||
]),
|
||||
userId: Schema.String,
|
||||
deviceId: Schema.String,
|
||||
kind: Schema.NullOr(RelayDeliveryKindSchema),
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return "Failed to persist Live Activity delivery state";
|
||||
return `Failed to persist Live Activity state during ${this.operation} for user ${this.userId} and device ${this.deviceId}.`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,11 +131,11 @@ export const make = Effect.gen(function* () {
|
||||
const db = yield* RelayDb.RelayDb;
|
||||
|
||||
return LiveActivities.of({
|
||||
register: Effect.fn("relay.live_activities.register")(
|
||||
function* (input) {
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"relay.mobile.device_id": input.registration.deviceId,
|
||||
});
|
||||
register: Effect.fn("relay.live_activities.register")(function* (input) {
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"relay.mobile.device_id": input.registration.deviceId,
|
||||
});
|
||||
yield* Effect.gen(function* () {
|
||||
const updatedAt = DateTime.formatIso(yield* DateTime.now);
|
||||
const registration = input.registration;
|
||||
|
||||
@@ -155,9 +176,17 @@ export const make = Effect.gen(function* () {
|
||||
updatedAt,
|
||||
},
|
||||
});
|
||||
},
|
||||
Effect.mapError((cause) => new LiveActivityRegistrationPersistenceError({ cause })),
|
||||
),
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new LiveActivityRegistrationPersistenceError({
|
||||
userId: input.userId,
|
||||
deviceId: input.registration.deviceId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}),
|
||||
|
||||
listTargets: Effect.fn("relay.live_activities.list_targets")(function* (input) {
|
||||
return yield* db
|
||||
@@ -207,16 +236,22 @@ export const make = Effect.gen(function* () {
|
||||
),
|
||||
),
|
||||
Effect.map((rows): ReadonlyArray<TargetRow> => rows),
|
||||
Effect.mapError((cause) => new LiveActivityTargetListPersistenceError({ cause })),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new LiveActivityTargetListPersistenceError({
|
||||
userId: input.userId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}),
|
||||
|
||||
markDelivery: Effect.fn("relay.live_activities.mark_delivery")(
|
||||
function* (input) {
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"relay.mobile.device_id": input.deviceId,
|
||||
"relay.delivery.kind": input.kind,
|
||||
});
|
||||
markDelivery: Effect.fn("relay.live_activities.mark_delivery")(function* (input) {
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"relay.mobile.device_id": input.deviceId,
|
||||
"relay.delivery.kind": input.kind,
|
||||
});
|
||||
yield* Effect.gen(function* () {
|
||||
const aggregateJson =
|
||||
input.aggregate === null
|
||||
? null
|
||||
@@ -257,9 +292,19 @@ export const make = Effect.gen(function* () {
|
||||
updatedAt: input.deliveredAt,
|
||||
},
|
||||
});
|
||||
},
|
||||
Effect.mapError((cause) => new LiveActivityDeliveryMarkPersistenceError({ cause })),
|
||||
),
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new LiveActivityDeliveryMarkPersistenceError({
|
||||
operation: "mark-delivery",
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
kind: input.kind,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}),
|
||||
|
||||
markStartQueued: Effect.fn("relay.live_activities.mark_start_queued")(function* (input) {
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
@@ -287,7 +332,18 @@ export const make = Effect.gen(function* () {
|
||||
updatedAt: input.queuedAt,
|
||||
},
|
||||
})
|
||||
.pipe(Effect.mapError((cause) => new LiveActivityDeliveryMarkPersistenceError({ cause })));
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new LiveActivityDeliveryMarkPersistenceError({
|
||||
operation: "mark-start-queued",
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
kind: null,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}),
|
||||
|
||||
clearStartQueued: Effect.fn("relay.live_activities.clear_start_queued")(function* (input) {
|
||||
@@ -303,7 +359,18 @@ export const make = Effect.gen(function* () {
|
||||
eq(relayLiveActivities.deviceId, input.deviceId),
|
||||
),
|
||||
)
|
||||
.pipe(Effect.mapError((cause) => new LiveActivityDeliveryMarkPersistenceError({ cause })));
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new LiveActivityDeliveryMarkPersistenceError({
|
||||
operation: "clear-start-queued",
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
kind: null,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}),
|
||||
|
||||
invalidateDeliveryToken: Effect.fn("relay.live_activities.invalidate_delivery_token")(
|
||||
@@ -312,39 +379,58 @@ export const make = Effect.gen(function* () {
|
||||
"relay.mobile.device_id": input.deviceId,
|
||||
"relay.delivery.kind": input.kind,
|
||||
});
|
||||
if (input.kind === "push_notification") {
|
||||
yield* db
|
||||
.update(relayMobileDevices)
|
||||
.set({
|
||||
pushToken: null,
|
||||
updatedAt: input.invalidatedAt,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(relayMobileDevices.userId, input.userId),
|
||||
eq(relayMobileDevices.deviceId, input.deviceId),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
yield* Effect.gen(function* () {
|
||||
if (input.kind === "push_notification") {
|
||||
yield* db
|
||||
.update(relayMobileDevices)
|
||||
.set({
|
||||
pushToken: null,
|
||||
updatedAt: input.invalidatedAt,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(relayMobileDevices.userId, input.userId),
|
||||
eq(relayMobileDevices.deviceId, input.deviceId),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (input.kind === "live_activity_start") {
|
||||
yield* db
|
||||
.update(relayMobileDevices)
|
||||
.set({
|
||||
pushToStartToken: null,
|
||||
updatedAt: input.invalidatedAt,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(relayMobileDevices.userId, input.userId),
|
||||
eq(relayMobileDevices.deviceId, input.deviceId),
|
||||
),
|
||||
);
|
||||
yield* db
|
||||
.update(relayLiveActivities)
|
||||
.set({
|
||||
remoteStartQueuedAt: null,
|
||||
updatedAt: input.invalidatedAt,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(relayLiveActivities.userId, input.userId),
|
||||
eq(relayLiveActivities.deviceId, input.deviceId),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (input.kind === "live_activity_start") {
|
||||
yield* db
|
||||
.update(relayMobileDevices)
|
||||
.set({
|
||||
pushToStartToken: null,
|
||||
updatedAt: input.invalidatedAt,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(relayMobileDevices.userId, input.userId),
|
||||
eq(relayMobileDevices.deviceId, input.deviceId),
|
||||
),
|
||||
);
|
||||
yield* db
|
||||
.update(relayLiveActivities)
|
||||
.set({
|
||||
activityPushToken: null,
|
||||
remoteStartQueuedAt: null,
|
||||
remoteStartedAt: null,
|
||||
endedAt: input.invalidatedAt,
|
||||
updatedAt: input.invalidatedAt,
|
||||
})
|
||||
.where(
|
||||
@@ -353,26 +439,19 @@ export const make = Effect.gen(function* () {
|
||||
eq(relayLiveActivities.deviceId, input.deviceId),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
yield* db
|
||||
.update(relayLiveActivities)
|
||||
.set({
|
||||
activityPushToken: null,
|
||||
remoteStartQueuedAt: null,
|
||||
remoteStartedAt: null,
|
||||
endedAt: input.invalidatedAt,
|
||||
updatedAt: input.invalidatedAt,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(relayLiveActivities.userId, input.userId),
|
||||
eq(relayLiveActivities.deviceId, input.deviceId),
|
||||
),
|
||||
);
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new LiveActivityDeliveryMarkPersistenceError({
|
||||
operation: "invalidate-delivery-token",
|
||||
userId: input.userId,
|
||||
deviceId: input.deviceId,
|
||||
kind: input.kind,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
},
|
||||
Effect.mapError((cause) => new LiveActivityDeliveryMarkPersistenceError({ cause })),
|
||||
),
|
||||
});
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user