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

Improve service errors adding more context #521

Open
josecelano opened this issue Mar 6, 2024 · 0 comments
Open

Improve service errors adding more context #521

josecelano opened this issue Mar 6, 2024 · 0 comments
Labels
- User - Enjoyable to Use our Software Enhancement / Feature Request Something New
Milestone

Comments

@josecelano
Copy link
Member

josecelano commented Mar 6, 2024

In the ServiceError enum we only support a single error message with no extra data.

#[derive(Debug, Display, PartialEq, Eq, Error)]
#[allow(dead_code)]
pub enum ServiceError {
    #[display(fmt = "internal server error")]
    InternalServerError,

    #[display(fmt = "This server is is closed for registration. Contact admin if this is unexpected")]
    ClosedForRegistration,

    #[display(fmt = "Email is required")] //405j
    EmailMissing,
    #[display(fmt = "Please enter a valid email address")] //405j
    EmailInvalid,

    #[display(fmt = "The value you entered for URL is not a URL")] //405j
    NotAUrl,

    #[display(fmt = "Invalid username/email or password")]
    WrongPasswordOrUsername,
    #[display(fmt = "Username not found")]
    UsernameNotFound,
    #[display(fmt = "User not found")]
    UserNotFound,

    #[display(fmt = "Account not found")]
    AccountNotFound,

    /// when the value passed contains profanity
    #[display(fmt = "Can't allow profanity in usernames")]
    ProfanityError,
    /// when the value passed contains blacklisted words
    /// see [blacklist](https://github.com/shuttlecraft/The-Big-Username-Blacklist)
    #[display(fmt = "Username contains blacklisted words")]
    BlacklistError,
    /// when the value passed contains characters not present
    /// in [UsernameCaseMapped](https://tools.ietf.org/html/rfc8265#page-7)
    /// profile
    #[display(fmt = "username_case_mapped violation")]
    UsernameCaseMappedError,

    #[display(fmt = "Password too short")]
    PasswordTooShort,
    #[display(fmt = "Username too long")]
    PasswordTooLong,
    #[display(fmt = "Passwords don't match")]
    PasswordsDontMatch,

    /// when the a username is already taken
    #[display(fmt = "Username not available")]
    UsernameTaken,

    #[display(fmt = "Invalid username. Usernames must consist of 1-20 alphanumeric characters, dashes, or underscore")]
    UsernameInvalid,

    /// email is already taken
    #[display(fmt = "Email not available")]
    EmailTaken,

    #[display(fmt = "Please verify your email before logging in")]
    EmailNotVerified,

    /// when the a token name is already taken
    /// token not found
    #[display(fmt = "Token not found. Please sign in.")]
    TokenNotFound,

    /// token expired
    #[display(fmt = "Token expired. Please sign in again.")]
    TokenExpired,

    #[display(fmt = "Token invalid.")]
    /// token invalid
    TokenInvalid,

    #[display(fmt = "Uploaded torrent is not valid.")]
    InvalidTorrentFile,

    // ...

    #[display(fmt = "This torrent already exists in our database.")]
    InfoHashAlreadyExists,

    #[display(fmt = "A torrent with the same canonical infohash already exists in our database.")]
    CanonicalInfoHashAlreadyExists,

    // ...

    #[display(fmt = "Tracker unknown response. Unexpected response from tracker. For example, if it can be parsed.")]
    TrackerUnknownResponse,

    #[display(fmt = "Torrent not found in tracker.")]
    TorrentNotFoundInTracker,

    #[display(fmt = "Invalid tracker API token.")]
    InvalidTrackerToken,
    // End tracker errors
}

For some of them, it would be better if we add extra data. FOr example, the error:

    #[display(fmt = "A torrent with the same canonical infohash already exists in our database.")]
    CanonicalInfoHashAlreadyExists,

should give you the info-hash, so the user can search/navigate to see the pre-existing torrent.

We can do it like we do for other error enums with the thiserror crate.

use thiserror::Error;

#[derive(Debug, Error)]
pub enum ServiceError {
    #[error("internal server error: {0}")]
    InternalServerError(String),
    // other variants...
}
@josecelano josecelano added Enhancement / Feature Request Something New - User - Enjoyable to Use our Software labels Mar 6, 2024
@josecelano josecelano added this to the v3.1.0 milestone Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- User - Enjoyable to Use our Software Enhancement / Feature Request Something New
Projects
None yet
Development

No branches or pull requests

1 participant