Added errors in the form if creating the endpoint fails
This commit is contained in:
@@ -2,7 +2,10 @@ import { parse } from "@conform-to/zod";
|
||||
import { ActionArgs, json } from "@remix-run/server-runtime";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "~/db.server";
|
||||
import { CreateEndpointService } from "~/services/endpoints/createEndpoint.server";
|
||||
import {
|
||||
CreateEndpointError,
|
||||
CreateEndpointService,
|
||||
} from "~/services/endpoints/createEndpoint.server";
|
||||
import { requireUserId } from "~/services/session.server";
|
||||
|
||||
const ParamsSchema = z.object({
|
||||
@@ -19,13 +22,8 @@ export async function action({ request, params }: ActionArgs) {
|
||||
const { environmentParam } = ParamsSchema.parse(params);
|
||||
|
||||
const formData = await request.formData();
|
||||
const object = Object.fromEntries(formData.entries());
|
||||
console.log("object", object);
|
||||
|
||||
const submission = parse(formData, { schema: bodySchema });
|
||||
|
||||
console.log(submission);
|
||||
|
||||
if (!submission.value || submission.intent !== "submit") {
|
||||
return json(submission);
|
||||
}
|
||||
@@ -53,6 +51,11 @@ export async function action({ request, params }: ActionArgs) {
|
||||
});
|
||||
return json(result);
|
||||
} catch (e) {
|
||||
if (e instanceof CreateEndpointError) {
|
||||
submission.error.url = e.message;
|
||||
return json(submission);
|
||||
}
|
||||
|
||||
return json(e, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,15 @@ const indexingHookIdentifier = customAlphabet(
|
||||
10
|
||||
);
|
||||
|
||||
export class CreateEndpointError extends Error {
|
||||
code: "FAILED_PING" | "FAILED_UPSERT";
|
||||
constructor(code: "FAILED_PING" | "FAILED_UPSERT", message: string) {
|
||||
super(message);
|
||||
Object.setPrototypeOf(this, CreateEndpointError.prototype);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
export class CreateEndpointService {
|
||||
#prismaClient: PrismaClient;
|
||||
|
||||
@@ -30,53 +39,63 @@ export class CreateEndpointService {
|
||||
const pong = await client.ping();
|
||||
|
||||
if (!pong.ok) {
|
||||
throw new Error(pong.error);
|
||||
throw new CreateEndpointError("FAILED_PING", pong.error);
|
||||
}
|
||||
|
||||
return await $transaction(this.#prismaClient, async (tx) => {
|
||||
const endpoint = await tx.endpoint.upsert({
|
||||
where: {
|
||||
environmentId_slug: {
|
||||
environmentId: environment.id,
|
||||
try {
|
||||
const result = await $transaction(this.#prismaClient, async (tx) => {
|
||||
const endpoint = await tx.endpoint.upsert({
|
||||
where: {
|
||||
environmentId_slug: {
|
||||
environmentId: environment.id,
|
||||
slug: id,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
environment: {
|
||||
connect: {
|
||||
id: environment.id,
|
||||
},
|
||||
},
|
||||
organization: {
|
||||
connect: {
|
||||
id: environment.organizationId,
|
||||
},
|
||||
},
|
||||
project: {
|
||||
connect: {
|
||||
id: environment.projectId,
|
||||
},
|
||||
},
|
||||
slug: id,
|
||||
url,
|
||||
indexingHookIdentifier: indexingHookIdentifier(),
|
||||
},
|
||||
},
|
||||
create: {
|
||||
environment: {
|
||||
connect: {
|
||||
id: environment.id,
|
||||
},
|
||||
update: {
|
||||
url,
|
||||
},
|
||||
organization: {
|
||||
connect: {
|
||||
id: environment.organizationId,
|
||||
},
|
||||
});
|
||||
|
||||
// Kick off process to fetch the jobs for this endpoint
|
||||
await workerQueue.enqueue(
|
||||
"indexEndpoint",
|
||||
{
|
||||
id: endpoint.id,
|
||||
source: "INTERNAL",
|
||||
},
|
||||
project: {
|
||||
connect: {
|
||||
id: environment.projectId,
|
||||
},
|
||||
},
|
||||
slug: id,
|
||||
url,
|
||||
indexingHookIdentifier: indexingHookIdentifier(),
|
||||
},
|
||||
update: {
|
||||
url,
|
||||
},
|
||||
{ tx }
|
||||
);
|
||||
|
||||
return endpoint;
|
||||
});
|
||||
|
||||
// Kick off process to fetch the jobs for this endpoint
|
||||
await workerQueue.enqueue(
|
||||
"indexEndpoint",
|
||||
{
|
||||
id: endpoint.id,
|
||||
source: "INTERNAL",
|
||||
},
|
||||
{ tx }
|
||||
);
|
||||
|
||||
return endpoint;
|
||||
});
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw new CreateEndpointError("FAILED_UPSERT", error.message);
|
||||
} else {
|
||||
throw new CreateEndpointError("FAILED_UPSERT", "Something went wrong");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user