Skip to content

Commit

Permalink
feat: using cause property to establish error chain
Browse files Browse the repository at this point in the history
When any error is thrown as the result of another error occurring, the original error is now contained within the `cause` property of the new error.

#304
  • Loading branch information
emmacasolin committed Jun 6, 2022
1 parent bc8a3f7 commit 6882759
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/bin/keys/CommandDecrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class CommandDecrypt extends CommandPolykey {
code: e.code,
path: e.path,
},
cause: e,
});
}
cryptoMessage.setData(cipherText);
Expand Down
1 change: 1 addition & 0 deletions src/bin/keys/CommandEncrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class CommandEncypt extends CommandPolykey {
code: e.code,
path: e.path,
},
cause: e,
});
}
cryptoMessage.setData(plainText);
Expand Down
1 change: 1 addition & 0 deletions src/bin/keys/CommandSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class CommandSign extends CommandPolykey {
code: e.code,
path: e.path,
},
cause: e,
});
}
cryptoMessage.setData(data);
Expand Down
1 change: 1 addition & 0 deletions src/bin/keys/CommandVerify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class CommandVerify extends CommandPolykey {
code: e.code,
path: e.path,
},
cause: e,
});
}
cryptoMessage.setData(data);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/nodes/CommandPing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CommandPing extends CommandPolykey {
error = new binErrors.ErrorNodePingFailed(
`Failed to resolve node ID ${nodesUtils.encodeNodeId(
nodeId,
)} to an address.`,
)} to an address.`, { cause: err },
);
} else {
throw err;
Expand Down
1 change: 1 addition & 0 deletions src/bin/secrets/CommandCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class CommandCreate extends CommandPolykey {
code: e.code,
path: e.path,
},
cause: e,
});
}
secretMessage.setSecretContent(content);
Expand Down
1 change: 1 addition & 0 deletions src/bin/secrets/CommandEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CommandEdit extends CommandPolykey {
code: e.code,
path: e.path,
},
cause: e,
});
}
secretMessage.setVault(vaultMessage);
Expand Down
1 change: 1 addition & 0 deletions src/bin/secrets/CommandUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class CommandUpdate extends CommandPolykey {
code: e.code,
path: e.path,
},
cause: e,
});
}
secretMessage.setSecretContent(content);
Expand Down
4 changes: 4 additions & 0 deletions src/bin/utils/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async function processPassword(
code: e.code,
path: e.path,
},
cause: e,
});
}
} else if (typeof process.env['PK_PASSWORD'] === 'string') {
Expand Down Expand Up @@ -139,6 +140,7 @@ async function processNewPassword(
code: e.code,
path: e.path,
},
cause: e,
});
}
} else if (!existing && typeof process.env['PK_PASSWORD'] === 'string') {
Expand Down Expand Up @@ -177,6 +179,7 @@ async function processRecoveryCode(
code: e.code,
path: e.path,
},
cause: e,
});
}
} else if (typeof process.env['PK_RECOVERY_CODE'] === 'string') {
Expand Down Expand Up @@ -384,6 +387,7 @@ async function processAuthentication(
code: e.code,
path: e.path,
},
cause: e,
});
}
meta = clientUtils.encodeAuthFromPassword(password);
Expand Down
8 changes: 5 additions & 3 deletions src/grpc/GRPCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ abstract class GRPCClient<T extends Client = Client> {
} catch (e) {
// If we fail here then we leak the client object...
client.close();
throw new grpcErrors.ErrorGRPCClientTimeout();
throw new grpcErrors.ErrorGRPCClientTimeout(e.message, { cause: e });
}
let serverCertChain: Array<Certificate> | undefined;
if (channelCredentials._isSecure()) {
Expand All @@ -151,8 +151,10 @@ abstract class GRPCClient<T extends Client = Client> {
`Failed GRPC server certificate verification connecting to ${address}`,
);
const e_ = new grpcErrors.ErrorGRPCClientVerification(
`${e.name}: ${e.message}`,
e.data,
`${e.name}: ${e.message}`,{
data: e.data,
cause: e,
},
);
session.destroy(e_, http2.constants.NGHTTP2_PROTOCOL_ERROR);
}
Expand Down
10 changes: 6 additions & 4 deletions src/grpc/GRPCServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class GRPCServer {
try {
this.port = await bindAsync(address, serverCredentials);
} catch (e) {
throw new grpcErrors.ErrorGRPCServerBind(e.message);
throw new grpcErrors.ErrorGRPCServerBind(e.message, { cause: e });
}
if (serverCredentials._isSecure()) {
// @ts-ignore hack for private property
Expand Down Expand Up @@ -100,8 +100,10 @@ class GRPCServer {
`Failed GRPC client certificate verification connecting from ${address}`,
);
const e_ = new grpcErrors.ErrorGRPCServerVerification(
`${e.name}: ${e.message}`,
e.data,
`${e.name}: ${e.message}`, {
data: e.data,
cause: e,
},
);
session.destroy(e_, http2.constants.NGHTTP2_PROTOCOL_ERROR);
} else {
Expand Down Expand Up @@ -140,7 +142,7 @@ class GRPCServer {
...(timer != null ? [timer.timerP] : []),
]);
} catch (e) {
throw new grpcErrors.ErrorGRPCServerShutdown(e.message);
throw new grpcErrors.ErrorGRPCServerShutdown(e.message, { cause: e });
} finally {
if (timer != null) timerStop(timer);
}
Expand Down
8 changes: 7 additions & 1 deletion src/identities/providers/github/GitHubProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class GitHubProvider extends Provider {
data = await response.json();
} catch (e) {
throw new identitiesErrors.ErrorProviderAuthentication(
'Provider access token response is not valid JSON',
'Provider access token response is not valid JSON', { cause: e },
);
}
if (data.error) {
Expand Down Expand Up @@ -200,6 +200,7 @@ class GitHubProvider extends Provider {
} catch (e) {
throw new identitiesErrors.ErrorProviderCall(
`Provider response body is not valid JSON`,
{ cause: e },
);
}
return data.login;
Expand Down Expand Up @@ -246,6 +247,7 @@ class GitHubProvider extends Provider {
} catch (e) {
throw new identitiesErrors.ErrorProviderCall(
`Provider response body is not valid JSON`,
{ cause: e },
);
}
return {
Expand Down Expand Up @@ -297,6 +299,7 @@ class GitHubProvider extends Provider {
} catch (e) {
throw new identitiesErrors.ErrorProviderCall(
`Provider response body is not valid JSON`,
{ cause: e },
);
}
for (const item of data) {
Expand Down Expand Up @@ -343,6 +346,7 @@ class GitHubProvider extends Provider {
} catch (e) {
throw new identitiesErrors.ErrorProviderCall(
`Provider response body is not valid JSON`,
{ cause: e },
);
}
for (const item of data) {
Expand Down Expand Up @@ -414,6 +418,7 @@ class GitHubProvider extends Provider {
} catch (e) {
throw new identitiesErrors.ErrorProviderCall(
`Provider response body is not valid JSON`,
{ cause: e },
);
}
return {
Expand Down Expand Up @@ -466,6 +471,7 @@ class GitHubProvider extends Provider {
} catch (e) {
throw new identitiesErrors.ErrorProviderCall(
`Provider response body is not valid JSON`,
{ cause: e },
);
}
const linkClaimData = data.files[this.gistFilename]?.content;
Expand Down
19 changes: 17 additions & 2 deletions src/keys/KeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
try {
Expand Down Expand Up @@ -422,6 +423,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
await this.garbageCollectRootCerts();
Expand Down Expand Up @@ -545,6 +547,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
}
Expand Down Expand Up @@ -642,6 +645,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
return true;
Expand All @@ -663,6 +667,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
let keyPair;
Expand All @@ -675,7 +680,7 @@ class KeyManager {
password,
);
} catch (e) {
throw new keysErrors.ErrorRootKeysParse(e.message);
throw new keysErrors.ErrorRootKeysParse(e.message, { cause: e });
}
return keyPair;
}
Expand Down Expand Up @@ -709,6 +714,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
}
Expand All @@ -735,6 +741,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
const rootKeyPairBits = keysUtils.publicKeyBitSize(
Expand Down Expand Up @@ -784,6 +791,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
return true;
Expand All @@ -802,6 +810,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
let keysDbKeyPlain;
Expand All @@ -811,7 +820,7 @@ class KeyManager {
keysDbKeyCipher,
);
} catch (e) {
throw new keysErrors.ErrorDBKeyParse(e.message);
throw new keysErrors.ErrorDBKeyParse(e.message, { cause: e });
}
return keysDbKeyPlain;
}
Expand All @@ -838,6 +847,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
}
Expand Down Expand Up @@ -881,6 +891,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
return true;
Expand All @@ -901,6 +912,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
const rootCert = keysUtils.certFromPem(rootCertPem);
Expand All @@ -924,6 +936,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
}
Expand All @@ -945,6 +958,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
rootCertsNames.sort((a, b) => {
Expand Down Expand Up @@ -984,6 +998,7 @@ class KeyManager {
code: e.code,
path: e.path,
},
cause: e,
});
}
return rootCertsPems;
Expand Down
1 change: 1 addition & 0 deletions src/network/ConnectionForward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class ConnectionForward extends Connection {
errno: e.errno,
syscall: e.syscall,
},
cause: e,
});
} finally {
clearInterval(punchInterval);
Expand Down
2 changes: 2 additions & 0 deletions src/network/ConnectionReverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class ConnectionReverse extends Connection {
errno: e.errno,
syscall: e.syscall,
},
cause: e,
});
} finally {
clearInterval(punchInterval);
Expand Down Expand Up @@ -254,6 +255,7 @@ class ConnectionReverse extends Connection {
errno: e.errno,
syscall: e.syscall,
},
cause: e,
});
}
tlsSocket.on('error', async (e) => {
Expand Down
2 changes: 1 addition & 1 deletion src/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async function resolveHost(host: Host | Hostname): Promise<Host> {
// Resolve the hostname and get the IPv4 address
resolvedHost = await lookup(host, 4);
} catch (e) {
throw new networkErrors.ErrorHostnameResolutionFailed(e.message);
throw new networkErrors.ErrorHostnameResolutionFailed(e.message, { cause: e });
}
// Returns an array of [ resolved address, family (4 or 6) ]
return resolvedHost[0] as Host;
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/NodeConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class NodeConnection<T extends GRPCClient> {
await nodeConnection.destroy();
// If the connection times out, re-throw this with a higher level nodes exception
if (e instanceof grpcErrors.ErrorGRPCClientTimeout) {
throw new nodesErrors.ErrorNodeConnectionTimeout();
throw new nodesErrors.ErrorNodeConnectionTimeout(e.message, { cause: e });
}
throw e;
}
Expand Down
2 changes: 1 addition & 1 deletion src/notifications/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function verifyAndDecodeNotif(notifJWT: string): Promise<Notification> {
throw err;
} else {
// Error came from jose
throw new notificationsErrors.ErrorNotificationsParse();
throw new notificationsErrors.ErrorNotificationsParse(err.message, { cause: err });
}
}
}
Expand Down
Loading

0 comments on commit 6882759

Please sign in to comment.