2025-11-14 10:26:23 -08:00
|
|
|
/**
|
|
|
|
|
* Transaction System Errors
|
|
|
|
|
*
|
|
|
|
|
* Provides detailed error information for transaction failures.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base class for all transaction errors
|
|
|
|
|
*/
|
|
|
|
|
export class TransactionError extends Error {
|
|
|
|
|
constructor(
|
|
|
|
|
message: string,
|
|
|
|
|
public readonly context?: Record<string, any>
|
|
|
|
|
) {
|
|
|
|
|
super(message)
|
|
|
|
|
this.name = 'TransactionError'
|
|
|
|
|
Error.captureStackTrace(this, this.constructor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Error during transaction execution
|
|
|
|
|
*/
|
|
|
|
|
export class TransactionExecutionError extends TransactionError {
|
|
|
|
|
constructor(
|
|
|
|
|
message: string,
|
|
|
|
|
public readonly operationIndex: number,
|
|
|
|
|
public readonly operationName: string | undefined,
|
2026-07-01 09:16:46 -07:00
|
|
|
public override readonly cause: Error
|
2025-11-14 10:26:23 -08:00
|
|
|
) {
|
|
|
|
|
super(message, {
|
|
|
|
|
operationIndex,
|
|
|
|
|
operationName,
|
|
|
|
|
cause: cause.message
|
|
|
|
|
})
|
|
|
|
|
this.name = 'TransactionExecutionError'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Error during transaction rollback
|
|
|
|
|
*/
|
|
|
|
|
export class TransactionRollbackError extends TransactionError {
|
|
|
|
|
constructor(
|
|
|
|
|
message: string,
|
|
|
|
|
public readonly originalError: Error,
|
|
|
|
|
public readonly rollbackErrors: Error[]
|
|
|
|
|
) {
|
|
|
|
|
super(message, {
|
|
|
|
|
originalError: originalError.message,
|
|
|
|
|
rollbackErrorCount: rollbackErrors.length,
|
|
|
|
|
rollbackErrors: rollbackErrors.map(e => e.message)
|
|
|
|
|
})
|
|
|
|
|
this.name = 'TransactionRollbackError'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Error for invalid transaction state
|
|
|
|
|
*/
|
|
|
|
|
export class InvalidTransactionStateError extends TransactionError {
|
|
|
|
|
constructor(
|
|
|
|
|
currentState: string,
|
|
|
|
|
attemptedAction: string
|
|
|
|
|
) {
|
|
|
|
|
super(
|
|
|
|
|
`Cannot ${attemptedAction}: transaction is in state '${currentState}'`,
|
|
|
|
|
{ currentState, attemptedAction }
|
|
|
|
|
)
|
|
|
|
|
this.name = 'InvalidTransactionStateError'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Error for transaction timeout
|
|
|
|
|
*/
|
|
|
|
|
export class TransactionTimeoutError extends TransactionError {
|
|
|
|
|
constructor(
|
|
|
|
|
timeoutMs: number,
|
2026-07-17 16:49:38 -07:00
|
|
|
operationIndex: number,
|
|
|
|
|
telemetry?: {
|
|
|
|
|
elapsedMs?: number
|
|
|
|
|
totalOperations?: number
|
|
|
|
|
operationName?: string
|
|
|
|
|
}
|
2025-11-14 10:26:23 -08:00
|
|
|
) {
|
2026-07-17 16:49:38 -07:00
|
|
|
const progress =
|
|
|
|
|
telemetry?.totalOperations !== undefined
|
|
|
|
|
? `${operationIndex}/${telemetry.totalOperations}`
|
|
|
|
|
: String(operationIndex)
|
|
|
|
|
const name = telemetry?.operationName ? ` ('${telemetry.operationName}')` : ''
|
|
|
|
|
const elapsed =
|
|
|
|
|
telemetry?.elapsedMs !== undefined ? `${telemetry.elapsedMs}ms elapsed, ` : ''
|
2025-11-14 10:26:23 -08:00
|
|
|
super(
|
2026-07-17 16:49:38 -07:00
|
|
|
`Transaction timed out at operation ${progress}${name} — ${elapsed}budget ${timeoutMs}ms. ` +
|
|
|
|
|
`The batch rolled back atomically; retry with a higher timeoutMs or a smaller batch.`,
|
|
|
|
|
{ timeoutMs, operationIndex, ...telemetry }
|
2025-11-14 10:26:23 -08:00
|
|
|
)
|
|
|
|
|
this.name = 'TransactionTimeoutError'
|
|
|
|
|
}
|
|
|
|
|
}
|