Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/server/src/persistence/AuthPairingLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { AuthEnvironmentScopes } from "@t3tools/contracts";

import {
type AuthPairingLinkRepositoryError,
toPersistenceDecodeError,
toPersistenceSqlError,
PersistenceDecodeError,
PersistenceSqlError,
} from "./Errors.ts";

export const AuthPairingLinkRecord = Schema.Struct({
Expand Down Expand Up @@ -90,8 +90,8 @@ export class AuthPairingLinkRepository extends Context.Service<
function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
return (cause: unknown): AuthPairingLinkRepositoryError =>
Schema.isSchemaError(cause)
? toPersistenceDecodeError(decodeOperation)(cause)
: toPersistenceSqlError(sqlOperation)(cause);
? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
: new PersistenceSqlError({ operation: sqlOperation, cause });
}

export const make = Effect.gen(function* () {
Expand Down
8 changes: 4 additions & 4 deletions apps/server/src/persistence/AuthSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {

import {
type AuthSessionRepositoryError,
toPersistenceDecodeError,
toPersistenceSqlError,
PersistenceDecodeError,
PersistenceSqlError,
} from "./Errors.ts";

export const AuthSessionClientMetadataRecord = Schema.Struct({
Expand Down Expand Up @@ -146,8 +146,8 @@ function toAuthSessionRecord(row: typeof AuthSessionDbRow.Type): AuthSessionReco
function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
return (cause: unknown): AuthSessionRepositoryError =>
Schema.isSchemaError(cause)
? toPersistenceDecodeError(decodeOperation)(cause)
: toPersistenceSqlError(sqlOperation)(cause);
? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
: new PersistenceSqlError({ operation: sqlOperation, cause });
}

export const make = Effect.gen(function* () {
Expand Down
49 changes: 49 additions & 0 deletions apps/server/src/persistence/Errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

import { PersistenceDecodeError, PersistenceSqlError } from "./Errors.ts";

const decodeRuntimePayload = Schema.decodeUnknownEffect(
Schema.Struct({
runtimePayload: Schema.Struct({
attempt: Schema.Number,
}),
}),
);

it("keeps SQL operation context without a tautological detail", () => {
const cause = new Error("database unavailable");
const error = new PersistenceSqlError({
operation: "AuthSessionRepository.list:query",
cause,
});

assert.equal(error.operation, "AuthSessionRepository.list:query");
assert.equal(error.detail, undefined);
assert.equal(error.cause, cause);
assert.equal(error.message, "SQL error in AuthSessionRepository.list:query");
});

it.effect("maps schema errors without copying rejected payloads into diagnostics", () =>
Effect.gen(function* () {
const rejectedPayload = "runtime-payload-secret-sentinel";
const cause = yield* Effect.flip(
decodeRuntimePayload({
runtimePayload: {
attempt: rejectedPayload,
},
}),
);
const error = PersistenceDecodeError.fromSchemaError(
"ProviderSessionRuntimeRepository.list:decodeRows",
cause,
);

assert.equal(error.operation, "ProviderSessionRuntimeRepository.list:decodeRows");
assert.equal(error.cause, cause);
assert.notInclude(error.issue, rejectedPayload);
assert.notInclude(error.message, rejectedPayload);
assert.include(error.issue, "InvalidType");
}),
);
47 changes: 30 additions & 17 deletions apps/server/src/persistence/Errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import * as Schema from "effect/Schema";
import * as SchemaIssue from "effect/SchemaIssue";

function summarizeSchemaIssue(issue: SchemaIssue.Issue): string {
switch (issue._tag) {
case "Filter":
case "Encoding":
case "Pointer":
return `${issue._tag}(${summarizeSchemaIssue(issue.issue)})`;
case "Composite":
case "AnyOf":
return `${issue._tag}(${issue.issues.map(summarizeSchemaIssue).join(",")})`;
default:
return issue._tag;
}
}

// ===============================
// Core Persistence Errors
// ===============================
Expand All @@ -9,12 +23,14 @@ export class PersistenceSqlError extends Schema.TaggedErrorClass<PersistenceSqlE
"PersistenceSqlError",
{
operation: Schema.String,
detail: Schema.String,
detail: Schema.optional(Schema.String),
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
return `SQL error in ${this.operation}: ${this.detail}`;
return this.detail === undefined
? `SQL error in ${this.operation}`
: `SQL error in ${this.operation}: ${this.detail}`;
}
}

Expand All @@ -26,13 +42,22 @@ export class PersistenceDecodeError extends Schema.TaggedErrorClass<PersistenceD
cause: Schema.optional(Schema.Defect()),
},
) {
static fromSchemaError(operation: string, cause: Schema.SchemaError): PersistenceDecodeError {
return new PersistenceDecodeError({
operation,
issue: summarizeSchemaIssue(cause.issue),
cause,
});
}

override get message(): string {
return `Decode error in ${this.operation}: ${this.issue}`;
}
}
const isPersistenceSqlError = Schema.is(PersistenceSqlError);
const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);

// Kept for orchestration/projection call sites, which are being revamped separately.
export function toPersistenceSqlError(operation: string) {
return (cause: unknown): PersistenceSqlError =>
new PersistenceSqlError({
Expand All @@ -42,22 +67,10 @@ export function toPersistenceSqlError(operation: string) {
});
}

// Kept for orchestration/projection call sites, which are being revamped separately.
export function toPersistenceDecodeError(operation: string) {
return (error: Schema.SchemaError): PersistenceDecodeError =>
new PersistenceDecodeError({
operation,
issue: SchemaIssue.makeFormatterDefault()(error.issue),
cause: error,
});
}

export function toPersistenceDecodeCauseError(operation: string) {
return (cause: unknown): PersistenceDecodeError =>
new PersistenceDecodeError({
operation,
issue: `Failed to execute ${operation}`,
cause,
});
return (cause: Schema.SchemaError): PersistenceDecodeError =>
PersistenceDecodeError.fromSchemaError(operation, cause);
}

export const isPersistenceError = (u: unknown) =>
Expand Down
26 changes: 17 additions & 9 deletions apps/server/src/persistence/ProviderSessionRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
} from "@t3tools/contracts";

import {
PersistenceDecodeError,
PersistenceSqlError,
type ProviderSessionRuntimeRepositoryError,
toPersistenceDecodeError,
toPersistenceSqlError,
} from "./Errors.ts";

/**
Expand Down Expand Up @@ -117,8 +117,8 @@ const DeleteRuntimeRequestSchema = GetRuntimeRequestSchema;
function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
return (cause: unknown): ProviderSessionRuntimeRepositoryError =>
Schema.isSchemaError(cause)
? toPersistenceDecodeError(decodeOperation)(cause)
: toPersistenceSqlError(sqlOperation)(cause);
? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
: new PersistenceSqlError({ operation: sqlOperation, cause });
}

export const make = Effect.gen(function* () {
Expand Down Expand Up @@ -235,9 +235,10 @@ export const make = Effect.gen(function* () {
onNone: () => Effect.succeed(Option.none()),
onSome: (row) =>
decodeRuntime(row).pipe(
Effect.mapError(
toPersistenceDecodeError(
Effect.mapError((cause) =>
PersistenceDecodeError.fromSchemaError(
"ProviderSessionRuntimeRepository.getByThreadId:rowToRuntime",
cause,
),
),
Effect.map((runtime) => Option.some(runtime)),
Expand All @@ -259,8 +260,11 @@ export const make = Effect.gen(function* () {
rows,
(row) =>
decodeRuntime(row).pipe(
Effect.mapError(
toPersistenceDecodeError("ProviderSessionRuntimeRepository.list:rowToRuntime"),
Effect.mapError((cause) =>
PersistenceDecodeError.fromSchemaError(
"ProviderSessionRuntimeRepository.list:rowToRuntime",
cause,
),
),
),
{ concurrency: "unbounded" },
Expand All @@ -273,7 +277,11 @@ export const make = Effect.gen(function* () {
) =>
deleteRuntimeByThreadId(input).pipe(
Effect.mapError(
toPersistenceSqlError("ProviderSessionRuntimeRepository.deleteByThreadId:query"),
(cause) =>
new PersistenceSqlError({
operation: "ProviderSessionRuntimeRepository.deleteByThreadId:query",
cause,
}),
),
);

Expand Down
Loading