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

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.
@@ -1660,7 +1660,7 @@ var KVService = class extends BaseService {
1660
1660
  return err(
1661
1661
  storageQuotaExceededError(
1662
1662
  "kv",
1663
- `Storage quota exceeded for key "${key}": ${errorText}`,
1663
+ `Storage quota exceeded for key ${JSON.stringify(key)}: ${errorText}`,
1664
1664
  {
1665
1665
  status: response.status,
1666
1666
  ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
@@ -1673,7 +1673,7 @@ var KVService = class extends BaseService {
1673
1673
  return err(
1674
1674
  storageLimitReachedError(
1675
1675
  "kv",
1676
- `Storage limit reached for key "${key}": ${errorText}`,
1676
+ `Storage limit reached for key ${JSON.stringify(key)}: ${errorText}`,
1677
1677
  {
1678
1678
  status: response.status,
1679
1679
  ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
@@ -1959,12 +1959,11 @@ var KVService = class extends BaseService {
1959
1959
  if (!response.ok) {
1960
1960
  const errorText = await response.text();
1961
1961
  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
- }));
1962
+ return this.authorizationFailure(
1963
+ `Failed to batch read ${keys.length} key(s)`,
1964
+ response,
1965
+ errorText
1966
+ );
1968
1967
  }
1969
1968
  if (response.status === 413) {
1970
1969
  return err(serviceError(
@@ -2063,23 +2062,39 @@ var KVService = class extends BaseService {
2063
2062
  } catch {
2064
2063
  }
2065
2064
  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
- }));
2065
+ return this.authorizationFailure(
2066
+ `Failed to create signed read URL for key ${JSON.stringify(key)}`,
2067
+ response,
2068
+ errorText
2069
+ );
2072
2070
  }
2073
2071
  const code = response.status === 400 ? ErrorCodes.INVALID_INPUT : ErrorCodes.NETWORK_ERROR;
2074
2072
  return err(
2075
2073
  serviceError(
2076
2074
  code,
2077
- `Failed to create signed read URL for key "${key}": ${response.status} - ${errorText}`,
2075
+ `Failed to create signed read URL for key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2078
2076
  "kv",
2079
2077
  { meta: { status: response.status, statusText: response.statusText } }
2080
2078
  )
2081
2079
  );
2082
2080
  }
2081
+ /**
2082
+ * AUTH_UNAUTHORIZED for a 401/403 response. The message keeps the HTTP
2083
+ * status and the server text (`<context>: <status> - <text>`, the same
2084
+ * shape as every other KV failure) so callers that rethrow only the message
2085
+ * still see the status; `meta.status` carries it for typed callers.
2086
+ * Resource/ability hints are parsed from the raw body, never the message.
2087
+ */
2088
+ authorizationFailure(context, response, errorText, extraMeta = {}) {
2089
+ const { resource, action } = parseAuthError(errorText);
2090
+ const detail = errorText.trim().length > 0 ? errorText : response.statusText || "authorization failed";
2091
+ return err(authUnauthorizedError("kv", `${context}: ${response.status} - ${detail}`, {
2092
+ status: response.status,
2093
+ ...action && { requiredAction: action },
2094
+ ...resource && { resource },
2095
+ ...extraMeta
2096
+ }));
2097
+ }
2083
2098
  normalizeSignedReadUrlResponse(data) {
2084
2099
  if (!data || typeof data !== "object") {
2085
2100
  return void 0;
@@ -2122,14 +2137,13 @@ var KVService = class extends BaseService {
2122
2137
  if (!response.ok) {
2123
2138
  if (response.status === 401 || response.status === 403) {
2124
2139
  const errorText2 = await response.text();
2125
- const { resource, action } = parseAuthError(errorText2);
2126
2140
  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
- }));
2141
+ return this.authorizationFailure(
2142
+ `Failed to get key ${JSON.stringify(key)}`,
2143
+ response,
2144
+ errorText2,
2145
+ permissionHint === void 0 ? {} : { permissionHint }
2146
+ );
2133
2147
  }
2134
2148
  if (response.status === 404) {
2135
2149
  return this.classifyNotFound(response, key);
@@ -2138,7 +2152,7 @@ var KVService = class extends BaseService {
2138
2152
  if (response.status === 413) {
2139
2153
  return err(serviceError(
2140
2154
  ErrorCodes.KV_RESPONSE_TOO_LARGE,
2141
- `KV value at key "${key}" exceeds the requested response limit`,
2155
+ `KV value at key ${JSON.stringify(key)} exceeds the requested response limit`,
2142
2156
  "kv",
2143
2157
  { meta: { status: response.status, statusText: response.statusText } }
2144
2158
  ));
@@ -2146,7 +2160,7 @@ var KVService = class extends BaseService {
2146
2160
  return err(
2147
2161
  serviceError(
2148
2162
  ErrorCodes.NETWORK_ERROR,
2149
- `Failed to get key "${key}": ${response.status} - ${errorText}`,
2163
+ `Failed to get key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2150
2164
  "kv",
2151
2165
  { meta: { status: response.status, statusText: response.statusText } }
2152
2166
  )
@@ -2202,21 +2216,18 @@ var KVService = class extends BaseService {
2202
2216
  }
2203
2217
  );
2204
2218
  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
- }));
2219
+ return this.authorizationFailure(
2220
+ `Failed to put key ${JSON.stringify(key)}`,
2221
+ response,
2222
+ await response.text()
2223
+ );
2213
2224
  }
2214
2225
  if (!response.ok) {
2215
2226
  const errorText = await response.text();
2216
2227
  if (response.status === 412) {
2217
2228
  return err(serviceError(
2218
2229
  ErrorCodes.KV_PRECONDITION_FAILED,
2219
- `KV precondition failed for key "${key}"`,
2230
+ `KV precondition failed for key ${JSON.stringify(key)}`,
2220
2231
  "kv",
2221
2232
  { meta: { status: response.status, statusText: response.statusText } }
2222
2233
  ));
@@ -2224,7 +2235,7 @@ var KVService = class extends BaseService {
2224
2235
  if (response.status === 503 && (options?.ifMatch !== void 0 || options?.ifNoneMatch !== void 0)) {
2225
2236
  return err(serviceError(
2226
2237
  ErrorCodes.KV_CONFLICT,
2227
- `Concurrent KV update conflicted for key "${key}"`,
2238
+ `Concurrent KV update conflicted for key ${JSON.stringify(key)}`,
2228
2239
  "kv",
2229
2240
  { meta: { status: response.status, statusText: response.statusText } }
2230
2241
  ));
@@ -2240,7 +2251,7 @@ var KVService = class extends BaseService {
2240
2251
  return err(
2241
2252
  serviceError(
2242
2253
  ErrorCodes.KV_WRITE_FAILED,
2243
- `Failed to put key "${key}": ${response.status} - ${errorText}`,
2254
+ `Failed to put key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2244
2255
  "kv",
2245
2256
  { meta: { status: response.status, statusText: response.statusText } }
2246
2257
  )
@@ -2334,12 +2345,11 @@ var KVService = class extends BaseService {
2334
2345
  );
2335
2346
  }
2336
2347
  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
- }));
2348
+ return this.authorizationFailure(
2349
+ `Failed to batch put ${items.length} key(s)`,
2350
+ response,
2351
+ errorText
2352
+ );
2343
2353
  }
2344
2354
  const quotaError = this.handleQuotaErrorResponse(
2345
2355
  response,
@@ -2460,13 +2470,11 @@ var KVService = class extends BaseService {
2460
2470
  );
2461
2471
  if (!response.ok) {
2462
2472
  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
- }));
2473
+ return this.authorizationFailure(
2474
+ "Failed to list keys",
2475
+ response,
2476
+ await response.text()
2477
+ );
2470
2478
  }
2471
2479
  const errorText = await response.text();
2472
2480
  return err(
@@ -2515,13 +2523,11 @@ var KVService = class extends BaseService {
2515
2523
  );
2516
2524
  if (!response.ok) {
2517
2525
  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
- }));
2526
+ return this.authorizationFailure(
2527
+ `Failed to delete key ${JSON.stringify(key)}`,
2528
+ response,
2529
+ await response.text()
2530
+ );
2525
2531
  }
2526
2532
  if (response.status === 404) {
2527
2533
  return this.classifyNotFound(response, key);
@@ -2530,7 +2536,7 @@ var KVService = class extends BaseService {
2530
2536
  if (response.status === 412) {
2531
2537
  return err(serviceError(
2532
2538
  ErrorCodes.KV_PRECONDITION_FAILED,
2533
- `KV precondition failed for key "${key}"`,
2539
+ `KV precondition failed for key ${JSON.stringify(key)}`,
2534
2540
  "kv",
2535
2541
  { meta: { status: response.status, statusText: response.statusText } }
2536
2542
  ));
@@ -2538,7 +2544,7 @@ var KVService = class extends BaseService {
2538
2544
  if (response.status === 503 && options?.ifMatch !== void 0) {
2539
2545
  return err(serviceError(
2540
2546
  ErrorCodes.KV_CONFLICT,
2541
- `Concurrent KV delete conflicted for key "${key}"`,
2547
+ `Concurrent KV delete conflicted for key ${JSON.stringify(key)}`,
2542
2548
  "kv",
2543
2549
  { meta: { status: response.status, statusText: response.statusText } }
2544
2550
  ));
@@ -2546,7 +2552,7 @@ var KVService = class extends BaseService {
2546
2552
  return err(
2547
2553
  serviceError(
2548
2554
  ErrorCodes.NETWORK_ERROR,
2549
- `Failed to delete key "${key}": ${response.status} - ${errorText}`,
2555
+ `Failed to delete key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2550
2556
  "kv",
2551
2557
  { meta: { status: response.status, statusText: response.statusText } }
2552
2558
  )
@@ -2579,13 +2585,11 @@ var KVService = class extends BaseService {
2579
2585
  );
2580
2586
  if (!response.ok) {
2581
2587
  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
- }));
2588
+ return this.authorizationFailure(
2589
+ `Failed to get metadata for key ${JSON.stringify(key)}`,
2590
+ response,
2591
+ await response.text()
2592
+ );
2589
2593
  }
2590
2594
  if (response.status === 404) {
2591
2595
  return this.classifyNotFound(response, key);
@@ -2594,7 +2598,7 @@ var KVService = class extends BaseService {
2594
2598
  return err(
2595
2599
  serviceError(
2596
2600
  ErrorCodes.NETWORK_ERROR,
2597
- `Failed to get metadata for key "${key}": ${response.status} - ${errorText}`,
2601
+ `Failed to get metadata for key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
2598
2602
  "kv",
2599
2603
  { meta: { status: response.status, statusText: response.statusText } }
2600
2604
  )
@@ -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";