From 64dd5ad67eeed89b135ee80136a16398dc855270 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 18 Sep 2018 14:06:59 +0200 Subject: [PATCH] Improve error handling Signed-off-by: Knut Ahlers --- README.md | 4 ++-- index.js | 14 ++++++++------ main.go | 10 +++++----- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 9b0374f..1cd3bbc 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,6 @@ $ ls -lh main.wasm* Chrome dev console output: ``` -(index):14 (2) ["encryptResponse", "U2FsdGVkX1/baq9kUCX1EmUY/XZfnz7CwqGr70vqo6g="] -(index):10 (2) ["decryptResponse", "Knut"] +index.js:6 (3) ["encryptResponse", "U2FsdGVkX1+IAEdepsByQ9zEm11UWw4QSBPYsMzfiio=", null] +index.js:2 (3) ["decryptResponse", "Knut", null] ``` diff --git a/index.js b/index.js index b11b622..69775d6 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,15 @@ -function decryptResponse(plaintext) { - console.log(["decryptResponse", plaintext]) +function decryptResponse(plaintext, error) { + console.log(["decryptResponse", plaintext, error]) } -function encryptResponse(ciphertext) { - console.log(["encryptResponse", ciphertext]) - decrypt(ciphertext, "password", decryptResponse) +function encryptResponse(ciphertext, error) { + console.log(["encryptResponse", ciphertext, error]) + if (error === null) { + decrypt(ciphertext, "password", decryptResponse) + } } -function wasmStartSuccess() { +function opensslLoaded() { encrypt("Knut", "password", encryptResponse) } diff --git a/main.go b/main.go index c2eab4e..67a9a92 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ func main() { js.Global().Set("encrypt", js.NewCallback(encrypt)) // Trigger custom "event" - js.Global().Call("wasmStartSuccess") + js.Global().Call("opensslLoaded") <-make(chan struct{}, 0) } @@ -31,11 +31,11 @@ func decrypt(i []js.Value) { o := openssl.New() plaintext, err := o.DecryptString(password, ciphertext) if err != nil { - println(fmt.Sprintf("decrypt failed: %s", err)) + callback.Invoke(nil, fmt.Sprintf("decrypt failed: %s", err)) return } - callback.Invoke(string(plaintext)) + callback.Invoke(string(plaintext), nil) } func encrypt(i []js.Value) { @@ -53,9 +53,9 @@ func encrypt(i []js.Value) { o := openssl.New() ciphertext, err := o.EncryptString(password, plaintext) if err != nil { - println(fmt.Sprintf("encrypt failed: %s", err)) + callback.Invoke(nil, fmt.Sprintf("encrypt failed: %s", err)) return } - callback.Invoke(string(ciphertext)) + callback.Invoke(string(ciphertext), nil) }