feat: basic error handling

Currently, this implements a simple runtime error which is raised when
either the private or public key generation fail. In the future, it
would make sense to include some runtime argument validation, and a more
parseable error.
This commit is contained in:
Erica Marigold 2024-03-28 16:12:44 +05:30
parent 2a49e303f9
commit 6dff9bbdb8
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1

View file

@ -16,16 +16,19 @@ export interface Wireguard {
export const wireguard: Wireguard = { export const wireguard: Wireguard = {
generateKeypair: function () { generateKeypair: function () {
const privateKey = generatePrivateKey(); const [privateKeyOk, privateKey] = pcall<[], number[]>(() => generatePrivateKey());
const publicKey = generatePublicKey(privateKey); const [publicKeyOk, publicKey] = privateKeyOk
? pcall<[], number[]>(() => generatePublicKey(privateKey))
: error("failed to generate private key");
return { return {
publicKey: atob(publicKey), publicKey: atob(publicKeyOk ? publicKey : error("failed to generate public key")),
privateKey: atob(privateKey), privateKey: atob(privateKey as number[]),
}; };
}, },
generatePublicKey: function (privateKey) { generatePublicKey: function (privateKey) {
return atob(generatePublicKey(privateKey)); const [publicKeyOk, publicKey] = pcall<[], number[]>(() => generatePublicKey(privateKey));
return atob(publicKeyOk ? publicKey : error("failed to generate public key"));
}, },
}; };