Skip to content

Commit

Permalink
chore: dealing with an edge case when error cannot be serialised
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
aryanjassal committed Nov 7, 2024
1 parent f03712c commit 25d5fea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
43 changes: 14 additions & 29 deletions src/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ function fromError(error: any) {
case 'symbol':
case 'bigint':
case 'function':
console.log('bad error');
throw TypeError(`${error} cannot be serialized`);
}

Expand All @@ -453,14 +452,6 @@ function fromError(error: any) {
const timestamp: string = ((error as any).timestamp ?? new Date()).toJSON();
if (error instanceof ErrorPolykey) {
return error.toJSON();
// } else if (error instanceof AbstractError) {
// const wrappedError = new errors.ErrorPolykeyUnexpected(
// `Encountered an unexpected error: ${error.name}`,
// {
// cause: error,
// },
// );
// return wrappedError.toJSON();
} else if (error instanceof AggregateError) {
// AggregateError has an `errors` property
return {
Expand All @@ -479,31 +470,25 @@ function fromError(error: any) {
// serialising only the error type, message and its stack.
const wrappedError = new errors.ErrorPolykeyUnexpected(
`Unexpected error occurred: ${error.name}`,
{
cause: error,
},
{ cause: error },
);
return wrappedError.toJSON();
// return {
// type: error.name,
// message: error.message,
// data: {
// stack: error.stack,
// timestamp,
// cause,
// },
// };
}

// If an error is not serialisable (for example, throwing Object.create(null))
// and then trying to serialise it as an error would throw an error.
let message = '';
try {
message = `Unexpected error occured: ${error}`;
} catch (e) {
message = 'Unexpected error occured';
}

// If the error was not an Error, then wrap it inside ErrorPolykeyUnexpected
// and return that.
console.log('wrapping');
const wrappedError = new errors.ErrorPolykeyUnexpected(
`Unexpected error occured: ${error}`,
{
data: { reason: error },
},
);
console.log(wrappedError.toJSON());
const wrappedError = new errors.ErrorPolykeyUnexpected(message, {
cause: error,
});
return wrappedError.toJSON();
}

Expand Down
4 changes: 3 additions & 1 deletion src/vaults/VaultOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ async function writeSecret(
content: Buffer | string,
logger?: Logger,
): Promise<void> {
// throw new Error()
console.log(Object.create(null))
throw Object.create(null);
throw new Error('oh no')
throw 123;
await vault.writeF(async (efs) => {
await efs.writeFile(secretName, content);
Expand Down

0 comments on commit 25d5fea

Please sign in to comment.