Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use promises to reduce errors #41

Closed
HPaulson opened this issue Jan 15, 2022 · 0 comments
Closed

Use promises to reduce errors #41

HPaulson opened this issue Jan 15, 2022 · 0 comments

Comments

@HPaulson
Copy link

In functions such as RSA.parseKey & RSA.importKey, it's likely a smarter idea to implement Promises so developers can catch errors more effectively. Currently, passing an invalid key into one of these functions returns a type error from the following function:

export function rsa_import_key(
  key: string | JSONWebKey,
  format: RSAImportKeyFormat,
): RSAKeyParams {
  const finalFormat = format === "auto" ? detect_format(key) : format;

  if (finalFormat === "jwk") return rsa_import_jwk(key as JSONWebKey);
  if (finalFormat === "pem") return rsa_import_pem(key as string);

  throw new TypeError("Unsupported key format");
}

This is bad practice, especially if the developer is passing on a user-provided public key. A better implementation would be to use a Promise and reject if the key format is invalid, which would then allow the end developer to handle such error and pass it off to the user with .catch().

@HPaulson HPaulson closed this as not planned Won't fix, can't repro, duplicate, stale Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant