misc: Improve error handling

This commit is contained in:
Xyphyn committed 2023-07-03 12:33:04 -07:00
1 parent 98b5e4b5c1
commit a5460a03b4
23 files changed
+285 -202

No files matched your search

+4 -2
View File
@@ -1,4 +1,4 @@
import { instance_url } from '$lib/lemmy.js'
import { DEFAULT_INSTANCE_URL } from '$lib/lemmy.js'
import { error } from '@sveltejs/kit'
export async function handle({ event, resolve }) {
@@ -14,16 +14,18 @@ export async function handle({ event, resolve }) {
event.request.headers.delete('host')
try {
console.log(`https://${instance}/${url.toString()}`)
const data = await fetch(`https://${instance}/${url.toString()}`, {
method: event.request.method,
body: event.request.body,
headers: event.request.headers,
signal: AbortSignal.timeout(20 * 1000),
})
return new Response(JSON.stringify(await data.json()))
} catch (err) {
console.error(err)
throw error(500, `The proxy failed to fetch from ${instance}.`)
throw error(500, { message: 'The proxy failed to fetch from the server' })
}
}
-19
View File
@@ -1,19 +0,0 @@
<script lang="ts">
import type { Community } from 'lemmy-js-client'
export let community: Community
function linkFromCommunity(community: Community) {
const local = community.local
if (local) {
return `/c/${community.name}`
} else {
const domain = new URL(community.actor_id).hostname
return `/c/${community.name}@${domain}`
}
}
</script>
<a href={linkFromCommunity(community)}>{community.title}</a>
+3 -4
View File
@@ -1,8 +1,7 @@
<script lang="ts">
import Link from '$lib/components/Link.svelte'
import Logo from '$lib/components/Logo.svelte'
export let instance_url: string | undefined
import { instance_url } from '$lib/lemmy.js'
</script>
<nav
@@ -14,13 +13,13 @@
<Logo width={40} />
<div class="flex flex-col">
<span class="font-bold">Xylemmy</span>
{#if instance_url}
{#if $instance_url}
<a
class="text-xs opacity-50 hover:opacity-100 hover:underline
transition-opacity"
href="/instance"
>
{new URL(`https://${instance_url}`).hostname}
{new URL(`https://${$instance_url}`).hostname}
</a>
{/if}
</div>
@@ -0,0 +1,32 @@
<script lang="ts">
import type { Community } from 'lemmy-js-client'
export let community: Community
export let avatar: boolean = false
function linkFromCommunity(community: Community) {
const local = community.local
if (local) {
return `/c/${community.name}`
} else {
const domain = new URL(community.actor_id).hostname
return `/c/${community.name}@${domain}`
}
}
</script>
<a class="items-center flex flex-row gap-2" href={linkFromCommunity(community)}>
{#if avatar}
<img
src={community.icon ??
`https://api.dicebear.com/6.x/initials/svg?seed=${community.name}`}
alt=""
width={24}
height={24}
class="rounded-full aspect-square object-cover"
/>
{/if}
{community.title}
</a>
+4 -1
View File
@@ -2,7 +2,10 @@ import { LemmyHttp, type LoginResponse } from 'lemmy-js-client'
import { writable } from 'svelte/store'
import { PUBLIC_PROXY_URL } from '$env/static/public'
export let instance_url = 'lemmy.world'
export const DEFAULT_INSTANCE_URL = 'lemmy.world'
export let instance_url = writable(DEFAULT_INSTANCE_URL)
export function getClient(instance: string = `lemmy.world`): LemmyHttp {
return new LemmyHttp(`${PUBLIC_PROXY_URL}/cors/${instance}`)
}
+9 -1
View File
@@ -1,8 +1,16 @@
<script lang="ts">
import { page } from '$app/stores'
import Link from '$lib/components/Link.svelte'
</script>
<div class="flex flex-col mx-auto gap-4 justify-center items-center">
<h1 class="font-black text-3xl">{$page.status}</h1>
<p>{JSON.stringify($page.error)}</p>
<p class="mx-4 md:mx-8">
Failed to fetch page. The lemmy servers are having quite a lot of issues
right now, this is likely on their end.
</p>
<code class="bg-zinc-800 px-2 py-1 rounded-md">
{JSON.stringify($page.error)}
</code>
<Link href={$page.url.toString()}>Retry</Link>
</div>
-7
View File
@@ -1,7 +0,0 @@
import { instance_url } from '$lib/lemmy.js'
export function load({ cookies, locals }) {
return {
instance_url: cookies.get('instance_url') || instance_url,
}
}
+1 -3
View File
@@ -19,11 +19,9 @@
nProgress.done()
}
}
export let data
</script>
<Navbar instance_url={data.instance_url} />
<Navbar />
<div class="mx-auto max-w-5xl p-4">
<slot />
</div>
+68 -30
View File
@@ -75,36 +75,74 @@
</div>
</div>
<div class="ml-auto hidden lg:block max-w-sm w-full">
<Card>
<Avatar
width={64}
url={data.site.site_view.site.icon ?? ''}
alt={data.site.site_view.site.name}
/>
<span class="flex flex-row items-center gap-1 text-sm">
<Icon src={Calendar} width={16} height={16} mini />
<RelativeDate date={new Date(data.site.site_view.site.published)} />
</span>
<div class="text-sm flex flex-row flex-wrap gap-3">
<span class="flex flex-row items-center gap-1">
<Icon src={UserGroup} width={16} height={16} mini />
<FormattedNumber number={data.site.site_view.counts.users} />
{#await data.streamed.site}
<Card>
<div
class="w-[64px] h-[64px] rounded-full bg-slate-200
dark:bg-zinc-700 animate-pulse"
/>
<div
class="w-32 h-3 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
<div
class="w-64 h-3 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
<div
class="w-36 h-4 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
<div
class="w-full h-4 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
<div
class="w-24 h-4 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
</Card>
{:then site}
<Card>
<Avatar
width={64}
url={site.site_view.site.icon ?? ''}
alt={site.site_view.site.name}
/>
<span class="flex flex-row items-center gap-1 text-sm">
<Icon src={Calendar} width={16} height={16} mini />
<RelativeDate date={new Date(site.site_view.site.published)} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={PencilSquare} width={16} height={16} mini />
<FormattedNumber number={data.site.site_view.counts.posts} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={ChatBubbleOvalLeftEllipsis} width={16} height={16} mini />
<FormattedNumber number={data.site.site_view.counts.comments} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={Newspaper} width={16} height={16} mini />
<FormattedNumber number={data.site.site_view.counts.communities} />
</span>
</div>
<h1 class="font-bold text-lg">{data.site.site_view.site.name}</h1>
<Markdown source={data.site.site_view.site.description ?? ''} />
</Card>
<div class="text-sm flex flex-row flex-wrap gap-3">
<span class="flex flex-row items-center gap-1">
<Icon src={UserGroup} width={16} height={16} mini />
<FormattedNumber number={site.site_view.counts.users} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={PencilSquare} width={16} height={16} mini />
<FormattedNumber number={site.site_view.counts.posts} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon
src={ChatBubbleOvalLeftEllipsis}
width={16}
height={16}
mini
/>
<FormattedNumber number={site.site_view.counts.comments} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={Newspaper} width={16} height={16} mini />
<FormattedNumber number={site.site_view.counts.communities} />
</span>
</div>
<h1 class="font-bold text-lg">{site.site_view.site.name}</h1>
<Markdown source={site.site_view.site.description ?? ''} />
</Card>
{:catch}
<Card>
<h1 class="font-bold text-lg">Failed to load</h1>
</Card>
{/await}
</div>
</div>
@@ -1,7 +1,7 @@
import { getClient } from '$lib/lemmy.js'
import type { ListingType, SortType } from 'lemmy-js-client'
export function load({ url, cookies }) {
export function load({ url }) {
const page = Number(url.searchParams.get('page') || 1) || 1
const sort: SortType = (url.searchParams.get('sort') as SortType) || 'Active'
@@ -9,12 +9,14 @@ export function load({ url, cookies }) {
(url.searchParams.get('type') as ListingType) || 'Local'
return {
posts: getClient(cookies.get('instance_url')).getPosts({
posts: getClient().getPosts({
limit: 40,
page: page,
sort: sort,
type_: listingType,
}),
site: getClient(cookies.get('instance_url')).getSite({}),
streamed: {
site: getClient().getSite({}),
},
}
}
-1
View File
@@ -1 +0,0 @@
<slot />
-5
View File
@@ -1,5 +0,0 @@
import { getClient } from '$lib/lemmy.js'
export function load({ params }) {
return getClient().getCommunity({ name: params.name })
}
+120
View File
@@ -0,0 +1,120 @@
<script lang="ts">
import { page } from '$app/stores'
import Link from '$lib/components/Link.svelte'
import MultiSelect from '$lib/components/MultiSelect.svelte'
import SvelteMarkdown from 'svelte-markdown'
import Avatar from '../../post/[id]/Avatar.svelte'
import Post from '../../post/[id]/Post.svelte'
import { goto } from '$app/navigation'
import RelativeDate from '$lib/components/RelativeDate.svelte'
import {
Calendar,
ChatBubbleOvalLeftEllipsis,
Icon,
Newspaper,
PencilSquare,
UserGroup,
} from 'svelte-hero-icons'
import FormattedNumber from '$lib/components/FormattedNumber.svelte'
import Card from '$lib/components/Card.svelte'
export let data
</script>
<div class="flex flex-row gap-4 justify-between">
<div class="flex flex-col gap-4 max-w-full">
<div class="flex flex-col sm:flex-row gap-4 max-w-full">
<MultiSelect
options={['Active', 'TopWeek', 'TopAll', 'New']}
optionNames={['Active', 'Top (Week)', 'Top (All)', 'New']}
selected={$page.url.searchParams.get('sort') ?? 'Active'}
on:select={(e) => {
const url = $page.url
url.searchParams.set('sort', e.detail)
url.searchParams.delete('page')
goto(url, {
invalidateAll: true,
})
}}
/>
</div>
{#each data.posts.posts as post}
<Post {post} />
{/each}
<div>
<Link
href={(function () {
$page.url.searchParams.set(
'page',
((Number($page.url.searchParams.get('page')) || 1) + 1).toString()
)
return $page.url.toString()
})()}
>
Next
</Link>
</div>
</div>
<Card>
{#await data.streamed.community}
<div
class="w-[64px] h-[64px] rounded-full bg-slate-200
dark:bg-zinc-700 animate-pulse"
/>
<div
class="w-32 h-3 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
<div
class="w-64 h-3 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
<div
class="w-36 h-4 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
<div
class="w-full h-4 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
<div
class="w-24 h-4 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
{:then community}
<Avatar
width={48}
url={community.community_view.community.icon ??
`https://api.dicebear.com/6.x/initials/svg?seed=${community.community_view.community.name}`}
alt={community.community_view.community.name}
/>
<span class="flex flex-row items-center gap-1 text-sm">
<Icon src={Calendar} width={16} height={16} mini />
<RelativeDate
date={new Date(community.community_view.community.published)}
/>
</span>
<div class="text-sm flex flex-row flex-wrap gap-3">
<span class="flex flex-row items-center gap-1">
<Icon src={UserGroup} width={16} height={16} mini />
<FormattedNumber
number={community.community_view.counts.subscribers}
/>
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={PencilSquare} width={16} height={16} mini />
<FormattedNumber number={community.community_view.counts.posts} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={ChatBubbleOvalLeftEllipsis} width={16} height={16} mini />
<FormattedNumber number={community.community_view.counts.comments} />
</span>
</div>
<h1 class="font-bold text-lg">
{community.community_view.community.name}
</h1>
<SvelteMarkdown source={community.community_view.community.description} />
{/await}
</Card>
</div>
+22 -4
View File
@@ -1,5 +1,23 @@
import { redirect } from '@sveltejs/kit';
import { getClient } from '$lib/lemmy.js'
import type { ListingType, SortType } from 'lemmy-js-client'
export function load({ params }) {
throw redirect(300, `/c/${params.name}/posts`)
}
export function load(req: any) {
const page = Number(req.url.searchParams.get('page') || 1) || 1
const sort: SortType =
(req.url.searchParams.get('sort') as SortType) || 'Active'
return {
posts: getClient().getPosts({
limit: 40,
community_name: req.params.name,
page: page,
sort: sort,
}),
streamed: {
community: getClient().getCommunity({
name: req.params.name,
}),
},
}
}
-16
View File
@@ -1,16 +0,0 @@
import { getClient } from '$lib/lemmy.js'
import type { ListingType, SortType } from 'lemmy-js-client'
export function load(req: any) {
const page = Number(req.url.searchParams.get('page') || 1) || 1
const sort: SortType =
(req.url.searchParams.get('sort') as SortType) || 'Active'
return getClient(req.cookies.get('instance_url')).getPosts({
limit: 40,
community_name: req.params.name,
page: page,
sort: sort,
})
}
-90
View File
@@ -1,90 +0,0 @@
<script lang="ts">
import { page } from '$app/stores'
import Link from '$lib/components/Link.svelte'
import MultiSelect from '$lib/components/MultiSelect.svelte'
import SvelteMarkdown from 'svelte-markdown'
import Avatar from '../../../post/[id]/Avatar.svelte'
import Post from '../../../post/[id]/Post.svelte'
import { goto } from '$app/navigation'
import RelativeDate from '$lib/components/RelativeDate.svelte'
import {
Calendar,
ChatBubbleOvalLeftEllipsis,
Icon,
Newspaper,
PencilSquare,
UserGroup,
} from 'svelte-hero-icons'
import FormattedNumber from '$lib/components/FormattedNumber.svelte'
export let data
</script>
<div class="flex flex-row gap-4 justify-between">
<div class="flex flex-col gap-4 max-w-full">
<div class="flex flex-col sm:flex-row gap-4 max-w-full">
<MultiSelect
options={['Active', 'TopWeek', 'TopAll', 'New']}
optionNames={['Active', 'Top (Week)', 'Top (All)', 'New']}
selected={$page.url.searchParams.get('sort') ?? 'Active'}
on:select={(e) => {
const url = $page.url
url.searchParams.set('sort', e.detail)
url.searchParams.delete('page')
goto(url, {
invalidateAll: true,
})
}}
/>
</div>
{#each data.posts as post}
<Post {post} />
{/each}
<div>
<Link
href={(function () {
$page.url.searchParams.set(
'page',
((Number($page.url.searchParams.get('page')) || 1) + 1).toString()
)
return $page.url.toString()
})()}
>
Next
</Link>
</div>
</div>
<div
class="sticky top-4 bg-white border border-slate-200
dark:border-zinc-800 dark:bg-zinc-900 break-words hidden lg:flex
flex-col gap-4 p-8 max-w-sm rounded-md h-max max-h-[calc(100vh-2rem)] overflow-auto"
>
<Avatar
width={48}
url={data.community_view.community.icon ??
`https://api.dicebear.com/6.x/initials/svg?seed=${data.community_view.community.name}`}
alt={data.community_view.community.name}
/>
<span class="flex flex-row items-center gap-1 text-sm">
<Icon src={Calendar} width={16} height={16} mini />
<RelativeDate date={new Date(data.community_view.community.published)} />
</span>
<div class="text-sm flex flex-row flex-wrap gap-3">
<span class="flex flex-row items-center gap-1">
<Icon src={UserGroup} width={16} height={16} mini />
<FormattedNumber number={data.community_view.counts.subscribers} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={PencilSquare} width={16} height={16} mini />
<FormattedNumber number={data.community_view.counts.posts} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={ChatBubbleOvalLeftEllipsis} width={16} height={16} mini />
<FormattedNumber number={data.community_view.counts.comments} />
</span>
</div>
<h1 class="font-bold text-lg">{data.community_view.community.name}</h1>
<SvelteMarkdown source={data.community_view.community.description} />
</div>
</div>
+1 -1
View File
@@ -9,7 +9,7 @@
PencilSquare,
UserGroup,
} from 'svelte-hero-icons'
import CommunityLink from '$lib/components/CommunityLink.svelte'
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import Link from '$lib/components/Link.svelte'
import { Color } from '$lib/ui/colors.js'
@@ -1,13 +1,13 @@
import { getClient } from '$lib/lemmy.js'
import type { ListingType } from 'lemmy-js-client'
export function load({ url, cookies }) {
export function load({ url }) {
const type = url.searchParams.get('listingType') as ListingType
const page = Number(url.searchParams.get('page')) || 1
const query = url.searchParams.get('q')
if (query) {
return getClient(cookies.get('instance_url')).search({
return getClient().search({
limit: 40,
page: page,
sort: 'Active',
@@ -16,7 +16,7 @@ export function load({ url, cookies }) {
q: query,
})
} else {
return getClient(cookies.get('instance_url')).listCommunities({
return getClient().listCommunities({
limit: 40,
page: page,
sort: 'Active',
+4 -4
View File
@@ -9,7 +9,7 @@
import type { CommentNodeI } from './comments.js'
import Comments from './Comments.svelte'
import SvelteMarkdown from 'svelte-markdown'
import CommunityLink from '$lib/components/CommunityLink.svelte'
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import { isImage } from '$lib/ui/image.js'
export let data: { post: GetPostResponse; comments: GetCommentsResponse }
@@ -88,8 +88,8 @@
<title>{data.post.post_view.post.name}</title>
<div class="flex flex-col gap-2">
<span class="flex flex-row gap-2 text-sm opacity-70">
<CommunityLink community={postData.community} />
<span class="flex flex-row gap-2 text-sm opacity-80 items-center">
<CommunityLink avatar community={postData.community} />
<span class="opacity-50">{postData.creator.name}</span>
</span>
<h1 class="font-bold text-lg">{post.name}</h1>
@@ -112,7 +112,7 @@
{#if post.body}
<p
class="bg-slate-100 border border-slate-200 dark:border-zinc-800
dark:bg-zinc-900 p-2 text-sm rounded-md"
dark:bg-zinc-900 p-2 text-sm rounded-md leading-5"
>
<SvelteMarkdown source={post.body} />
</p>
@@ -1,11 +1,11 @@
import { getClient } from '$lib/lemmy.js'
export async function load({ params, cookies }) {
export async function load({ params }) {
return {
post: await getClient(cookies.get('instance_url')).getPost({
post: await getClient().getPost({
id: Number(params.id),
}),
comments: await getClient(cookies.get('instance_url')).getComments({
comments: await getClient().getComments({
post_id: Number(params.id),
type_: 'All',
limit: 250,
+1 -1
View File
@@ -11,7 +11,7 @@
<ul
class={isParent
? ''
: 'ml-2 mt-2 mx-2.5 px-2.5 border-l-2 border-black/10 dark:border-white/10'}
: 'ml-2.5 mt-2 px-2.5 border-l-2 border-black/10 dark:border-white/10'}
>
{#each nodes.slice(0, maxComments) as node}
<Comment {node} open={true}>
+4 -3
View File
@@ -17,7 +17,7 @@
} from 'svelte-hero-icons'
import type { CommentNodeI } from './comments.js'
import RelativeDate from '$lib/components/RelativeDate.svelte'
import CommunityLink from '$lib/components/CommunityLink.svelte'
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import { isImage } from '$lib/ui/image.js'
let postRes: PostView
@@ -29,8 +29,9 @@
rounded-md flex flex-col overflow-hidden max-w-xl w-full"
>
<div class="flex flex-col gap-2 bg-white dark:bg-zinc-900 p-4 rounded-md">
<span class="flex flex-row gap-2 text-sm opacity-70">
<CommunityLink community={postRes.community} />
<span class="flex flex-row gap-2 text-sm opacity-80 items-center">
<CommunityLink avatar community={postRes.community} />
<div class="ml-auto" />
<span class="opacity-50">{postRes.creator.name}</span>
<span class="opacity-50">
<RelativeDate date={new Date(postRes.post.published)} />
+1 -1
View File
@@ -11,7 +11,7 @@ const config = {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter()
adapter: adapter(),
}
};