Fixed error when configuring a new endpoint failed and fixed ping to now throw an error when parsing JSON
This commit is contained in:
@@ -57,6 +57,12 @@ export async function action({ request, params }: ActionArgs) {
|
||||
return json(submission);
|
||||
}
|
||||
|
||||
return json(e, { status: 400 });
|
||||
if (e instanceof Error) {
|
||||
submission.error.url = `${e.name}: ${e.message}`;
|
||||
} else {
|
||||
submission.error.url = "Unknown error";
|
||||
}
|
||||
|
||||
return json(submission, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
RunJobBody,
|
||||
RunJobResponseSchema,
|
||||
} from "@trigger.dev/core";
|
||||
import { safeBodyFromResponse } from "~/utils/json";
|
||||
import { safeBodyFromResponse, safeParseBodyFromResponse } from "~/utils/json";
|
||||
import { logger } from "./logger.server";
|
||||
|
||||
export class EndpointApiError extends Error {
|
||||
@@ -27,11 +27,7 @@ export class EndpointApiError extends Error {
|
||||
|
||||
// TODO: this should work with tunnelling
|
||||
export class EndpointApi {
|
||||
constructor(
|
||||
private apiKey: string,
|
||||
private url: string,
|
||||
private id: string
|
||||
) {}
|
||||
constructor(private apiKey: string, private url: string, private id: string) {}
|
||||
|
||||
async ping(): Promise<PongResponse> {
|
||||
const response = await safeFetch(this.url, {
|
||||
@@ -73,13 +69,23 @@ export class EndpointApi {
|
||||
};
|
||||
}
|
||||
|
||||
const anyBody = await response.json();
|
||||
const pongResponse = await safeParseBodyFromResponse(response, PongResponseSchema);
|
||||
|
||||
logger.debug("ping() response from endpoint", {
|
||||
body: anyBody,
|
||||
});
|
||||
if (!pongResponse) {
|
||||
return {
|
||||
ok: false,
|
||||
error: `Could not parse response from endpoint. Make sure it points to the correct URL (you might be missing /api/trigger)`,
|
||||
};
|
||||
}
|
||||
|
||||
return PongResponseSchema.parse(anyBody);
|
||||
if (!pongResponse.success) {
|
||||
return {
|
||||
ok: false,
|
||||
error: `Endpoint ${this.url} responded with error: ${pongResponse.error.message}`,
|
||||
};
|
||||
}
|
||||
|
||||
return pongResponse.data;
|
||||
}
|
||||
|
||||
async indexEndpoint() {
|
||||
|
||||
@@ -43,3 +43,20 @@ export async function safeBodyFromResponse<T>(
|
||||
return parsedJson.data;
|
||||
}
|
||||
}
|
||||
|
||||
export async function safeParseBodyFromResponse<T>(
|
||||
response: Response,
|
||||
schema: z.Schema<T>
|
||||
): Promise<z.SafeParseReturnType<unknown, T> | undefined> {
|
||||
try {
|
||||
const unknownJson = await response.json();
|
||||
|
||||
if (!unknownJson) {
|
||||
return;
|
||||
}
|
||||
|
||||
const parsedJson = schema.safeParse(unknownJson);
|
||||
|
||||
return parsedJson;
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user