Skip to content

Commit 3392d8f

Browse files
committed
JS client: Better handling of error output
1 parent c334fe5 commit 3392d8f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

clients/typescript/src/model.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ type cstr = internalTypes["cstr"];
2929
*/
3030
export default class StanModel {
3131
private m: WasmModule;
32-
private printCallback: PrintCallback | null;
32+
private printErrorCallback: PrintCallback | null;
3333
// used to send multiple JSON values in one string
3434
private sep: string;
3535

3636
private constructor(m: WasmModule, pc: PrintCallback | null) {
3737
this.m = m;
38-
this.printCallback = pc;
38+
this.printErrorCallback = pc;
3939
this.sep = String.fromCharCode(m._tinystan_separator_char());
4040
}
4141

@@ -49,16 +49,18 @@ export default class StanModel {
4949
* @returns {Promise<StanModel>} A promise that resolves to a `StanModel`
5050
*/
5151
public static async load(
52-
createModule: (proto?: object) => Promise<WasmModule>,
53-
printCallback: PrintCallback | null,
52+
createModule: (moduleArg?: object) => Promise<object>,
53+
printCallback: PrintCallback | null = null,
54+
printErrorCallback: PrintCallback | null = null,
5455
): Promise<StanModel> {
5556
// Create the initial object which will have the rest of the WASM
5657
// functions attached to it
5758
// See https://emscripten.org/docs/api_reference/module.html
58-
const prototype = { print: printCallback };
59+
printErrorCallback = printErrorCallback ?? printCallback;
60+
const prototype = { print: printCallback, printErr: printErrorCallback };
5961

6062
const module = await createModule(prototype);
61-
return new StanModel(module, printCallback);
63+
return new StanModel(module as WasmModule, printErrorCallback);
6264
}
6365

6466
private encodeString(s: string): cstr {
@@ -79,7 +81,7 @@ export default class StanModel {
7981
const err_msg = "Exception from Stan:\n" + this.m.UTF8ToString(err_msg_ptr);
8082
this.m._tinystan_destroy_error(err);
8183
this.m._free(err_ptr);
82-
this.printCallback?.(err_msg);
84+
this.printErrorCallback?.(err_msg);
8385
throw new Error(err_msg);
8486
}
8587

0 commit comments

Comments
 (0)