fix: missing crypto error handling improvement

This commit is contained in:
Sebastian 2022-09-28 13:15:31 -03:00
parent 50d6047782
commit 7ce1cea1c7
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -15,6 +15,7 @@
*/ */
import { Logger } from "@gnu-taler/taler-util"; import { Logger } from "@gnu-taler/taler-util";
import { getErrorDetailFromException } from "../../errors.js";
import { import {
nativeCryptoR, nativeCryptoR,
TalerCryptoInterfaceR, TalerCryptoInterfaceR,
@ -60,7 +61,7 @@ export class SynchronousCryptoWorker {
private dispatchMessage(msg: any): void { private dispatchMessage(msg: any): void {
if (this.onmessage) { if (this.onmessage) {
this.onmessage({ data: msg }); this.onmessage(msg);
} }
} }
@ -72,20 +73,27 @@ export class SynchronousCryptoWorker {
const impl = this.cryptoImplR; const impl = this.cryptoImplR;
if (!(operation in impl)) { if (!(operation in impl)) {
console.error(`crypto operation '${operation}' not found`); logger.error(`crypto operation '${operation}' not found`);
return; return;
} }
let result: any; let responseMsg: any;
try { try {
result = await (impl as any)[operation](impl, req); const result = await (impl as any)[operation](impl, req);
responseMsg = { data: { type: "success", result, id } };
} catch (e: any) { } catch (e: any) {
logger.error(`error during operation '${operation}': ${e}`); logger.error(`error during operation: ${e.stack ?? e.toString()}`);
return; responseMsg = {
data: {
type: "error",
id,
error: getErrorDetailFromException(e),
},
};
} }
try { try {
setTimeout(() => this.dispatchMessage({ result, id }), 0); setTimeout(() => this.dispatchMessage(responseMsg), 0);
} catch (e) { } catch (e) {
logger.error("got error during dispatch", e); logger.error("got error during dispatch", e);
} }
@ -97,22 +105,22 @@ export class SynchronousCryptoWorker {
postMessage(msg: any): void { postMessage(msg: any): void {
const req = msg.req; const req = msg.req;
if (typeof req !== "object") { if (typeof req !== "object") {
console.error("request must be an object"); logger.error("request must be an object");
return; return;
} }
const id = msg.id; const id = msg.id;
if (typeof id !== "number") { if (typeof id !== "number") {
console.error("RPC id must be number"); logger.error("RPC id must be number");
return; return;
} }
const operation = msg.operation; const operation = msg.operation;
if (typeof operation !== "string") { if (typeof operation !== "string") {
console.error("RPC operation must be string"); logger.error("RPC operation must be string");
return; return;
} }
this.handleRequest(operation, id, req).catch((e) => { this.handleRequest(operation, id, req).catch((e) => {
console.error("Error while handling crypto request:", e); logger.error("Error while handling crypto request:", e);
}); });
} }