Skip to content

Commit 0d3936e

Browse files
committed
chore: error handling for vercel
1 parent fa7a6e7 commit 0d3936e

File tree

3 files changed

+82
-16
lines changed

3 files changed

+82
-16
lines changed

frontend/src/app/dialogs/connect-vercel-frame.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from "@tanstack/react-query";
77
import confetti from "canvas-confetti";
88
import { useWatch } from "react-hook-form";
9-
import z from "zod";
9+
import type z from "zod";
1010
import * as ConnectVercelForm from "@/app/forms/connect-vercel-form";
1111
import { HelpDropdown } from "@/app/help-dropdown";
1212
import {
@@ -21,7 +21,7 @@ import {
2121
import { type Region, useEngineCompatDataProvider } from "@/components/actors";
2222
import { type JoinStepSchemas, StepperForm } from "../forms/stepper-form";
2323

24-
const {stepper} = ConnectVercelForm;
24+
const { stepper } = ConnectVercelForm;
2525

2626
type FormValues = z.infer<JoinStepSchemas<typeof stepper.steps>>;
2727

@@ -183,7 +183,7 @@ function Step3() {
183183
</p>
184184
<div className="mt-2">
185185
<ConnectVercelForm.Endpoint />
186-
<ConnectVercelForm.ConnectionCheck />
186+
<ConnectVercelForm.ConnectionCheck />
187187
</div>
188188
</>
189189
);

frontend/src/app/forms/connect-vercel-form.tsx

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ import {
55
faTriangleExclamation,
66
Icon,
77
} from "@rivet-gg/icons";
8+
import type { Rivet } from "@rivetkit/engine-api-full";
89
import { useInfiniteQuery, useQuery } from "@tanstack/react-query";
910
import { AnimatePresence, motion } from "framer-motion";
1011
import { useEffect } from "react";
11-
import { useController, useFieldArray, useForm, useFormContext, useWatch } from "react-hook-form";
12+
import {
13+
useController,
14+
useFieldArray,
15+
useForm,
16+
useFormContext,
17+
useWatch,
18+
} from "react-hook-form";
19+
import { match, P } from "ts-pattern";
1220
import { useDebounceValue } from "usehooks-ts";
1321
import z from "zod";
1422
import {
@@ -33,9 +41,8 @@ import {
3341
SelectValue,
3442
} from "@/components";
3543
import { useEngineCompatDataProvider } from "@/components/actors";
36-
import { VisibilitySensor } from "@/components/visibility-sensor";
3744
import { defineStepper } from "@/components/ui/stepper";
38-
import { Rivet } from "@rivetkit/engine-api-full";
45+
import { VisibilitySensor } from "@/components/visibility-sensor";
3946

4047
const endpointSchema = z
4148
.string()
@@ -405,7 +412,13 @@ const code = ({ plan }: { plan: string }) =>
405412
export const Json = ({ plan }: { plan: string }) => {
406413
return (
407414
<div className="space-y-2 mt-2">
408-
<CodeFrame language="json" title="vercel.json" code={() => code({plan}).replaceAll(" // [!code highlight]", "")}>
415+
<CodeFrame
416+
language="json"
417+
title="vercel.json"
418+
code={() =>
419+
code({ plan }).replaceAll(" // [!code highlight]", "")
420+
}
421+
>
409422
<CodePreview
410423
className="w-full min-w-0"
411424
language="json"
@@ -442,7 +455,8 @@ export const Endpoint = ({
442455
<Input
443456
type="url"
444457
placeholder={
445-
placeholder || "https://my-rivet-app.vercel.app/api/rivet"
458+
placeholder ||
459+
"https://my-rivet-app.vercel.app/api/rivet"
446460
}
447461
{...field}
448462
/>
@@ -462,15 +476,15 @@ export function ConnectionCheck() {
462476

463477
const enabled = !!endpoint && endpointSchema.safeParse(endpoint).success;
464478

465-
console.log(enabled);
466-
467479
const [debounced] = useDebounceValue(endpoint, 300);
468480

469481
const { isSuccess, data, isError, isRefetchError, isLoadingError, error } =
470482
useQuery({
471483
...dataProvider.runnerHealthCheckQueryOptions({
472484
runnerUrl: debounced,
473-
headers: Object.fromEntries(headers.filter(([k, v]) => k && v).map(([k, v]) => [k, v])),
485+
headers: Object.fromEntries(
486+
headers.filter(([k, v]) => k && v).map(([k, v]) => [k, v]),
487+
),
474488
}),
475489
enabled,
476490
retry: 0,
@@ -517,7 +531,9 @@ export function ConnectionCheck() {
517531
Health check failed, verify the endpoint is
518532
correct.
519533
</p>
520-
{isRivetHealthCheckFailureResponse(error) ? <p className="font-mono-console">{JSON.stringify(error.failure.error)}</p> : null}
534+
{isRivetHealthCheckFailureResponse(error) ? (
535+
<HealthCheckFailure error={error} />
536+
) : null}
521537
<p>
522538
Endpoint{" "}
523539
<a
@@ -547,7 +563,58 @@ export function ConnectionCheck() {
547563
);
548564
}
549565

566+
function isRivetHealthCheckFailureResponse(
567+
error: any,
568+
): error is Rivet.RunnerConfigsServerlessHealthCheckResponseFailure["failure"] {
569+
return error && "error" in error;
570+
}
571+
572+
function HealthCheckFailure({
573+
error,
574+
}: {
575+
error: Rivet.RunnerConfigsServerlessHealthCheckResponseFailure["failure"];
576+
}) {
577+
if (!("error" in error)) {
578+
return null;
579+
}
580+
if (!error.error) {
581+
return null;
582+
}
550583

551-
function isRivetHealthCheckFailureResponse(error: any): error is Rivet.RunnerConfigsServerlessHealthCheckResponseFailure {
552-
return error && 'failure' in error;
553-
}
584+
return match(error.error)
585+
.with({ nonSuccessStatus: P.any }, (e) => {
586+
return (
587+
<p>
588+
Health check failed with status{" "}
589+
{e.nonSuccessStatus.statusCode}
590+
</p>
591+
);
592+
})
593+
.with({ invalidRequest: P.any }, (e) => {
594+
return <p>Health check failed because the request was invalid.</p>;
595+
})
596+
.with({ invalidResponseJson: P.any }, (e) => {
597+
return (
598+
<p>
599+
Health check failed because the response was not valid JSON.
600+
</p>
601+
);
602+
})
603+
.with({ requestFailed: P.any }, (e) => {
604+
return (
605+
<p>
606+
Health check failed because the request could not be
607+
completed.
608+
</p>
609+
);
610+
})
611+
.with({ requestTimedOut: P.any }, (e) => {
612+
return <p>Health check failed because the request timed out.</p>;
613+
})
614+
.with({ invalidResponseSchema: P.any }, (e) => {
615+
return (
616+
<p>Health check failed because the response was not valid.</p>
617+
);
618+
})
619+
.exhaustive();
620+
}

frontend/src/app/forms/stepper-form.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ function Content<const Steps extends Step[]>({
8787
const ref = useRef<z.infer<JoinStepSchemas<Steps>> | null>({});
8888

8989
const handleSubmit = (values: z.infer<JoinStepSchemas<Steps>>) => {
90-
console.log("submitting");
9190
ref.current = { ...ref.current, ...values };
9291
if (stepper.isLast) {
9392
return onSubmit?.({ values: ref.current, form, stepper });

0 commit comments

Comments
 (0)