Browse Source

Remove unnecessary `throw` commands

master
buttercat1791 8 months ago
parent
commit
01dc9233ed
  1. 16
      src/lib/utils/websocket_utils.ts
  2. 10
      src/routes/publication/+page.server.ts
  3. 2
      src/routes/publication/[type]/[identifier]/+layout.server.ts
  4. 4
      src/routes/publication/[type]/[identifier]/+layout.ts
  5. 2
      src/routes/publication/[type]/[identifier]/+page.server.ts
  6. 6
      src/routes/publication/[type]/[identifier]/+page.ts

16
src/lib/utils/websocket_utils.ts

@ -70,14 +70,14 @@ export async function fetchEventById(id: string): Promise<NostrEvent> {
try { try {
const event = await fetchNostrEvent({ ids: [id], limit: 1 }); const event = await fetchNostrEvent({ ids: [id], limit: 1 });
if (!event) { if (!event) {
throw error(404, `Event not found for ID: ${id}`); error(404, `Event not found for ID: ${id}`);
} }
return event; return event;
} catch (err) { } catch (err) {
if (err && typeof err === "object" && "status" in err) { if (err && typeof err === "object" && "status" in err) {
throw err; throw err;
} }
throw error(404, `Failed to fetch event by ID: ${err}`); error(404, `Failed to fetch event by ID: ${err}`);
} }
} }
@ -88,14 +88,14 @@ export async function fetchEventByDTag(dTag: string): Promise<NostrEvent> {
try { try {
const event = await fetchNostrEvent({ "#d": [dTag], limit: 1 }); const event = await fetchNostrEvent({ "#d": [dTag], limit: 1 });
if (!event) { if (!event) {
throw error(404, `Event not found for d-tag: ${dTag}`); error(404, `Event not found for d-tag: ${dTag}`);
} }
return event; return event;
} catch (err) { } catch (err) {
if (err && typeof err === "object" && "status" in err) { if (err && typeof err === "object" && "status" in err) {
throw err; throw err;
} }
throw error(404, `Failed to fetch event by d-tag: ${err}`); error(404, `Failed to fetch event by d-tag: ${err}`);
} }
} }
@ -112,14 +112,14 @@ export async function fetchEventByNaddr(naddr: string): Promise<NostrEvent> {
}; };
const event = await fetchNostrEvent(filter); const event = await fetchNostrEvent(filter);
if (!event) { if (!event) {
throw error(404, `Event not found for naddr: ${naddr}`); error(404, `Event not found for naddr: ${naddr}`);
} }
return event; return event;
} catch (err) { } catch (err) {
if (err && typeof err === "object" && "status" in err) { if (err && typeof err === "object" && "status" in err) {
throw err; throw err;
} }
throw error(404, `Failed to fetch event by naddr: ${err}`); error(404, `Failed to fetch event by naddr: ${err}`);
} }
} }
@ -131,13 +131,13 @@ export async function fetchEventByNevent(nevent: string): Promise<NostrEvent> {
const decoded = neventDecode(nevent); const decoded = neventDecode(nevent);
const event = await fetchNostrEvent({ ids: [decoded.id], limit: 1 }); const event = await fetchNostrEvent({ ids: [decoded.id], limit: 1 });
if (!event) { if (!event) {
throw error(404, `Event not found for nevent: ${nevent}`); error(404, `Event not found for nevent: ${nevent}`);
} }
return event; return event;
} catch (err) { } catch (err) {
if (err && typeof err === "object" && "status" in err) { if (err && typeof err === "object" && "status" in err) {
throw err; throw err;
} }
throw error(404, `Failed to fetch event by nevent: ${err}`); error(404, `Failed to fetch event by nevent: ${err}`);
} }
} }

10
src/routes/publication/+page.server.ts

