Skip to content

Commit

Permalink
fix: don't use try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
elianiva committed Nov 24, 2024
1 parent 12724e8 commit 847e308
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
16 changes: 6 additions & 10 deletions routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ export const handler: Handlers = {
case "PUT": {
const originalUrl = form.get("original_url") as string;
const customUrl = form.get("custom_url") as string;
try {
const customised = await customiseShortenedUrl(
originalUrl,
customUrl,
);
return ctx.render({
url: customised,
isUpdated: true,
});
} catch {
const customised = await customiseShortenedUrl(originalUrl, customUrl);
if (!customised) {
return ctx.render({ error: "Customised url already exists" });
}
return ctx.render({
url: customised,
isUpdated: true,
});
}
default: {
const originalUrl = form.get("url") as string;
Expand Down
6 changes: 3 additions & 3 deletions services/shortener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export async function customiseShortenedUrl(original: string, customised: string
// trim customised url
const customisedKey = ["url", customised.trim()];

// check if customised already exists
if (await kv.get(customisedKey)) {
throw new Error("Customised url already exists");
const existingCustomised = await kv.get(customisedKey);
if (existingCustomised) {
return null;
}

await kv
Expand Down

0 comments on commit 847e308

Please sign in to comment.