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

[PM-13660] implement catchable non generic error types #31

Merged
merged 54 commits into from
Nov 19, 2024

Conversation

coroiu
Copy link
Contributor

@coroiu coroiu commented Nov 12, 2024

🎟️ Tracking

📔 Objective

  • Makes it easier to return errors from rust.
  • Makes it easier to catch and handle errors in JS.

Every error outputs a function is<type_name>() that can be used from TS to check and cast errors.

Example:

#[bitwarden_error(flat)]
#[derive(Debug, Error)]
pub enum EncryptionSettingsError {
    #[error("Cryptography error, {0}")]
    Crypto(#[from] bitwarden_crypto::CryptoError),

    #[error(transparent)]
    InvalidBase64(#[from] base64::DecodeError),

    #[error(transparent)]
    VaultLocked(#[from] VaultLocked),

    #[error("Invalid private key")]
    InvalidPrivateKey,

    #[error("Missing private key")]
    MissingPrivateKey,
}
export interface EncryptionSettingsError extends Error {
  name: "EncryptionSettingsError";
  variant: "Crypto" | "InvalidBase64" | "VaultLocked" | "InvalidPrivateKey" | "MissingPrivateKey";
}

export function isEncryptionSettingsError(error: any): error is EncryptionSettingsError;

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation
    team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed
    issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

coroiu added 30 commits November 5, 2024 14:08
this won't fully work because the TS types will be appended into crates that won't necessarily have WASM enabled
[bitwarden_error(flat)] on struct was doing basically the same thing as #[bitwarden_error(flat)] so I'm removing it
Copy link
Contributor

github-actions bot commented Nov 12, 2024

Logo
Checkmarx One – Scan Summary & Details7c6a7caf-076f-45c1-bd54-5e406bc6385c

No New Or Fixed Issues Found

Copy link

codecov bot commented Nov 12, 2024

Codecov Report

Attention: Patch coverage is 87.20000% with 32 lines in your changes missing coverage. Please review.

Project coverage is 63.39%. Comparing base (1b10d83) to head (6347bae).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/bitwarden-wasm-internal/src/crypto.rs 0.00% 10 Missing ⚠️
crates/bitwarden-wasm-internal/src/client.rs 0.00% 6 Missing ⚠️
crates/bitwarden-error-macro/src/attribute.rs 89.18% 4 Missing ⚠️
crates/bitwarden-error-macro/src/flat/attribute.rs 97.05% 3 Missing ⚠️
crates/bitwarden-error-macro/src/full/attribute.rs 88.00% 3 Missing ⚠️
crates/bitwarden-error-macro/src/args.rs 0.00% 2 Missing ⚠️
...rates/bitwarden-wasm-internal/src/vault/folders.rs 0.00% 2 Missing ⚠️
...s/bitwarden-core/src/client/encryption_settings.rs 0.00% 1 Missing ⚠️
crates/bitwarden-core/src/error.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #31      +/-   ##
==========================================
+ Coverage   62.87%   63.39%   +0.51%     
==========================================
  Files         179      184       +5     
  Lines       12699    12935     +236     
==========================================
+ Hits         7985     8200     +215     
- Misses       4714     4735      +21     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

crates/bitwarden-error/tests/flat.rs Fixed Show fixed Hide fixed
crates/bitwarden-error/tests/flat.rs Fixed Show fixed Hide fixed
crates/bitwarden-error/tests/flat.rs Fixed Show fixed Hide fixed
crates/bitwarden-error/tests/flat.rs Fixed Show fixed Hide fixed
@coroiu coroiu marked this pull request as ready for review November 13, 2024 13:32
@coroiu coroiu requested review from dani-garcia and Hinton November 13, 2024 13:34
Copy link
Member

@dani-garcia dani-garcia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, looks pretty good to me, some initial comments on the macro implementation.

Comment on lines 15 to 16
let variant_ident = &variant.ident;
let variant_name = format!("{}", variant_ident);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bit of repetition with these lines for all the branches, can they be moved outside the match?

Also small subjective choice, but variant_ident.to_string() is the same as format!("{}", variant_ident), but I think it looks nicer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'll fix both!

#wasm

#[automatically_derived]
impl FlatError for #type_identifier {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When referring to types in macro generated code, we want to always use the fully qualified names, otherwise we're requiring users to import the correct type. If you're importingprelude::* that's not an issue, but nothing keeps the user from directly importing just the macro.

Suggested change
impl FlatError for #type_identifier {
impl ::bitwarden_error::prelude::FlatError for #type_identifier {

Note that we also use :: in front of the crate name, this is to avoid collisions with user defined modules using the same name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this for types that we depend on (e.g. wasm-bindgen) but never considered our own types, good catch!

crates/bitwarden-error-macro/src/args.rs Show resolved Hide resolved
crates/bitwarden-error-macro/src/lib.rs Show resolved Hide resolved
@coroiu coroiu requested a review from dani-garcia November 15, 2024 09:57
@coroiu coroiu merged commit 3cdf320 into main Nov 19, 2024
39 checks passed
@coroiu coroiu deleted the PM-13660-implement-catchable-non-generic-error-types branch November 19, 2024 13:02
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

Successfully merging this pull request may close these issues.

2 participants