@ragable/sdk 0.6.3 → 0.6.4

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.
package/dist/index.mjs CHANGED
@@ -14,6 +14,12 @@ var RagableSdkError = class extends Error {
14
14
  constructor(message) {
15
15
  super(message);
16
16
  this.name = this.constructor.name;
17
+ Object.defineProperty(this, "message", {
18
+ configurable: true,
19
+ enumerable: true,
20
+ writable: true,
21
+ value: message
22
+ });
17
23
  }
18
24
  toJSON() {
19
25
  return {
@@ -33,7 +39,7 @@ var RagableError = class extends RagableSdkError {
33
39
  __publicField(this, "details");
34
40
  this.status = status;
35
41
  this.body = body;
36
- this.code = body && typeof body === "object" ? typeof body.code === "string" ? body.code : void 0 : void 0;
42
+ this.code = body && typeof body === "object" ? typeof body.code === "string" ? body.code : typeof body.code === "number" ? String(body.code) : void 0 : void 0;
37
43
  this.details = body && typeof body === "object" ? typeof body.details === "string" ? body.details : void 0 : void 0;
38
44
  }
39
45
  toJSON() {
@@ -68,11 +74,15 @@ function formatSdkError(err) {
68
74
  try {
69
75
  const s = JSON.stringify(err);
70
76
  if (s !== "{}") return s;
77
+ return "Unknown error (empty object \u2014 avoid `{...error}` spread; use error.message or formatSdkError)";
71
78
  } catch {
72
79
  }
73
80
  }
74
81
  return String(err);
75
82
  }
83
+ function formatPostgrestError(error) {
84
+ return formatSdkError(error);
85
+ }
76
86
  var RagableNetworkError = class extends RagableSdkError {
77
87
  constructor(message, cause) {
78
88
  super(message);
@@ -642,6 +652,27 @@ function encodeFilterValue(op, value) {
642
652
  }
643
653
  return `${op}.${value}`;
644
654
  }
655
+ function extractPostgRESTErrorMessage(payload, status, statusText) {
656
+ const st = (statusText ?? "").trim();
657
+ if (typeof payload !== "object" || payload === null) {
658
+ return st || `HTTP ${status}`;
659
+ }
660
+ const p = payload;
661
+ const raw = p.message ?? p.error ?? p.hint;
662
+ let msg;
663
+ if (typeof raw === "string") {
664
+ msg = raw;
665
+ } else if (typeof raw === "number" || typeof raw === "boolean") {
666
+ msg = String(raw);
667
+ } else if (raw !== null && raw !== void 0 && typeof raw === "object") {
668
+ msg = JSON.stringify(raw);
669
+ } else {
670
+ msg = st || `HTTP ${status}`;
671
+ }
672
+ msg = msg.trim();
673
+ if (!msg) return st || `HTTP ${status}`;
674
+ return msg;
675
+ }
645
676
  async function parsePostgRESTResponse(response) {
646
677
  if (response.status === 204) return null;
647
678
  const text = await response.text();
@@ -657,9 +688,8 @@ async function parsePostgRESTResponse(response) {
657
688
  );
658
689
  }
659
690
  if (!response.ok) {
660
- const msg = typeof payload === "object" && payload !== null ? payload.message ?? payload.error ?? response.statusText : response.statusText;
661
- const code = typeof payload === "object" && payload !== null ? payload.code : void 0;
662
- throw new RagableError(String(msg), response.status, { code });
691
+ const msg = extractPostgRESTErrorMessage(payload, response.status, response.statusText);
692
+ throw new RagableError(msg, response.status, payload);
663
693
  }
664
694
  return payload;
665
695
  }
@@ -2446,6 +2476,7 @@ export {
2446
2476
  createRagableServerClient,
2447
2477
  detectStorage,
2448
2478
  extractErrorMessage,
2479
+ formatPostgrestError,
2449
2480
  formatRetrievalContext,
2450
2481
  formatSdkError,
2451
2482
  generateIdempotencyKey,