mirror of
https://github.com/anomalyco/opencode.git
synced 2026-04-24 06:45:22 +00:00
core: simplify account error payloads
This commit is contained in:
@@ -11,13 +11,12 @@ const decodeAccount = Schema.decodeUnknownSync(Account)
|
||||
|
||||
type DbClient = Parameters<typeof Database.use>[0] extends (db: infer T) => unknown ? T : never
|
||||
|
||||
const toAccountRepoError = (operation: string, message: string, cause?: unknown) =>
|
||||
new AccountRepoError({ operation, message, cause })
|
||||
const toAccountRepoError = (message: string, cause?: unknown) => new AccountRepoError({ message, cause })
|
||||
|
||||
const db = <A>(run: (db: DbClient) => A) =>
|
||||
Effect.try({
|
||||
try: () => Database.use(run),
|
||||
catch: (cause) => toAccountRepoError("db", "Database operation failed", cause),
|
||||
catch: (cause) => toAccountRepoError("Database operation failed", cause),
|
||||
})
|
||||
|
||||
const fromRow = (row: AccountRow) => decodeAccount(row)
|
||||
@@ -143,7 +142,7 @@ export class AccountRepo extends ServiceMap.Service<
|
||||
.run()
|
||||
setActive(tx, input.id)
|
||||
}),
|
||||
catch: (cause) => toAccountRepoError("db", "Database operation failed", cause),
|
||||
catch: (cause) => toAccountRepoError("Database operation failed", cause),
|
||||
}).pipe(Effect.asVoid)
|
||||
}),
|
||||
}),
|
||||
|
||||
@@ -33,13 +33,11 @@ export class Org extends Schema.Class<Org>("Org")({
|
||||
}) {}
|
||||
|
||||
export class AccountRepoError extends Schema.TaggedErrorClass<AccountRepoError>()("AccountRepoError", {
|
||||
operation: Schema.String,
|
||||
message: Schema.String,
|
||||
cause: Schema.optional(Schema.Defect),
|
||||
}) {}
|
||||
|
||||
export class AccountServiceError extends Schema.TaggedErrorClass<AccountServiceError>()("AccountServiceError", {
|
||||
operation: Schema.String,
|
||||
message: Schema.String,
|
||||
cause: Schema.optional(Schema.Defect),
|
||||
}) {}
|
||||
|
||||
@@ -84,15 +84,14 @@ const DeviceTokenRequest = Schema.Struct({
|
||||
const serverDefault = "https://web-14275-d60e67f5-pyqs0590.onporter.run"
|
||||
const clientId = "opencode-cli"
|
||||
|
||||
const toAccountServiceError = (operation: string, message: string, cause?: unknown) =>
|
||||
new AccountServiceError({ operation, message, cause })
|
||||
const toAccountServiceError = (message: string, cause?: unknown) => new AccountServiceError({ message, cause })
|
||||
|
||||
const mapAccountServiceError =
|
||||
(operation: string, message = "Account service operation failed") =>
|
||||
<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, AccountServiceError, R> =>
|
||||
effect.pipe(
|
||||
Effect.mapError((error) =>
|
||||
error instanceof AccountServiceError ? error : toAccountServiceError(operation, message, error),
|
||||
error instanceof AccountServiceError ? error : toAccountServiceError(`${message} (${operation})`, error),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -265,7 +264,7 @@ export class AccountService extends ServiceMap.Service<
|
||||
const ok = yield* okOrNone("login", response)
|
||||
if (Option.isNone(ok)) {
|
||||
const body = yield* response.text.pipe(Effect.orElseSucceed(() => ""))
|
||||
return yield* toAccountServiceError("login", `Failed to initiate device flow: ${body || response.status}`)
|
||||
return yield* toAccountServiceError(`Failed to initiate device flow: ${body || response.status}`)
|
||||
}
|
||||
|
||||
const parsed = yield* HttpClientResponse.schemaBodyJson(DeviceCode)(ok.value).pipe(
|
||||
|
||||
Reference in New Issue
Block a user