Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
chore: make errors lowercase (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Mar 15, 2024
1 parent 04f7685 commit cc9b388
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
- push

env:
CURRENT_WORKING_ENGINE_COMMIT: cbdd1e1b364b1906347b3ef541a4a5057651650f
CURRENT_WORKING_ENGINE_COMMIT: 1971d6db5c5ffd065fec24764c122b4ab74e3172

jobs:
build:
Expand Down
33 changes: 27 additions & 6 deletions modules/auth/module.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions modules/auth/scripts/auth_email_passwordless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function run(
): Promise<Response> {
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;
Expand All @@ -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");
}
}

Expand Down
12 changes: 6 additions & 6 deletions modules/auth/scripts/verify_email_passwordless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
29 changes: 21 additions & 8 deletions modules/currency/module.yaml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions modules/currency/scripts/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ 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) => {
const balance = await getBalance(tx, req.userId);

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 {
Expand Down
4 changes: 2 additions & 2 deletions modules/currency/scripts/set_balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down
6 changes: 3 additions & 3 deletions modules/currency/scripts/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ 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) => {
const balance = await getBalance(tx, req.userId);

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 {
Expand Down
2 changes: 1 addition & 1 deletion modules/currency/utils/set_balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
18 changes: 15 additions & 3 deletions modules/email/module.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions modules/email/scripts/send_email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export async function run(
req: Request,
): Promise<Response> {
if (!req.html && !req.text) {
throw new RuntimeError("EMAIL_MISSING_CONTENT");
throw new RuntimeError("email_missing_content");
}

if ("test" in ctx.userConfig.provider) {
// Do nothing
} else if ("sendGrid" in ctx.userConfig.provider) {
await useSendGrid(ctx.userConfig.provider.sendGrid, req);
} else {
throw new RuntimeError("UNREACHABLE");
throw new RuntimeError("unreachable");
}

return {};
Expand Down
43 changes: 35 additions & 8 deletions modules/friends/module.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion modules/friends/scripts/send_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions modules/rate_limit/module.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit cc9b388

Please sign in to comment.