simplify wallet message dispatching and error handling

This commit is contained in:
Florian Dold 2016-02-17 15:56:48 +01:00
parent 0c760bc2a1
commit 874d083ec3
5 changed files with 203 additions and 199 deletions

View File

@ -25,12 +25,20 @@
*/ */
export namespace Checkable { export namespace Checkable {
export function SchemaError(message) {
this.name = 'SchemaError';
this.message = message;
this.stack = (<any>new Error()).stack;
}
SchemaError.prototype = new Error;
let chkSym = Symbol("checkable"); let chkSym = Symbol("checkable");
function checkNumber(target, prop, path): any { function checkNumber(target, prop, path): any {
if ((typeof target) !== "number") { if ((typeof target) !== "number") {
throw Error(`expected number for ${path}`); throw new SchemaError(`expected number for ${path}`);
} }
return target; return target;
} }
@ -38,7 +46,7 @@ export namespace Checkable {
function checkString(target, prop, path): any { function checkString(target, prop, path): any {
if (typeof target !== "string") { if (typeof target !== "string") {
throw Error(`expected string for ${path}, got ${typeof target} instead`); throw new SchemaError(`expected string for ${path}, got ${typeof target} instead`);
} }
return target; return target;
} }
@ -46,7 +54,7 @@ export namespace Checkable {
function checkAnyObject(target, prop, path): any { function checkAnyObject(target, prop, path): any {
if (typeof target !== "object") { if (typeof target !== "object") {
throw Error(`expected (any) object for ${path}, got ${typeof target} instead`); throw new SchemaError(`expected (any) object for ${path}, got ${typeof target} instead`);
} }
return target; return target;
} }
@ -59,7 +67,7 @@ export namespace Checkable {
function checkList(target, prop, path): any { function checkList(target, prop, path): any {
if (!Array.isArray(target)) { if (!Array.isArray(target)) {
throw Error(`array expected for ${path}, got ${typeof target} instead`); throw new SchemaError(`array expected for ${path}, got ${typeof target} instead`);
} }
for (let i = 0; i < target.length; i++) { for (let i = 0; i < target.length; i++) {
let v = target[i]; let v = target[i];
@ -76,17 +84,20 @@ export namespace Checkable {
} }
let v = target; let v = target;
if (!v || typeof v !== "object") { if (!v || typeof v !== "object") {
throw Error(`expected object for ${path}, got ${typeof v} instead`); throw new SchemaError(`expected object for ${path}, got ${typeof v} instead`);
} }
let props = type.prototype[chkSym].props; let props = type.prototype[chkSym].props;
let remainingPropNames = new Set(Object.getOwnPropertyNames(v)); let remainingPropNames = new Set(Object.getOwnPropertyNames(v));
let obj = new type(); let obj = new type();
for (let prop of props) { for (let prop of props) {
if (!remainingPropNames.has(prop.propertyKey)) { if (!remainingPropNames.has(prop.propertyKey)) {
throw Error("Property missing: " + prop.propertyKey); if (prop.optional) {
continue;
}
throw new SchemaError("Property missing: " + prop.propertyKey);
} }
if (!remainingPropNames.delete(prop.propertyKey)) { if (!remainingPropNames.delete(prop.propertyKey)) {
throw Error("assertion failed"); throw new SchemaError("assertion failed");
} }
let propVal = v[prop.propertyKey]; let propVal = v[prop.propertyKey];
obj[prop.propertyKey] = prop.checker(propVal, obj[prop.propertyKey] = prop.checker(propVal,
@ -95,8 +106,8 @@ export namespace Checkable {
} }
if (remainingPropNames.size != 0) { if (remainingPropNames.size != 0) {
throw Error("superfluous properties " + JSON.stringify(Array.from( throw new SchemaError("superfluous properties " + JSON.stringify(Array.from(
remainingPropNames.values()))); remainingPropNames.values())));
} }
return obj; return obj;
} }
@ -162,14 +173,21 @@ export namespace Checkable {
export function AnyObject(target: Object, export function AnyObject(target: Object,
propertyKey: string | symbol): void { propertyKey: string | symbol): void {
let chk = mkChk(target); let chk = mkChk(target);
chk.props.push({propertyKey: propertyKey, checker: checkAnyObject}); chk.props.push({
propertyKey: propertyKey,
checker: checkAnyObject
});
} }
export function Any(target: Object, export function Any(target: Object,
propertyKey: string | symbol): void { propertyKey: string | symbol): void {
let chk = mkChk(target); let chk = mkChk(target);
chk.props.push({propertyKey: propertyKey, checker: checkAny}); chk.props.push({
propertyKey: propertyKey,
checker: checkAny,
optional: true
});
} }

View File

@ -13,34 +13,20 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/ */
System.register(["./wallet", "./db", "./http"], function(exports_1, context_1) { System.register(["./wallet", "./db", "./http", "./checkable"], function(exports_1, context_1) {
"use strict"; "use strict";
var __moduleName = context_1 && context_1.id; var __moduleName = context_1 && context_1.id;
var wallet_1, db_1, http_1; var wallet_1, db_1, http_1, checkable_1;
var ChromeBadge; var ChromeBadge;
/** function makeHandlers(db, wallet) {
* Messaging for the WebExtensions wallet. Should contain
* parts that are specific for WebExtensions, but as little business
* logic as possible.
*
* @author Florian Dold
*/
function makeHandlers(wallet) {
return (_a = {}, return (_a = {},
_a["balances"] = function (db, detail, sendResponse) { _a["balances"] = function (detail) {
wallet.getBalances() return wallet.getBalances();
.then(sendResponse)
.catch(function (e) {
console.log("exception during 'balances'");
console.error(e.stack);
});
return true;
}, },
_a["dump-db"] = function (db, detail, sendResponse) { _a["dump-db"] = function (detail) {
db_1.exportDb(db).then(sendResponse); return db_1.exportDb(db);
return true;
}, },
_a["reset"] = function (db, detail, sendResponse) { _a["reset"] = function (detail) {
var tx = db.transaction(db.objectStoreNames, 'readwrite'); var tx = db.transaction(db.objectStoreNames, 'readwrite');
for (var i = 0; i < db.objectStoreNames.length; i++) { for (var i = 0; i < db.objectStoreNames.length; i++) {
tx.objectStore(db.objectStoreNames[i]).clear(); tx.objectStore(db.objectStoreNames[i]).clear();
@ -49,108 +35,102 @@ System.register(["./wallet", "./db", "./http"], function(exports_1, context_1) {
chrome.browserAction.setBadgeText({ text: "" }); chrome.browserAction.setBadgeText({ text: "" });
console.log("reset done"); console.log("reset done");
// Response is synchronous // Response is synchronous
return false; return Promise.resolve({});
}, },
_a["create-reserve"] = function (db, detail, sendResponse) { _a["create-reserve"] = function (detail) {
var d = { var d = {
mint: detail.mint, mint: detail.mint,
amount: detail.amount, amount: detail.amount,
}; };
var req = wallet_1.CreateReserveRequest.checked(d); var req = wallet_1.CreateReserveRequest.checked(d);
wallet.createReserve(req) return wallet.createReserve(req);
.then(function (resp) {
sendResponse(resp);
})
.catch(function (e) {
sendResponse({ error: "exception" });
console.error("exception during 'create-reserve'");
console.error(e.stack);
});
return true;
}, },
_a["confirm-reserve"] = function (db, detail, sendResponse) { _a["confirm-reserve"] = function (detail) {
// TODO: make it a checkable // TODO: make it a checkable
var d = { var d = {
reservePub: detail.reservePub reservePub: detail.reservePub
}; };
var req = wallet_1.ConfirmReserveRequest.checked(d); var req = wallet_1.ConfirmReserveRequest.checked(d);
wallet.confirmReserve(req) return wallet.confirmReserve(req);
.then(function (resp) {
sendResponse(resp);
})
.catch(function (e) {
sendResponse({ error: "exception" });
console.error("exception during 'confirm-reserve'");
console.error(e.stack);
});
return true;
}, },
_a["confirm-pay"] = function (db, detail, sendResponse) { _a["confirm-pay"] = function (detail) {
console.log("in confirm-pay handler"); var offer;
var offer = wallet_1.Offer.checked(detail.offer); try {
wallet.confirmPay(offer) offer = wallet_1.Offer.checked(detail.offer);
.then(function (r) { }
sendResponse(r); catch (e) {
}) if (e instanceof checkable_1.Checkable.SchemaError) {
.catch(function (e) { console.error("schema error:", e.message);
console.error("exception during 'confirm-pay'"); return Promise.resolve({ error: "invalid contract", hint: e.message });
console.error(e.stack); }
sendResponse({ error: e.message }); else {
}); throw e;
return true; }
}
return wallet.confirmPay(offer);
}, },
_a["execute-payment"] = function (db, detail, sendResponse) { _a["execute-payment"] = function (detail) {
wallet.executePayment(detail.H_contract) return wallet.executePayment(detail.H_contract);
.then(function (r) {
sendResponse(r);
})
.catch(function (e) {
console.error("exception during 'execute-payment'");
console.error(e.stack);
sendResponse({ error: e.message });
});
// async sendResponse
return true;
}, },
_a["get-history"] = function (db, detail, sendResponse) { _a["get-history"] = function (detail) {
// TODO: limit history length // TODO: limit history length
wallet.getHistory() return wallet.getHistory();
.then(function (h) {
sendResponse(h);
})
.catch(function (e) {
console.error("exception during 'get-history'");
console.error(e.stack);
});
return true;
},
_a["error-fatal"] = function (db, detail, sendResponse) {
console.log("fatal error from page", detail.url);
}, },
_a _a
); );
var _a; var _a;
} }
function dispatch(handlers, db, req, sendResponse) {
if (req.type in handlers) {
Promise
.resolve()
.then(function () {
var p = handlers[req.type](db, req.detail);
return p.then(function (r) {
sendResponse(r);
});
})
.catch(function (e) {
console.log("exception during wallet handler'");
console.error(e.stack);
sendResponse({
error: "exception",
hint: e.message,
stack: e.stack.toString()
});
});
// The sendResponse call is async
return true;
}
else {
console.error("Request type " + JSON.stringify(req) + " unknown, req " + req.type);
sendResponse({ error: "request unknown" });
// The sendResponse call is sync
return false;
}
}
function wxMain() { function wxMain() {
chrome.browserAction.setBadgeText({ text: "" }); chrome.browserAction.setBadgeText({ text: "" });
db_1.openTalerDb() Promise.resolve()
.then(function () {
return db_1.openTalerDb();
})
.catch(function (e) {
console.error("could not open database");
console.error(e);
})
.then(function (db) { .then(function (db) {
var http = new http_1.BrowserHttpLib(); var http = new http_1.BrowserHttpLib();
var badge = new ChromeBadge(); var badge = new ChromeBadge();
var wallet = new wallet_1.Wallet(db, http, badge); var wallet = new wallet_1.Wallet(db, http, badge);
var handlers = makeHandlers(wallet); var handlers = makeHandlers(db, wallet);
chrome.runtime.onMessage.addListener(function (req, sender, onresponse) { chrome.runtime.onMessage.addListener(function (req, sender, sendResponse) {
if (req.type in handlers) { return dispatch(handlers, db, req, sendResponse);
return handlers[req.type](db, req.detail, onresponse);
}
console.error("Request type " + JSON.stringify(req) + " unknown, req " + req.type);
onresponse({ error: "request unknown" });
return false;
}); });
}) })
.catch(function (e) { .catch(function (e) {
console.error("could not open database:"); console.error("could not initialize wallet messaging");
console.error(e.stack); console.error(e);
}); });
} }
exports_1("wxMain", wxMain); exports_1("wxMain", wxMain);
@ -164,6 +144,9 @@ System.register(["./wallet", "./db", "./http"], function(exports_1, context_1) {
}, },
function (http_1_1) { function (http_1_1) {
http_1 = http_1_1; http_1 = http_1_1;
},
function (checkable_1_1) {
checkable_1 = checkable_1_1;
}], }],
execute: function() { execute: function() {
"use strict"; "use strict";

View File

@ -18,6 +18,7 @@
import {Wallet, Offer, Badge, ConfirmReserveRequest, CreateReserveRequest} from "./wallet"; import {Wallet, Offer, Badge, ConfirmReserveRequest, CreateReserveRequest} from "./wallet";
import {deleteDb, exportDb, openTalerDb} from "./db"; import {deleteDb, exportDb, openTalerDb} from "./db";
import {BrowserHttpLib} from "./http"; import {BrowserHttpLib} from "./http";
import {Checkable} from "./checkable";
"use strict"; "use strict";
@ -30,22 +31,18 @@ import {BrowserHttpLib} from "./http";
*/ */
function makeHandlers(wallet: Wallet) { type Handler = (detail: any) => Promise<any>;
function makeHandlers(db: IDBDatabase,
wallet: Wallet): {[msg: string]: Handler} {
return { return {
["balances"]: function(db, detail, sendResponse) { ["balances"]: function(detail) {
wallet.getBalances() return wallet.getBalances();
.then(sendResponse)
.catch((e) => {
console.log("exception during 'balances'");
console.error(e.stack);
});
return true;
}, },
["dump-db"]: function(db, detail, sendResponse) { ["dump-db"]: function(detail) {
exportDb(db).then(sendResponse); return exportDb(db);
return true;
}, },
["reset"]: function(db, detail, sendResponse) { ["reset"]: function(detail) {
let tx = db.transaction(db.objectStoreNames, 'readwrite'); let tx = db.transaction(db.objectStoreNames, 'readwrite');
for (let i = 0; i < db.objectStoreNames.length; i++) { for (let i = 0; i < db.objectStoreNames.length; i++) {
tx.objectStore(db.objectStoreNames[i]).clear(); tx.objectStore(db.objectStoreNames[i]).clear();
@ -55,84 +52,46 @@ function makeHandlers(wallet: Wallet) {
chrome.browserAction.setBadgeText({text: ""}); chrome.browserAction.setBadgeText({text: ""});
console.log("reset done"); console.log("reset done");
// Response is synchronous // Response is synchronous
return false; return Promise.resolve({});
}, },
["create-reserve"]: function(db, detail, sendResponse) { ["create-reserve"]: function(detail) {
const d = { const d = {
mint: detail.mint, mint: detail.mint,
amount: detail.amount, amount: detail.amount,
}; };
const req = CreateReserveRequest.checked(d); const req = CreateReserveRequest.checked(d);
wallet.createReserve(req) return wallet.createReserve(req);
.then((resp) => {
sendResponse(resp);
})
.catch((e) => {
sendResponse({error: "exception"});
console.error("exception during 'create-reserve'");
console.error(e.stack);
});
return true;
}, },
["confirm-reserve"]: function(db, detail, sendResponse) { ["confirm-reserve"]: function(detail) {
// TODO: make it a checkable // TODO: make it a checkable
const d = { const d = {
reservePub: detail.reservePub reservePub: detail.reservePub
}; };
const req = ConfirmReserveRequest.checked(d); const req = ConfirmReserveRequest.checked(d);
wallet.confirmReserve(req) return wallet.confirmReserve(req);
.then((resp) => {
sendResponse(resp);
})
.catch((e) => {
sendResponse({error: "exception"});
console.error("exception during 'confirm-reserve'");
console.error(e.stack);
});
return true;
}, },
["confirm-pay"]: function(db, detail, sendResponse) { ["confirm-pay"]: function(detail) {
console.log("in confirm-pay handler"); let offer;
const offer = Offer.checked(detail.offer); try {
wallet.confirmPay(offer) offer = Offer.checked(detail.offer);
.then((r) => { } catch (e) {
sendResponse(r) if (e instanceof Checkable.SchemaError) {
}) console.error("schema error:", e.message);
.catch((e) => { return Promise.resolve({error: "invalid contract", hint: e.message});
console.error("exception during 'confirm-pay'"); } else {
console.error(e.stack); throw e;
sendResponse({error: e.message}); }
}); }
return true;
return wallet.confirmPay(offer);
}, },
["execute-payment"]: function(db, detail, sendResponse) { ["execute-payment"]: function(detail) {
wallet.executePayment(detail.H_contract) return wallet.executePayment(detail.H_contract);
.then((r) => {
sendResponse(r);
})
.catch((e) => {
console.error("exception during 'execute-payment'");
console.error(e.stack);
sendResponse({error: e.message});
});
// async sendResponse
return true;
}, },
["get-history"]: function(db, detail, sendResponse) { ["get-history"]: function(detail) {
// TODO: limit history length // TODO: limit history length
wallet.getHistory() return wallet.getHistory();
.then((h) => {
sendResponse(h);
})
.catch((e) => {
console.error("exception during 'get-history'");
console.error(e.stack);
});
return true;
}, },
["error-fatal"]: function(db, detail, sendResponse) {
console.log("fatal error from page", detail.url);
}
}; };
} }
@ -148,27 +107,59 @@ class ChromeBadge implements Badge {
} }
function dispatch(handlers, db, req, sendResponse) {
if (req.type in handlers) {
Promise
.resolve()
.then(() => {
const p = handlers[req.type](db, req.detail);
return p.then((r) => {
sendResponse(r);
})
})
.catch((e) => {
console.log("exception during wallet handler'");
console.error(e.stack);
sendResponse({
error: "exception",
hint: e.message,
stack: e.stack.toString()
});
});
// The sendResponse call is async
return true;
} else {
console.error(`Request type ${JSON.stringify(req)} unknown, req ${req.type}`);
sendResponse({error: "request unknown"});
// The sendResponse call is sync
return false;
}
}
export function wxMain() { export function wxMain() {
chrome.browserAction.setBadgeText({text: ""}); chrome.browserAction.setBadgeText({text: ""});
openTalerDb() Promise.resolve()
.then((db) => { .then(() => {
let http = new BrowserHttpLib(); return openTalerDb();
let badge = new ChromeBadge(); })
let wallet = new Wallet(db, http, badge); .catch((e) => {
let handlers = makeHandlers(wallet); console.error("could not open database");
chrome.runtime.onMessage.addListener( console.error(e);
function(req, sender, onresponse) { })
if (req.type in handlers) { .then((db) => {
return handlers[req.type](db, req.detail, onresponse); let http = new BrowserHttpLib();
} let badge = new ChromeBadge();
console.error(`Request type ${JSON.stringify(req)} unknown, req ${req.type}`); let wallet = new Wallet(db, http, badge);
onresponse({error: "request unknown"}); let handlers = makeHandlers(db, wallet);
return false; chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
}); return dispatch(handlers, db, req, sendResponse)
}) });
.catch((e) => { })
console.error("could not open database:"); .catch((e) => {
console.error(e.stack); console.error("could not initialize wallet messaging");
}); console.error(e);
});
} }

View File

@ -48,6 +48,9 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril"], fun
return mithril_1.default("div", controls); return mithril_1.default("div", controls);
var _a; var _a;
} }
function probeMint(mintBaseUrl) {
throw Error("not implemented");
}
function getSuggestedMint(currency) { function getSuggestedMint(currency) {
// TODO: make this request go to the wallet backend // TODO: make this request go to the wallet backend
// Right now, this is a stub. // Right now, this is a stub.

View File

@ -200,6 +200,15 @@ function view(ctrl: Controller) {
} }
interface MintProbeResult {
keyInfo?: any;
}
function probeMint(mintBaseUrl: string): Promise<MintProbeResult> {
throw Error("not implemented");
}
function getSuggestedMint(currency: string): Promise<string> { function getSuggestedMint(currency: string): Promise<string> {
// TODO: make this request go to the wallet backend // TODO: make this request go to the wallet backend
// Right now, this is a stub. // Right now, this is a stub.