From cc9b3880acb3102e37dbde8154c7a44000232e1d Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Fri, 15 Mar 2024 12:30:39 +0000 Subject: [PATCH] chore: make errors lowercase (#64) --- .github/workflows/test-all.yml | 2 +- modules/auth/module.yaml | 33 +++++++++++--- .../auth/scripts/auth_email_passwordless.ts | 4 +- .../auth/scripts/verify_email_passwordless.ts | 12 +++--- modules/currency/module.yaml | 29 +++++++++---- modules/currency/scripts/deposit.ts | 6 +-- modules/currency/scripts/set_balance.ts | 4 +- modules/currency/scripts/withdraw.ts | 6 +-- modules/currency/utils/set_balance.ts | 2 +- modules/email/module.yaml | 18 ++++++-- modules/email/scripts/send_email.ts | 4 +- modules/friends/module.yaml | 43 +++++++++++++++---- modules/friends/scripts/send_request.ts | 2 +- modules/rate_limit/module.yaml | 20 +++++++-- modules/tokens/module.yaml | 37 ++++++++++++---- modules/tokens/scripts/validate.ts | 6 +-- modules/users/module.yaml | 25 ++++++++--- modules/users/scripts/validate_user_token.ts | 2 +- 18 files changed, 187 insertions(+), 68 deletions(-) diff --git a/.github/workflows/test-all.yml b/.github/workflows/test-all.yml index 5e71c142..428a0e54 100644 --- a/.github/workflows/test-all.yml +++ b/.github/workflows/test-all.yml @@ -3,7 +3,7 @@ on: - push env: - CURRENT_WORKING_ENGINE_COMMIT: cbdd1e1b364b1906347b3ef541a4a5057651650f + CURRENT_WORKING_ENGINE_COMMIT: 1971d6db5c5ffd065fec24764c122b4ab74e3172 jobs: build: diff --git a/modules/auth/module.yaml b/modules/auth/module.yaml index fdd82826..b5d0bd32 100644 --- a/modules/auth/module.yaml +++ b/modules/auth/module.yaml @@ -1,16 +1,37 @@ +name: Authentication +description: Authenticate users with multiple authentication methods. +icon: key +tags: + - core + - auth + - user +authors: + - rivet-gg + - NathanFlurry +status: stable dependencies: email: {} users: {} rate_limit: {} scripts: auth_email_passwordless: + name: Authenticate Email Passwordless + description: Send a one-time verification code to a user's email address to authenticate them. public: true verify_email_passwordless: + name: Verify Email Passwordless + description: Verify a user's email address with a one-time verification code. public: true errors: - PROVIDER_DISABLED: {} - VERIFICATION_CODE_INVALID: {} - VERIFICATION_CODE_ATTEMPT_LIMIT: {} - VERIFICATION_CODE_EXPIRED: {} - VERIFICATION_CODE_ALREADY_USED: {} - EMAIL_ALREADY_USED: {} + provider_disabled: + name: Provider Disabled + verification_code_invalid: + name: Verification Code Invalid + verification_code_attempt_limit: + name: Verification Code Attempt Limit + verification_code_expired: + name: Verification Code Expired + verification_code_already_used: + name: Verification Code Already Used + email_already_used: + name: Email Already Used diff --git a/modules/auth/scripts/auth_email_passwordless.ts b/modules/auth/scripts/auth_email_passwordless.ts index 820e9702..f430fad6 100644 --- a/modules/auth/scripts/auth_email_passwordless.ts +++ b/modules/auth/scripts/auth_email_passwordless.ts @@ -17,7 +17,7 @@ export async function run( ): Promise { await ctx.modules.rateLimit.throttlePublic({}); - if (!ctx.userConfig.email) throw new RuntimeError("PROVIDER_DISABLED"); + if (!ctx.userConfig.email) throw new RuntimeError("provider_disabled"); // Fetch existing user if session token is provided let userId: string | undefined; @@ -31,7 +31,7 @@ export async function run( where: { email: req.email }, }); if (existingIdentity && existingIdentity.userId !== userId) { - throw new RuntimeError("EMAIL_ALREADY_USED"); + throw new RuntimeError("email_already_used"); } } diff --git a/modules/auth/scripts/verify_email_passwordless.ts b/modules/auth/scripts/verify_email_passwordless.ts index 0757d6ea..5719eab2 100644 --- a/modules/auth/scripts/verify_email_passwordless.ts +++ b/modules/auth/scripts/verify_email_passwordless.ts @@ -45,20 +45,20 @@ export async function run( }, }); if (!verification) { - throw new RuntimeError("VERIFICATION_CODE_INVALID"); + throw new RuntimeError("verification_code_invalid"); } if (verification.attemptCount >= verification.maxAttemptCount) { - throw new RuntimeError("VERIFICATION_CODE_ATTEMPT_LIMIT"); + throw new RuntimeError("verification_code_attempt_limit"); } if (verification.completedAt !== null) { - throw new RuntimeError("VERIFICATION_CODE_ALREADY_USED"); + throw new RuntimeError("verification_code_already_used"); } if (verification.code !== code) { // Same error as above to prevent exploitation - throw new RuntimeError("VERIFICATION_CODE_INVALID"); + throw new RuntimeError("verification_code_invalid"); } if (verification.expireAt < new Date()) { - throw new RuntimeError("VERIFICATION_CODE_EXPIRED"); + throw new RuntimeError("verification_code_expired"); } // Mark as used @@ -73,7 +73,7 @@ export async function run( }, }); if (verificationConfirmation === null) { - throw new RuntimeError("VERIFICATION_CODE_ALREADY_USED"); + throw new RuntimeError("verification_code_already_used"); } // Get or create user diff --git a/modules/currency/module.yaml b/modules/currency/module.yaml index 363dc5d4..7caa2169 100644 --- a/modules/currency/module.yaml +++ b/modules/currency/module.yaml @@ -1,19 +1,32 @@ -status: preview +name: Currency +description: Track user balances and allow them to deposit and withdraw. +icon: coin +tags: + - economy authors: - ABCxFF +status: preview dependencies: rate_limit: {} users: {} scripts: # Maybe deposit? - deposit: {} + deposit: + name: Deposit # Maybe withdraw? - withdraw: {} - get_balance: {} - set_balance: {} + withdraw: + name: Withdraw + get_balance: + name: Get Balance + set_balance: + name: Set Balance get_balance_by_token: + name: Get Balance by Token public: true errors: - INVALID_USER: {} - INVALID_AMOUNT: {} - NOT_ENOUGH_FUNDS: {} + invalid_user: + name: Invalid User + invalid_amount: + name: Invalid Amount + not_enough_funds: + name: Not Enough Funds diff --git a/modules/currency/scripts/deposit.ts b/modules/currency/scripts/deposit.ts index 78f265e3..dfa6fa25 100644 --- a/modules/currency/scripts/deposit.ts +++ b/modules/currency/scripts/deposit.ts @@ -19,7 +19,7 @@ export async function run( await ctx.modules.rateLimit.throttlePublic({ requests: 25 }); if (req.amount < 0 || !Number.isFinite(req.amount)) { - throw new RuntimeError("INVALID_AMOUNT"); + throw new RuntimeError("invalid_amount"); } return ctx.db.$transaction(async (tx) => { @@ -27,12 +27,12 @@ export async function run( const updatedBalance = balance + req.amount; - if (updatedBalance < 0) throw new RuntimeError("NOT_ENOUGH_FUNDS"); + if (updatedBalance < 0) throw new RuntimeError("not_enough_funds"); try { await setBalance(tx, req.userId, updatedBalance); } catch { - throw new RuntimeError("INVALID_AMOUNT"); + throw new RuntimeError("invalid_amount"); } return { diff --git a/modules/currency/scripts/set_balance.ts b/modules/currency/scripts/set_balance.ts index f2129578..6584bab8 100644 --- a/modules/currency/scripts/set_balance.ts +++ b/modules/currency/scripts/set_balance.ts @@ -17,13 +17,13 @@ export async function run( await ctx.modules.rateLimit.throttlePublic({ requests: 25 }); if (req.balance < 0 || !Number.isFinite(req.balance)) { - throw new RuntimeError("INVALID_AMOUNT"); + throw new RuntimeError("invalid_amount"); } try { await setBalance(ctx.db, req.userId, req.balance); } catch { - throw new RuntimeError("INVALID_AMOUNT"); + throw new RuntimeError("invalid_amount"); } return {}; diff --git a/modules/currency/scripts/withdraw.ts b/modules/currency/scripts/withdraw.ts index 87b76bc2..cf9bc01a 100644 --- a/modules/currency/scripts/withdraw.ts +++ b/modules/currency/scripts/withdraw.ts @@ -19,7 +19,7 @@ export async function run( await ctx.modules.rateLimit.throttlePublic({ requests: 25 }); if (req.amount < 0 || !Number.isFinite(req.amount)) { - throw new RuntimeError("INVALID_AMOUNT"); + throw new RuntimeError("invalid_amount"); } return ctx.db.$transaction(async (tx) => { @@ -27,12 +27,12 @@ export async function run( const updatedBalance = balance - req.amount; - if (updatedBalance < 0) throw new RuntimeError("NOT_ENOUGH_FUNDS"); + if (updatedBalance < 0) throw new RuntimeError("not_enough_funds"); try { await setBalance(tx, req.userId, updatedBalance); } catch { - throw new RuntimeError("INVALID_AMOUNT"); + throw new RuntimeError("invalid_amount"); } return { diff --git a/modules/currency/utils/set_balance.ts b/modules/currency/utils/set_balance.ts index 6d9db948..694858cf 100644 --- a/modules/currency/utils/set_balance.ts +++ b/modules/currency/utils/set_balance.ts @@ -10,7 +10,7 @@ export const setBalance = async ( userId: string, balance: number, ) => { - if (balance < 0) throw new RuntimeError("INVALID_AMOUNT"); + if (balance < 0) throw new RuntimeError("invalid_amount"); await db.userWallet.upsert({ where: { diff --git a/modules/email/module.yaml b/modules/email/module.yaml index 28124614..5cec1a5e 100644 --- a/modules/email/module.yaml +++ b/modules/email/module.yaml @@ -1,6 +1,18 @@ +name: Email +description: Send emails using multiple providers. +icon: envelope +tags: + - email +authors: + - rivet-gg + - NathanFlurry +status: stable scripts: - send_email: {} + send_email: + name: Send Email errors: - EMAIL_MISSING_CONTENT: + email_missing_content: + name: Email Missing Content description: Email must have `html` and/or `text` - SENDGRID_ERROR: {} + sendgrid_error: + name: SendGrid Error diff --git a/modules/email/scripts/send_email.ts b/modules/email/scripts/send_email.ts index fbd4c5cd..29715c79 100644 --- a/modules/email/scripts/send_email.ts +++ b/modules/email/scripts/send_email.ts @@ -22,7 +22,7 @@ export async function run( req: Request, ): Promise { if (!req.html && !req.text) { - throw new RuntimeError("EMAIL_MISSING_CONTENT"); + throw new RuntimeError("email_missing_content"); } if ("test" in ctx.userConfig.provider) { @@ -30,7 +30,7 @@ export async function run( } else if ("sendGrid" in ctx.userConfig.provider) { await useSendGrid(ctx.userConfig.provider.sendGrid, req); } else { - throw new RuntimeError("UNREACHABLE"); + throw new RuntimeError("unreachable"); } return {}; diff --git a/modules/friends/module.yaml b/modules/friends/module.yaml index 75f47c08..38d2d812 100644 --- a/modules/friends/module.yaml +++ b/modules/friends/module.yaml @@ -1,29 +1,56 @@ -status: preview +name: Friends +description: Allow users to send and accept friend requests. +icon: user-group +tags: + - social authors: + - rivet-gg - NathanFlurry +status: beta dependencies: rate_limit: {} users: {} scripts: send_request: + name: Send Request + description: Send a friend request to another user. public: true accept_request: + name: Accept Request + description: Accept a friend request from another user. public: true decline_request: + name: Decline Request + description: Decline a friend request from another user. public: true remove_friend: + name: Remove Friend + description: Remove a friend from your friends list. public: true list_friends: + name: List Friends + description: List all friends of a user. public: true list_outgoing_friend_requests: + name: List Outgoing Friend Requests + description: List all friend requests sent by a user. public: true list_incoming_friend_requests: + name: List Incoming Friend Requests + description: List all friend requests received by a user. public: true errors: - ALREADY_FRIENDS: {} - FRIEND_REQUEST_NOT_FOUND: {} - FRIEND_REQUEST_ALREADY_EXISTS: {} - NOT_FRIEND_REQUEST_RECIPIENT: {} - FRIEND_REQUEST_ALREADY_ACCEPTED: {} - FRIEND_REQUEST_ALREADY_DECLINED: {} - CANNOT_SEND_TO_SELF: {} + already_friends: + name: Already Friends + friend_request_not_found: + name: Friend Request Not Found + friend_request_already_exists: + name: Friend Request Already Exists + not_friend_request_recipient: + name: Not Friend Request Recipient + friend_request_already_accepted: + name: Friend Request Already Accepted + friend_request_already_declined: + name: Friend Request Already Declined + cannot_send_to_self: + name: Cannot Send to Self diff --git a/modules/friends/scripts/send_request.ts b/modules/friends/scripts/send_request.ts index 5bf24e16..0ec3688b 100644 --- a/modules/friends/scripts/send_request.ts +++ b/modules/friends/scripts/send_request.ts @@ -21,7 +21,7 @@ export async function run( }); if (userId === req.targetUserId) { - throw new RuntimeError("CANNOT_SEND_TO_SELF"); + throw new RuntimeError("cannot_send_to_self"); } // Sort the user IDs to ensure consistency diff --git a/modules/rate_limit/module.yaml b/modules/rate_limit/module.yaml index 9e39d183..2e54df5e 100644 --- a/modules/rate_limit/module.yaml +++ b/modules/rate_limit/module.yaml @@ -1,8 +1,20 @@ -status: preview +name: Rate Limit +description: Prevent abuse by limiting request rate. +icon: gauge-circle-minus +tags: + - core + - utility authors: + - rivet-gg - NathanFlurry +status: stable scripts: - throttle: {} - throttle_public: {} + throttle: + name: Throttle + description: Limit the amount of times an request can be made by a given key. + throttle_public: + name: Throttle Public + description: Limit the amount of times a public request can be made by a given key. This will rate limit based off the user's IP address. errors: - RATE_LIMIT_EXCEEDED: {} + rate_limit_exceeded: + name: Rate Limit Exceeded diff --git a/modules/tokens/module.yaml b/modules/tokens/module.yaml index 2f8411cd..5527f692 100644 --- a/modules/tokens/module.yaml +++ b/modules/tokens/module.yaml @@ -1,13 +1,32 @@ -status: preview +name: Tokens +description: Create & verify tokens for authorization purposes. +icon: lock +tags: + - core + - utility authors: + - rivet-gg - NathanFlurry +status: stable scripts: - create: {} - get: {} - get_by_token: {} - revoke: {} - validate: {} + create: + name: Create Token + get: + name: Get Token + description: Get a token by its ID. + get_by_token: + name: Get Token by Token + description: Get a token by its secret token. + revoke: + name: Revoke Token + description: Revoke a token, preventing it from being used again. + validate: + name: Validate Token + description: Validate a token. Throws an error if the token is invalid. errors: - TOKEN_NOT_FOUND: {} - TOKEN_REVOKED: {} - TOKEN_EXPIRED: {} + token_not_found: + name: Token Not Found + token_revoked: + name: Token Revoked + token_expired: + name: Token Expired diff --git a/modules/tokens/scripts/validate.ts b/modules/tokens/scripts/validate.ts index 4da9ca91..2171eb5a 100644 --- a/modules/tokens/scripts/validate.ts +++ b/modules/tokens/scripts/validate.ts @@ -18,15 +18,15 @@ export async function run( }); const token = tokens[0]; - if (!token) throw new RuntimeError("TOKEN_NOT_FOUND"); + if (!token) throw new RuntimeError("token_not_found"); - if (token.revokedAt) throw new RuntimeError("TOKEN_REVOKED"); + if (token.revokedAt) throw new RuntimeError("token_revoked"); if (token.expireAt) { const expireAt = new Date(token.expireAt); const now = new Date(); if (expireAt < now) { - throw new RuntimeError("TOKEN_EXPIRED"); + throw new RuntimeError("token_expired"); } } diff --git a/modules/users/module.yaml b/modules/users/module.yaml index e051c4fd..fc0f4c5f 100644 --- a/modules/users/module.yaml +++ b/modules/users/module.yaml @@ -1,16 +1,31 @@ -status: preview +name: Users +description: Identify and manage users. +icon: user +tags: + - core + - social authors: + - rivet-gg - NathanFlurry +status: stable dependencies: rate_limit: {} tokens: {} scripts: get_user: + name: Get User public: true - create_user: {} + create_user: + name: Create User validate_user_token: + name: Validate User Token + description: Validate a user token. Throws an error if the token is invalid. public: true - create_user_token: {} + create_user_token: + name: Create User Token + description: Create a token for a user to authenticate future requests. errors: - TOKEN_NOT_USER_TOKEN: {} - UNKNOWN_IDENTITY_TYPE: {} + token_not_user_token: + name: Token Not User Token + unknown_identity_type: + name: Unknown Identity Type diff --git a/modules/users/scripts/validate_user_token.ts b/modules/users/scripts/validate_user_token.ts index 29e0c6dc..b2309937 100644 --- a/modules/users/scripts/validate_user_token.ts +++ b/modules/users/scripts/validate_user_token.ts @@ -18,7 +18,7 @@ export async function run( const { token } = await ctx.modules.tokens.validate({ token: req.userToken, }); - if (token.type !== "user") throw new RuntimeError("TOKEN_NOT_USER_TOKEN"); + if (token.type !== "user") throw new RuntimeError("token_not_user_token"); const userId = token.meta.userId; return { userId };