Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve keyring endowment error messaging (#884)
* Improve keyring endowment error messaging * Fix coverage * Use assertIsNamespacesObject and make assertStruct more generic * Fix small things * Fix coverage
- Loading branch information
Showing
8 changed files
with
133 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,50 @@ | ||
import { AssertionError } from '@metamask/utils'; | ||
import { Struct, assert as assertSuperstruct } from 'superstruct'; | ||
|
||
export type AssertionErrorConstructor = | ||
| (new (args: { message: string }) => Error) | ||
| ((args: { message: string }) => Error); | ||
|
||
/** | ||
* Check if a value is a constructor. | ||
* | ||
* @param fn - The value to check. | ||
* @returns `true` if the value is a constructor, or `false` otherwise. | ||
*/ | ||
function isConstructable( | ||
fn: AssertionErrorConstructor, | ||
): fn is new (args: { message: string }) => Error { | ||
return Boolean(typeof fn?.prototype?.constructor?.name === 'string'); | ||
} | ||
|
||
/** | ||
* Assert a value against a Superstruct struct. | ||
* | ||
* @param value - The value to validate. | ||
* @param struct - The struct to validate against. | ||
* @param errorPrefix - A prefix to add to the error message. Defaults to | ||
* "Assertion failed". | ||
* @param ErrorWrapper - The error class to throw if the assertion fails. | ||
* Defaults to {@link AssertionError}. | ||
* @throws If the value is not valid. | ||
*/ | ||
export function assertStruct<T, S>( | ||
value: unknown, | ||
struct: Struct<T, S>, | ||
errorPrefix = 'Assertion failed', | ||
ErrorWrapper: AssertionErrorConstructor = AssertionError, | ||
): asserts value is T { | ||
try { | ||
assertSuperstruct(value, struct); | ||
} catch (error) { | ||
throw new AssertionError({ | ||
message: `${errorPrefix}: ${error.message}`, | ||
}); | ||
if (isConstructable(ErrorWrapper)) { | ||
throw new ErrorWrapper({ | ||
message: `${errorPrefix}: ${error.message}.`, | ||
}); | ||
} else { | ||
throw ErrorWrapper({ | ||
message: `${errorPrefix}: ${error.message}.`, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters