Skip to content

The only tool you need to know what is happening and how to fix it.

5.9 KB
Raw Blame
<script lang="ts">
	import { getErrorMessage, getErrorStatus } from '$lib/utils/errors';
	import { untrack } from 'svelte';
	import { page } from '$app/state';
	import { goto } from '$app/navigation';
	import { api } from '$lib/api';
	import { LoadingCircle } from '$lib/components/ui/loading-circle';
	import { ErrorDisplay } from '$lib/components/ui/error-display';
	import { projectsState, isJsFramework, isJsLanguage } from '$lib/state/projects.svelte';
	import { StackTraceCard, EventCard, EventsTable } from '$lib/components/issues';
	import PageHeader from '$lib/components/traceway/page-header.svelte';
	import { toast } from 'svelte-sonner';
	import ArchiveConfirmationDialog from '$lib/components/archive-confirmation-dialog.svelte';
	import OncallOwner from '$lib/components/traceway/oncall-owner.svelte';
	import { linkedTraceFrom } from '$lib/types/exceptions';
	import type {
		ExceptionGroup,
		ExceptionOccurrence,
		LinkedTrace,
		SessionRecording
	} from '$lib/types/exceptions';
	import { createSmartBackHandler } from '$lib/utils/back-navigation';
	import { resolve } from '$app/paths';

	let group = $state<ExceptionGroup | null>(null);
	let occurrences = $state<ExceptionOccurrence[]>([]);
	let loading = $state(true);
	let error = $state('');
	let notFound = $state(false);
	let total = $state(0);
	let linkedTrace = $state<LinkedTrace | null>(null);
	let sessionRecording = $state<SessionRecording | null>(null);
	let sessionId = $state<string | null>(null);
	let showArchiveDialog = $state(false);
	let archiving = $state(false);

	const exceptionHash = $derived(page.params.exceptionHash ?? '');
	const latestOccurrence = $derived(occurrences[0]);
	const isMessage = $derived(latestOccurrence?.isMessage ?? false);
	const isJavaScript = $derived(
		(projectsState.currentProject?.framework
			? isJsFramework(projectsState.currentProject.framework)
			: false) || isJsLanguage(latestOccurrence?.attributes?.['telemetry.sdk.language'])
	);
	const isFlutter = $derived(projectsState.currentProject?.framework === 'flutter');
	const isIOS = $derived(
		projectsState.currentProject?.framework === 'ios' ||
			['ios', 'swift'].includes(
				(latestOccurrence?.attributes?.['telemetry.sdk.language'] ?? '').toLowerCase()
			)
	);
	const hasMoreOccurrences = $derived(total > 10);
	const firstLineOfStackTrace = $derived(
		latestOccurrence?.stackTrace.split('\n')[0] || 'Exception'
	);

	let loadSequence = 0;

	async function loadData() {
		const sequence = ++loadSequence;
		loading = true;
		error = '';
		notFound = false;
		linkedTrace = null;
		sessionRecording = null;
		sessionId = null;

		try {
			const exceptionHash = page.params.exceptionHash;
			const response = await api.post(
				`/exception-stack-traces/${exceptionHash}`,
				{
					pagination: {
						page: 1,
						pageSize: 10
					}
				},
				{ projectId: projectsState.currentProjectId ?? undefined }
			);

			if (sequence !== loadSequence) return;
			group = response.group;
			occurrences = response.occurrences || [];
			total = response.pagination.total;
			sessionRecording = response.sessionRecording ?? null;
			sessionId = response.sessionId ?? null;
			linkedTrace = linkedTraceFrom(response.relatedEntity);
		} catch (e) {
			if (sequence !== loadSequence) return;
			console.error(e);
			if (getErrorStatus(e) === 404) {
				notFound = true;
			} else {
				error = getErrorMessage(e) || 'Failed to load exception details';
			}
		} finally {
			if (sequence === loadSequence) loading = false;
		}
	}

	async function archiveIssue(resolvePages: boolean) {
		archiving = true;
		try {
			await api.post(
				'/exception-stack-traces/archive',
				{ hashes: [exceptionHash], resolvePages },
				{ projectId: projectsState.currentProjectId ?? undefined }
			);
			toast.success('Successfully archived the Issue');
			goto(resolve('/issues'));
		} catch (e) {
			console.error('Archive failed:', e);
			throw e;
		} finally {
			archiving = false;
		}
	}

	$effect(() => {
		void page.params.exceptionHash;
		void projectsState.currentProjectId;
		untrack(() => {
			group = null;
			occurrences = [];
			total = 0;
			loadData();
		});
		return () => {
			loadSequence++;
		};
	});
</script>

<div class="space-y-6">
	<PageHeader
		title={firstLineOfStackTrace}
		subtitle="Exception Hash: {exceptionHash}"
		onBack={createSmartBackHandler({ fallbackPath: resolve('/issues') })}
	>
		{#snippet actions()}
			<OncallOwner />
		{/snippet}
	</PageHeader>

	{#if loading && !group}
		<div class="flex items-center justify-center py-20">
			<LoadingCircle size="xlg" />
		</div>
	{:else if notFound}
		<ErrorDisplay
			status={404}
			title="Exception Not Found"
			description="The exception you're looking for doesn't exist or may have been removed. It's possible the data has expired or the link is incorrect."
			onBack={createSmartBackHandler({ fallbackPath: resolve('/issues') })}
			backLabel="Back to Issues"
			onRetry={() => loadData()}
			identifier={exceptionHash}
		/>
	{:else if error}
		<ErrorDisplay
			status={400}
			title="Something Went Wrong"
			description={error}
			onBack={createSmartBackHandler({ fallbackPath: resolve('/issues') })}
			backLabel="Back to Issues"
			onRetry={() => loadData()}
		/>
	{:else if group}
		<StackTraceCard
			stackTrace={latestOccurrence?.stackTrace ?? group.stackTrace}
			{isMessage}
			{isJavaScript}
			{isFlutter}
			{isIOS}
			firstSeen={group.firstSeen}
			lastSeen={group.lastSeen}
			totalCount={group.count}
			bind:showArchiveDialog
			bind:archiving
		/>

		{#if latestOccurrence}
			<EventCard
				occurrence={latestOccurrence}
				{linkedTrace}
				{sessionRecording}
				{sessionId}
				title="Last Event"
				description="Details from the most recent occurrence of this exception"
			/>
		{/if}

		<EventsTable
			{occurrences}
			{exceptionHash}
			{total}
			hasMore={hasMoreOccurrences}
			showViewAll={true}
		/>
	{/if}
</div>

<ArchiveConfirmationDialog
	open={showArchiveDialog}
	onOpenChange={(open) => (showArchiveDialog = open)}
	count={1}
	hashes={[exceptionHash]}
	onConfirm={archiveIssue}
/>