@ -25,17 +25,17 @@ export const load: PageServerLoad = ({ url }) => {
if (id) { if (id) {
// Check if id is an naddr or nevent // Check if id is an naddr or nevent
if (id.startsWith(IDENTIFIER_PREFIXES.NADDR)) { if (id.startsWith(IDENTIFIER_PREFIXES.NADDR)) {
throw redirect(301, `${ROUTES.NADDR}/${id}`); redirect(301, `${ROUTES.NADDR}/${id}`);
} else if (id.startsWith(IDENTIFIER_PREFIXES.NEVENT)) { } else if (id.startsWith(IDENTIFIER_PREFIXES.NEVENT)) {
throw redirect(301, `${ROUTES.NEVENT}/${id}`); redirect(301, `${ROUTES.NEVENT}/${id}`);
} else { } else {
// Assume it's a hex ID // Assume it's a hex ID
throw redirect(301, `${ROUTES.ID}/${id}`); redirect(301, `${ROUTES.ID}/${id}`);
} }
} else if (dTag) { } else if (dTag) {
throw redirect(301, `${ROUTES.D_TAG}/${dTag}`); redirect(301, `${ROUTES.D_TAG}/${dTag}`);
} }
// If no query parameters, redirect to the start page // If no query parameters, redirect to the start page
throw redirect(301, ROUTES.START); redirect(301, ROUTES.START);
}; };

2
src/routes/publication/[type]/[identifier]/+layout.server.ts

@ -7,7 +7,7 @@ export const load: LayoutServerLoad = async ({ params, url }) => {
// Validate the identifier type for SSR // Validate the identifier type for SSR
const validTypes = ['id', 'd', 'naddr', 'nevent']; const validTypes = ['id', 'd', 'naddr', 'nevent'];
if (!validTypes.includes(type)) { if (!validTypes.includes(type)) {
throw error(400, `Unsupported identifier type: ${type}`); error(400, `Unsupported identifier type: ${type}`);
} }
// Provide basic metadata for SSR - actual fetching will happen on client // Provide basic metadata for SSR - actual fetching will happen on client

4
src/routes/publication/[type]/[identifier]/+layout.ts

@ -39,7 +39,7 @@ export const load: LayoutLoad = async ({ params, url }) => {
indexEvent = await fetchEventByNevent(identifier); indexEvent = await fetchEventByNevent(identifier);
break; break;
default: default:
throw error(400, `Unsupported identifier type: ${type}`); error(400, `Unsupported identifier type: ${type}`);
} }
// Extract metadata for meta tags // Extract metadata for meta tags
@ -60,6 +60,6 @@ export const load: LayoutLoad = async ({ params, url }) => {
}; };
} catch (err) { } catch (err) {
console.error('Failed to fetch publication:', err); console.error('Failed to fetch publication:', err);
throw error(404, `Failed to load publication: ${err}`); error(404, `Failed to load publication: ${err}`);
} }
}; };

2
src/routes/publication/[type]/[identifier]/+page.server.ts

@ -7,7 +7,7 @@ export const load: PageServerLoad = async ({ params }) => {
// Validate the identifier type for SSR // Validate the identifier type for SSR
const validTypes = ['id', 'd', 'naddr', 'nevent']; const validTypes = ['id', 'd', 'naddr', 'nevent'];
if (!validTypes.includes(type)) { if (!validTypes.includes(type)) {
throw error(400, `Unsupported identifier type: ${type}`); error(400, `Unsupported identifier type: ${type}`);
} }
// Provide basic data for SSR - actual fetching will happen on client // Provide basic data for SSR - actual fetching will happen on client

6
src/routes/publication/[type]/[identifier]/+page.ts

@ -34,11 +34,11 @@ export const load: PageLoad = async ({ params }) => {
indexEvent = await fetchEventByNevent(identifier); indexEvent = await fetchEventByNevent(identifier);
break; break;
default: default:
throw error(400, `Unsupported identifier type: ${type}`); error(400, `Unsupported identifier type: ${type}`);
} }
if (!indexEvent) { if (!indexEvent) {
throw error(404, `Event not found for ${type}: ${identifier}`); error(404, `Event not found for ${type}: ${identifier}`);
} }
const publicationType = indexEvent.tags.find((tag) => tag[0] === "type")?.[1] ?? ""; const publicationType = indexEvent.tags.find((tag) => tag[0] === "type")?.[1] ?? "";
@ -49,6 +49,6 @@ export const load: PageLoad = async ({ params }) => {
}; };
} catch (err) { } catch (err) {
console.error('Failed to fetch publication:', err); console.error('Failed to fetch publication:', err);
throw error(404, `Failed to load publication: ${err}`); error(404, `Failed to load publication: ${err}`);
} }
}; };
Loading…
Cancel
Save