@tinycloud/operations 0.3.4-beta.1 → 0.3.4-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1266,6 +1266,20 @@ function storageLimitReachedError(service, message, meta) {
1266
1266
  meta
1267
1267
  };
1268
1268
  }
1269
+ var STORAGE_FULL_MESSAGE = "TinyCloud storage is full, so this change was not saved. Reading still works. Free up space or upgrade your plan to save again.";
1270
+ var STORAGE_WRITE_TOO_LARGE_MESSAGE = "This change is larger than the TinyCloud storage you have left, so it was not saved. Reading still works. Free up space or upgrade your plan to save it.";
1271
+ function parseStorageQuotaBytes(responseText) {
1272
+ const match = responseText.match(/Used:\s*(\d+)\s*bytes,\s*Limit:\s*(\d+)\s*bytes/i);
1273
+ if (!match) return void 0;
1274
+ return {
1275
+ usedBytes: parseInt(match[1], 10),
1276
+ limitBytes: parseInt(match[2], 10)
1277
+ };
1278
+ }
1279
+ function storageRejectionError(service, status, meta, responseText) {
1280
+ const quotaMeta = { ...meta, ...parseStorageQuotaBytes(responseText) };
1281
+ return status === 402 ? storageQuotaExceededError(service, STORAGE_FULL_MESSAGE, quotaMeta) : storageLimitReachedError(service, STORAGE_WRITE_TOO_LARGE_MESSAGE, quotaMeta);
1282
+ }
1269
1283
  function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
