Skip to content

Commit 6d5e9d6

Browse files
committed
improve musicxml2pv error handling
1 parent 05b0b54 commit 6d5e9d6

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

annotation-tool/src/App/Utils.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import jsDownload from "downloadjs";
44
import { WASI } from "@runno/wasi";
55

6-
7-
const wasm_binary = fetch(new URL("musicxml2pv.wasm", import.meta.url));
8-
96
export const copyToClipboard = str => () => navigator.clipboard.writeText(str);
107

118
export function download_ (data) {
@@ -139,16 +136,16 @@ export const examplePieceJSONLong = [
139136
]},
140137
];
141138

142-
export const musicxml2pv = (unfold) => (musicxml) => async () => {
143-
var output = null;
139+
export const musicxml2pv = (mkLeft) => (mkRight) => (unfold) => (musicxml) => async () => {
140+
var output = "";
144141
var args = ["musicxml2pv", "/input.musicxml"];
145142
if (unfold) {
146143
args.splice(1, 0, "-u");
147144
}
148145
console.log(args);
149146
const wasi = new WASI({
150147
args: args,
151-
stdout: (out) => output = out,
148+
stdout: (out) => output += out,
152149
stderr: (err) => console.error("wasm err:", err),
153150
fs: {
154151
"/input.musicxml": {
@@ -165,10 +162,17 @@ export const musicxml2pv = (unfold) => (musicxml) => async () => {
165162
});
166163
// console.log(wasi);
167164

168-
const wasm = await WebAssembly.instantiateStreaming(wasm_binary, wasi.getImportObject());
165+
const wasm = await WebAssembly.instantiateStreaming(fetch(new URL("musicxml2pv.wasm", import.meta.url)), wasi.getImportObject());
169166
// console.log(wasm);
170167

171-
await wasi.start(wasm, {});
172-
return output;
168+
result = await wasi.start(wasm, {});
169+
console.log(result);
170+
if (output == "") {
171+
return mkLeft("musicxml2pv returned empty string");
172+
} else if (result.exitCode != 0) {
173+
return mkLeft("musicxml2pv returned exit code" + result.exitCode.toString());
174+
} else {
175+
return mkRight(output);
176+
}
173177
};
174178

annotation-tool/src/App/Utils.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ download :: String -> String -> String -> Effect Boolean
4040
download str filename mimetype = download_ (unsafeToForeign str) filename mimetype
4141

4242
-- TODO: better error handling
43-
foreign import musicxml2pv :: Boolean -> String -> Effect (Promise String)
43+
foreign import musicxml2pv :: (forall a b. a -> Either a b) -> (forall a b. b -> Either a b) -> Boolean -> String -> Effect (Promise (Either String String))
4444

4545
convertMusicXML :: Boolean -> String -> Aff (Either String String)
4646
convertMusicXML unfold input = do
47-
result <- toAffE $ musicxml2pv unfold input
48-
pure $ Right result
47+
result <- toAffE $ musicxml2pv Left Right unfold input
48+
pure $ result

0 commit comments

Comments
 (0)