hs-playlib 0.2.0 → 0.4.0

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.
@@ -18,6 +18,8 @@ const koffi_1 = __importDefault(require("koffi"));
18
18
  const path_1 = __importDefault(require("path"));
19
19
  // @ts-ignore - generated CommonJS module
20
20
  const _generated_flows_js_1 = require("./_generated_flows.js");
21
+ // @ts-ignore - generated protobuf types
22
+ const proto_js_1 = require("./generated/proto.js");
21
23
  const _dirname = __dirname;
22
24
  const FLOW_NAMES = Object.keys(_generated_flows_js_1.FLOWS);
23
25
  const SINGLE_FLOW_NAMES = Object.keys((_generated_flows_js_1.SINGLE_FLOWS || {}));
@@ -58,11 +60,8 @@ function makeCallStatus() {
58
60
  function checkCallStatus(ffi, status) {
59
61
  if (status.code === 0)
60
62
  return;
61
- if (status.code === 1) {
62
- const errMsg = liftError(status.error_buf);
63
- freeRustBuffer(ffi, status.error_buf);
64
- throw new Error(errMsg);
65
- }
63
+ // Only Rust panics should reach here now (status.code === 2)
64
+ // Normal errors are encoded as protobuf RequestError/ResponseError in returned bytes
66
65
  if (status.error_buf.len > 0n) {
67
66
  const msg = liftString(status.error_buf);
68
67
  freeRustBuffer(ffi, status.error_buf);
@@ -70,28 +69,6 @@ function checkCallStatus(ffi, status) {
70
69
  }
71
70
  throw new Error("Unknown Rust panic");
72
71
  }
73
- function liftError(buf) {
74
- if (!buf.data || buf.len === 0n)
75
- return "Unknown error";
76
- const raw = Buffer.from(koffi_1.default.decode(buf.data, "uint8", Number(buf.len)));
77
- let offset = 0;
78
- // UniFFI Error layout: [i32 variant] + [i32 len] + [bytes]
79
- const variant = raw.readInt32BE(offset);
80
- offset += 4;
81
- const variantNames = {
82
- 1: "DecodeError",
83
- 2: "MissingMetadata",
84
- 3: "MetadataParseError",
85
- 4: "HandlerError",
86
- 5: "NoConnectorRequest",
87
- };
88
- if (variant === 5)
89
- return "NoConnectorRequest";
90
- const strLen = raw.readInt32BE(offset);
91
- offset += 4;
92
- const msg = raw.subarray(offset, offset + strLen).toString("utf-8");
93
- return `${variantNames[variant] || "UniffiError"}: ${msg}`;
94
- }
95
72
  /**
96
73
  * UniFFI Strings are serialized as raw UTF8 bytes when top-level in RustBuffer.
97
74
  */
@@ -154,7 +131,21 @@ class UniffiClient {
154
131
  const result = fn(rbReq, rbOpts, status);
155
132
  try {
156
133
  checkCallStatus(this._ffi, status);
157
- return liftBytes(result);
134
+ const bytes = liftBytes(result);
135
+ try {
136
+ const reqErr = proto_js_1.types.RequestError.decode(bytes);
137
+ if (reqErr.errorMessage) {
138
+ const err = new Error(reqErr.errorMessage);
139
+ err.ffiError = reqErr;
140
+ throw err;
141
+ }
142
+ }
143
+ catch (e) {
144
+ if (e.ffiError)
145
+ throw e;
146
+ // decode failed — not an error proto, return bytes as-is
147
+ }
148
+ return bytes;
158
149
  }
159
150
  finally {
160
151
  freeRustBuffer(this._ffi, result);
@@ -176,7 +167,21 @@ class UniffiClient {
176
167
  const result = fn(rbRes, rbReq, rbOpts, status);
177
168
  try {
178
169
  checkCallStatus(this._ffi, status);
179
- return liftBytes(result);
170
+ const bytes = liftBytes(result);
171
+ try {
172
+ const resErr = proto_js_1.types.ResponseError.decode(bytes);
173
+ if (resErr.errorMessage) {
174
+ const err = new Error(resErr.errorMessage);
175
+ err.ffiError = resErr;
176
+ throw err;
177
+ }
178
+ }
179
+ catch (e) {
180
+ if (e.ffiError)
181
+ throw e;
182
+ // decode failed — not an error proto, return bytes as-is
183
+ }
184
+ return bytes;
180
185
  }
181
186
  finally {
182
187
  freeRustBuffer(this._ffi, result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hs-playlib",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Hyperswitch Payments SDK — Node.js client for connector integrations via UniFFI FFI",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",