1270
1284
  if (error instanceof Error) {
1271
1285
  if (error.name === "AbortError") {
@@ -1641,47 +1655,21 @@ var KVService = class extends BaseService {
1641
1655
  get config() {
1642
1656
  return this._config;
1643
1657
  }
1644
- // Parses "Used: X bytes, Limit: Y bytes" from tinycloud-node error responses
1645
- parseQuotaInfo(errorText) {
1646
- const match = errorText.match(
1647
- /Used:\s*(\d+)\s*bytes,\s*Limit:\s*(\d+)\s*bytes/i
1648
- );
1649
- if (match) {
1650
- return {
1651
- usedBytes: parseInt(match[1], 10),
1652
- limitBytes: parseInt(match[2], 10)
1653
- };
1654
- }
1655
- return void 0;
1656
- }
1657
1658
  handleQuotaErrorResponse(response, errorText, key) {
1658
- if (response.status === 402) {
1659
- const quotaInfo = this.parseQuotaInfo(errorText);
1660
- return err(
1661
- storageQuotaExceededError(
1662
- "kv",
1663
- `Storage quota exceeded for key "${key}": ${errorText}`,
1664
- {
1665
- status: response.status,
1666
- ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
1667
- }
1668
- )
1669
- );
1659
+ if (response.status !== 402 && response.status !== 413) {
1660
+ return void 0;
1670
1661
  }
1671
- if (response.status === 413) {
1672
- const quotaInfo = this.parseQuotaInfo(errorText);
1673
- return err(
1674
- storageLimitReachedError(
1675
- "kv",
1676
- `Storage limit reached for key "${key}": ${errorText}`,
1677
- {
1678
- status: response.status,
1679
- ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
1680
- }
1681
- )
1682
- );
1662
+ if (response.status === 413 && !parseStorageQuotaBytes(errorText)) {
1663
+ return void 0;
1683
1664
  }
1684
- return void 0;
1665
+ return err(
1666
+ storageRejectionError(
1667
+ "kv",
1668
+ response.status,
1669
+ { status: response.status, statusText: response.statusText, key },
1670
+ errorText
1671
+ )
1672
+ );
1685
1673
  }
1686
1674
  /**
1687
1675
  * Classify a KV 404 by reading the response body once.
@@ -1959,12 +1947,11 @@ var KVService = class extends BaseService {
1959
1947
  if (!response.ok) {
1960
1948
  const errorText = await response.text();
1961
1949
  if (response.status === 401 || response.status === 403) {
1962
- const { resource, action: requiredAction } = parseAuthError(errorText);
1963
- return err(authUnauthorizedError("kv", errorText, {
1964
- status: response.status,
1965
- ...requiredAction && { requiredAction },
1966
- ...resource && { resource }
1967
- }));
1950
+ return this.authorizationFailure(
1951
+ `Failed to batch read ${keys.length} key(s)`,
1952
+ response,
1953
+ errorText
1954
+ );
1968
1955
  }
1969
1956
  if (response.status === 413) {
1970
1957
  return err(serviceError(
@@ -2063,23 +2050,39 @@ var KVService = class extends BaseService {
2063
2050
  } catch {
2064
2051
  }
2065
2052
  if (response.status === 401 || response.status === 403) {
2066
- const { resource, action } = parseAuthError(errorText);
2067
- return err(authUnauthorizedError("kv", errorText, {
2068
- status: response.status,
2069
- ...action && { requiredAction: action },
2070
- ...resource && { resource }
2071
- }));
2053
+ return this.authorizationFailure(
2054
+ `Failed to create signed read URL for key ${JSON.stringify(key)}`,
2055
+ response,
2056
+ errorText
2057
+ );
2072
2058
  }
2073
2059
  const code = response.status === 400 ? ErrorCodes.INVALID_INPUT : ErrorCodes.NETWORK_ERROR;
2074
2060
  return err(
2075
2061
  serviceError(
2076
2062
  code,
2077
- `Failed to create signed read URL for key "${key}": ${response.status} - ${errorText}`,
2063
+ `Failed to create signed read URL for key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2078
2064
  "kv",
2079
2065
  { meta: { status: response.status, statusText: response.statusText } }
2080
2066
  )
2081
2067
  );
2082
2068
  }
2069
+ /**
2070
+ * AUTH_UNAUTHORIZED for a 401/403 response. The message keeps the HTTP
2071
+ * status and the server text (`<context>: <status> - <text>`, the same
2072
+ * shape as every other KV failure) so callers that rethrow only the message
2073
+ * still see the status; `meta.status` carries it for typed callers.
2074
+ * Resource/ability hints are parsed from the raw body, never the message.
2075
+ */
2076
+ authorizationFailure(context, response, errorText, extraMeta = {}) {
2077
+ const { resource, action } = parseAuthError(errorText);
2078
+ const detail = errorText.trim().length > 0 ? errorText : response.statusText || "authorization failed";
2079
+ return err(authUnauthorizedError("kv", `${context}: ${response.status} - ${detail}`, {
2080
+ status: response.status,
2081
+ ...action && { requiredAction: action },
2082
+ ...resource && { resource },
2083
+ ...extraMeta
2084
+ }));
2085
+ }
2083
2086
  normalizeSignedReadUrlResponse(data) {
2084
2087
  if (!data || typeof data !== "object") {
2085
2088
  return void 0;
@@ -2122,14 +2125,13 @@ var KVService = class extends BaseService {
2122
2125
  if (!response.ok) {
2123
2126
  if (response.status === 401 || response.status === 403) {
2124
2127
  const errorText2 = await response.text();
2125
- const { resource, action } = parseAuthError(errorText2);
2126
2128
  const permissionHint = parsePermissionHintFromErrorText(errorText2);
2127
- return err(authUnauthorizedError("kv", errorText2, {
2128
- status: response.status,
2129
- ...action && { requiredAction: action },
2130
- ...resource && { resource },
2131
- ...permissionHint === void 0 ? {} : { permissionHint }
2132
- }));
2129
+ return this.authorizationFailure(
2130
+ `Failed to get key ${JSON.stringify(key)}`,
2131
+ response,
2132
+ errorText2,
2133
+ permissionHint === void 0 ? {} : { permissionHint }
2134
+ );
2133
2135
  }
2134
2136
  if (response.status === 404) {
2135
2137
  return this.classifyNotFound(response, key);
@@ -2138,7 +2140,7 @@ var KVService = class extends BaseService {
2138
2140
  if (response.status === 413) {
2139
2141
  return err(serviceError(
2140
2142
  ErrorCodes.KV_RESPONSE_TOO_LARGE,
2141
- `KV value at key "${key}" exceeds the requested response limit`,
2143
+ `KV value at key ${JSON.stringify(key)} exceeds the requested response limit`,
2142
2144
  "kv",
2143
2145
  { meta: { status: response.status, statusText: response.statusText } }
2144
2146
  ));
@@ -2146,7 +2148,7 @@ var KVService = class extends BaseService {
2146
2148
  return err(
2147
2149
  serviceError(
2148
2150
  ErrorCodes.NETWORK_ERROR,
2149
- `Failed to get key "${key}": ${response.status} - ${errorText}`,
2151
+ `Failed to get key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2150
2152
  "kv",
2151
2153
  { meta: { status: response.status, statusText: response.statusText } }
2152
2154
  )
@@ -2202,21 +2204,18 @@ var KVService = class extends BaseService {
2202
2204
  }
2203
2205
  );
2204
2206
  if (response.status === 401 || response.status === 403) {
2205
- const errorText = await response.text();
2206
- const message = errorText.trim().length > 0 ? errorText : `Failed to put key "${key}": ${response.status} - ${response.statusText || "authorization failed"}`;
2207
- const { resource, action: requiredAction } = parseAuthError(errorText);
2208
- return err(authUnauthorizedError("kv", message, {
2209
- status: response.status,
2210
- ...requiredAction && { requiredAction },
2211
- ...resource && { resource }
2212
- }));
2207
+ return this.authorizationFailure(
2208
+ `Failed to put key ${JSON.stringify(key)}`,
2209
+ response,
2210
+ await response.text()
2211
+ );
2213
2212
  }
2214
2213
  if (!response.ok) {
2215
2214
  const errorText = await response.text();
2216
2215
  if (response.status === 412) {
2217
2216
  return err(serviceError(
2218
2217
  ErrorCodes.KV_PRECONDITION_FAILED,
2219
- `KV precondition failed for key "${key}"`,
2218
+ `KV precondition failed for key ${JSON.stringify(key)}`,
2220
2219
  "kv",
2221
2220
  { meta: { status: response.status, statusText: response.statusText } }
2222
2221
  ));
@@ -2224,7 +2223,7 @@ var KVService = class extends BaseService {
2224
2223
  if (response.status === 503 && (options?.ifMatch !== void 0 || options?.ifNoneMatch !== void 0)) {
2225
2224
  return err(serviceError(
2226
2225
  ErrorCodes.KV_CONFLICT,
2227
- `Concurrent KV update conflicted for key "${key}"`,
2226
+ `Concurrent KV update conflicted for key ${JSON.stringify(key)}`,
2228
2227
  "kv",
2229
2228
  { meta: { status: response.status, statusText: response.statusText } }
2230
2229
  ));
@@ -2240,7 +2239,7 @@ var KVService = class extends BaseService {
2240
2239
  return err(
2241
2240
  serviceError(
2242
2241
  ErrorCodes.KV_WRITE_FAILED,
2243
- `Failed to put key "${key}": ${response.status} - ${errorText}`,
2242
+ `Failed to put key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2244
2243
  "kv",
2245
2244
  { meta: { status: response.status, statusText: response.statusText } }
2246
2245
  )
@@ -2334,12 +2333,11 @@ var KVService = class extends BaseService {
2334
2333
  );
2335
2334
  }
2336
2335
  if (response.status === 401 || response.status === 403) {
2337
- const { resource, action } = parseAuthError(errorText);
2338
- return err(authUnauthorizedError("kv", errorText, {
2339
- status: response.status,
2340
- ...action && { requiredAction: action },
2341
- ...resource && { resource }
2342
- }));
2336
+ return this.authorizationFailure(
2337
+ `Failed to batch put ${items.length} key(s)`,
2338
+ response,
2339
+ errorText
2340
+ );
2343
2341
  }
2344
2342
  const quotaError = this.handleQuotaErrorResponse(
2345
2343
  response,
@@ -2460,13 +2458,11 @@ var KVService = class extends BaseService {
2460
2458
  );
2461
2459
  if (!response.ok) {
2462
2460
  if (response.status === 401) {
2463
- const errorText2 = await response.text();
2464
- const { resource, action } = parseAuthError(errorText2);
2465
- return err(authUnauthorizedError("kv", errorText2, {
2466
- status: response.status,
2467
- ...action && { requiredAction: action },
2468
- ...resource && { resource }
2469
- }));
2461
+ return this.authorizationFailure(
2462
+ "Failed to list keys",
2463
+ response,
2464
+ await response.text()
2465
+ );
2470
2466
  }
2471
2467
  const errorText = await response.text();
2472
2468
  return err(
@@ -2515,13 +2511,11 @@ var KVService = class extends BaseService {
2515
2511
  );
2516
2512
  if (!response.ok) {
2517
2513
  if (response.status === 401) {
2518
- const errorText2 = await response.text();
2519
- const { resource, action } = parseAuthError(errorText2);
2520
- return err(authUnauthorizedError("kv", errorText2, {
2521
- status: response.status,
2522
- ...action && { requiredAction: action },
2523
- ...resource && { resource }
2524
- }));
2514
+ return this.authorizationFailure(
2515
+ `Failed to delete key ${JSON.stringify(key)}`,
2516
+ response,
2517
+ await response.text()
2518
+ );
2525
2519
  }
2526
2520
  if (response.status === 404) {
2527
2521
  return this.classifyNotFound(response, key);
@@ -2530,7 +2524,7 @@ var KVService = class extends BaseService {
2530
2524
  if (response.status === 412) {
2531
2525
  return err(serviceError(
2532
2526
  ErrorCodes.KV_PRECONDITION_FAILED,
2533
- `KV precondition failed for key "${key}"`,
2527
+ `KV precondition failed for key ${JSON.stringify(key)}`,
2534
2528
  "kv",
2535
2529
  { meta: { status: response.status, statusText: response.statusText } }
2536
2530
  ));
@@ -2538,7 +2532,7 @@ var KVService = class extends BaseService {
2538
2532
  if (response.status === 503 && options?.ifMatch !== void 0) {
2539
2533
  return err(serviceError(
2540
2534
  ErrorCodes.KV_CONFLICT,
2541
- `Concurrent KV delete conflicted for key "${key}"`,
2535
+ `Concurrent KV delete conflicted for key ${JSON.stringify(key)}`,
2542
2536
  "kv",
2543
2537
  { meta: { status: response.status, statusText: response.statusText } }
2544
2538
  ));
@@ -2546,7 +2540,7 @@ var KVService = class extends BaseService {
2546
2540
  return err(
2547
2541
  serviceError(
2548
2542
  ErrorCodes.NETWORK_ERROR,
2549
- `Failed to delete key "${key}": ${response.status} - ${errorText}`,
2543
+ `Failed to delete key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2550
2544
  "kv",
2551
2545
  { meta: { status: response.status, statusText: response.statusText } }
2552
2546
  )
@@ -2579,13 +2573,11 @@ var KVService = class extends BaseService {
2579
2573
  );
2580
2574
  if (!response.ok) {
2581
2575
  if (response.status === 401) {
2582
- const errorText2 = await response.text();
2583
- const { resource, action } = parseAuthError(errorText2);
2584
- return err(authUnauthorizedError("kv", errorText2, {
2585
- status: response.status,
2586
- ...action && { requiredAction: action },
2587
- ...resource && { resource }
2588
- }));
2576
+ return this.authorizationFailure(
2577
+ `Failed to get metadata for key ${JSON.stringify(key)}`,
2578
+ response,
2579
+ await response.text()
2580
+ );
2589
2581
  }
2590
2582
  if (response.status === 404) {
2591
2583
  return this.classifyNotFound(response, key);
@@ -2594,7 +2586,7 @@ var KVService = class extends BaseService {
2594
2586
  return err(
2595
2587
  serviceError(
2596
2588
  ErrorCodes.NETWORK_ERROR,
2597
- `Failed to get metadata for key "${key}": ${response.status} - ${errorText}`,
2589
+ `Failed to get metadata for key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2598
2590
  "kv",
2599
2591
  { meta: { status: response.status, statusText: response.statusText } }
2600
2592
  )
@@ -3000,21 +2992,24 @@ var SQLService = class extends BaseService {
3000
2992
  }
3001
2993
  async applyMigrationsOnDbUnlocked(dbName, options) {
3002
2994
  return this.withTelemetry("migrations.apply", dbName, async () => {
3003
- const created = await this.ensureMigrationsTable(dbName, options.signal);
3004
- if (!created.ok) return created;
3005
2995
  const listed = await this.queryOnDb(
3006
2996
  dbName,
3007
2997
  `SELECT id FROM ${MIGRATIONS_TABLE} WHERE namespace = ? ORDER BY applied_at, id`,
3008
2998
  [options.namespace],
3009
2999
  { signal: options.signal }
3010
3000
  );
3011
- if (!listed.ok) return listed;
3001
+ const tableMissing = !listed.ok && (listed.error.code === ErrorCodes.SQL_DATABASE_NOT_FOUND || isMissingTableError(listed.error.message));
3002
+ if (!listed.ok && !tableMissing) return listed;
3012
3003
  const appliedIds = new Set(
3013
- listed.data.rows.map((row) => rowValue(row, 0)).filter((id) => typeof id === "string")
3004
+ (listed.ok ? listed.data.rows : []).map((row) => rowValue(row, 0)).filter((id) => typeof id === "string")
3014
3005
  );
3015
3006
  const skipped = options.migrations.filter((migration) => appliedIds.has(migration.id)).map((migration) => migration.id);
3016
3007
  const pending = options.migrations.filter((migration) => !appliedIds.has(migration.id));
3017
3008
  const applied = [];
3009
+ if (pending.length > 0 && tableMissing) {
3010
+ const created = await this.ensureMigrationsTable(dbName, options.signal);
3011
+ if (!created.ok) return created;
3012
+ }
3018
3013
  for (const migration of pending) {
3019
3014
  const result = await this.applyOneMigration(dbName, options.namespace, migration, options.signal);
3020
3015
  if (!result.ok) return result;
@@ -3117,6 +3112,10 @@ var SQLService = class extends BaseService {
3117
3112
  }
3118
3113
  async handleErrorResponse(response, operation) {
3119
3114
  const errorText = await response.text();
3115
+ const meta = responseErrorMeta(response.status, response.statusText, errorText);
3116
+ if (response.status === 402) {
3117
+ return err(storageRejectionError("sql", 402, meta, errorText));
3118
+ }
3120
3119
  const errorBody = parseServiceErrorBody(errorText);
3121
3120
  const errorCode = this.mapHttpStatusToErrorCode(
3122
3121
  response.status,
@@ -3129,7 +3128,6 @@ var SQLService = class extends BaseService {
3129
3128
  errorText,
3130
3129
  errorBody
3131
3130
  );
3132
- const meta = responseErrorMeta(response.status, response.statusText, errorText);
3133
3131
  if (response.status === 401) {
3134
3132
  const { resource, action } = parseAuthError(errorText);
3135
3133
  if (action) meta.requiredAction = action;
@@ -3230,6 +3228,9 @@ function serializeSqlValues(values) {
3230
3228
  (value) => value instanceof Uint8Array ? Array.from(value) : value
3231
3229
  );
3232
3230
  }
3231
+ function isMissingTableError(message) {
3232
+ return /no such table/i.test(message);
3233
+ }
3233
3234
  function migrationKey(namespace, id) {
3234
3235
  return `${encodeURIComponent(namespace)}:${encodeURIComponent(id)}`;
3235
3236
  }
@@ -3544,6 +3545,10 @@ var DuckDbService = class extends BaseService {
3544
3545
  }
3545
3546
  async handleErrorResponse(response, operation) {
3546
3547
  const errorText = await response.text();
3548
+ const meta = responseErrorMeta(response.status, response.statusText, errorText);
3549
+ if (response.status === 402) {
3550
+ return err(storageRejectionError("duckdb", 402, meta, errorText));
3551
+ }
3547
3552
  const errorBody = parseServiceErrorBody(errorText);
3548
3553
  const errorCode = this.mapHttpStatusToErrorCode(
3549
3554
  response.status,
@@ -3556,7 +3561,6 @@ var DuckDbService = class extends BaseService {
3556
3561
  errorText,
3557
3562
  errorBody
3558
3563
  );
3559
- const meta = responseErrorMeta(response.status, response.statusText, errorText);
3560
3564
  if (response.status === 401) {
3561
3565
  const { resource, action } = parseAuthError(errorText);
3562
3566
  if (action) meta.requiredAction = action;
@@ -4053,10 +4057,14 @@ async function responseError(service, message, response) {
4053
4057
  }
4054
4058
  } catch {
4055
4059
  }
4056
- return wrapError(
4060
+ const error = wrapError(
4057
4061
  service,
4058
4062
  new Error(`${message}: ${response.status} ${detail}`)
4059
4063
  );
4064
+ return {
4065
+ ...error,
4066
+ meta: { ...error.meta, status: response.status, statusText: response.statusText }
4067
+ };
4060
4068
  }
4061
4069
  function isAbortError(error) {
4062
4070
  return error instanceof DOMException && error.name === "AbortError";
@@ -4479,6 +4487,9 @@ function defaultVaultMessage(input) {
4479
4487
  return input.message ?? `Public key not found for ${input.did}`;
4480
4488
  case "STORAGE_ERROR":
4481
4489
  return input.message ?? input.cause.message;
4490
+ case "STORAGE_QUOTA_EXCEEDED":
4491
+ case "STORAGE_LIMIT_REACHED":
4492
+ return input.message;
4482
4493
  }
4483
4494
  }
4484
4495
  function vaultError(input) {
@@ -4489,6 +4500,15 @@ function vaultError(input) {
4489
4500
  };
4490
4501
  return { ok: false, error };
4491
4502
  }
4503
+ function kvWriteError(context, error) {
4504
+ if (error.code === ErrorCodes.STORAGE_QUOTA_EXCEEDED || error.code === ErrorCodes.STORAGE_LIMIT_REACHED) {
4505
+ return vaultError({ code: error.code, message: error.message, meta: error.meta });
4506
+ }
4507
+ return vaultError({
4508
+ code: "STORAGE_ERROR",
4509
+ cause: new Error(`${context}: ${error.message}`)
4510
+ });
4511
+ }
4492
4512
  var DataVaultService = class extends BaseService {
4493
4513
  /**
4494
4514
  * Create a new DataVaultService instance.
@@ -4771,12 +4791,7 @@ var DataVaultService = class extends BaseService {
4771
4791
  JSON.stringify(envelopeResult.data)
4772
4792
  );
4773
4793
  if (!valuePutResult.ok) {
4774
- return vaultError({
4775
- code: "STORAGE_ERROR",
4776
- cause: new Error(
4777
- `Failed to store encrypted value: ${valuePutResult.error.message}`
4778
- )
4779
- });
4794
+ return kvWriteError("Failed to store encrypted value", valuePutResult.error);
4780
4795
  }
4781
4796
  return { ok: true, data: void 0 };
4782
4797
  } catch (error) {
@@ -4994,12 +5009,7 @@ var DataVaultService = class extends BaseService {
4994
5009
  keyPayload
4995
5010
  );
4996
5011
  if (!keyPutResult.ok) {
4997
- return vaultError({
4998
- code: "STORAGE_ERROR",
4999
- cause: new Error(
5000
- `Failed to store key blob: ${keyPutResult.error.message}`
5001
- )
5002
- });
5012
+ return kvWriteError("Failed to store key blob", keyPutResult.error);
5003
5013
  }
5004
5014
  const valuePayload = JSON.stringify({
5005
5015
  data: base64Encode(encrypted),
@@ -5010,12 +5020,7 @@ var DataVaultService = class extends BaseService {
5010
5020
  valuePayload
5011
5021
  );
5012
5022
  if (!valuePutResult.ok) {
5013
- return vaultError({
5014
- code: "STORAGE_ERROR",
5015
- cause: new Error(
5016
- `Failed to store encrypted value: ${valuePutResult.error.message}`
5017
- )
5018
- });
5023
+ return kvWriteError("Failed to store encrypted value", valuePutResult.error);
5019
5024
  }
5020
5025
  return ok(void 0);
5021
5026
  } catch (error) {
@@ -5346,12 +5351,7 @@ var DataVaultService = class extends BaseService {
5346
5351
  grantPayload
5347
5352
  );
5348
5353
  if (!grantPutResult.ok) {
5349
- return vaultError({
5350
- code: "STORAGE_ERROR",
5351
- cause: new Error(
5352
- `Failed to store grant: ${grantPutResult.error.message}`
5353
- )
5354
- });
5354
+ return kvWriteError("Failed to store grant", grantPutResult.error);
5355
5355
  }
5356
5356
  return ok(void 0);
5357
5357
  } catch (error) {
@@ -5696,12 +5696,7 @@ var DataVaultService = class extends BaseService {
5696
5696
  keyPayload
5697
5697
  );
5698
5698
  if (!keyPutResult.ok) {
5699
- return vaultError({
5700
- code: "STORAGE_ERROR",
5701
- cause: new Error(
5702
- `Failed to store rotated key blob: ${keyPutResult.error.message}`
5703
- )
5704
- });
5699
+ return kvWriteError("Failed to store rotated key blob", keyPutResult.error);
5705
5700
  }
5706
5701
  const valuePayload = JSON.stringify({
5707
5702
  data: base64Encode(encrypted),
@@ -5712,12 +5707,7 @@ var DataVaultService = class extends BaseService {
5712
5707
  valuePayload
5713
5708
  );
5714
5709
  if (!valuePutResult.ok) {
5715
- return vaultError({
5716
- code: "STORAGE_ERROR",
5717
- cause: new Error(
5718
- `Failed to store re-encrypted value: ${valuePutResult.error.message}`
5719
- )
5720
- });
5710
+ return kvWriteError("Failed to store re-encrypted value", valuePutResult.error);
5721
5711
  }
5722
5712
  for (const did of remainingGrantees) {
5723
5713
  const grantResult = await this.reencrypt(key, did);