Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: gracefully handle dev callback failed #1081

Merged
merged 7 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions packages/uploadthing/src/_internal/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
HttpServerRequest,
HttpServerResponse,
} from "@effect/platform";
import type { ResponseError } from "@effect/platform/HttpClientError";
import * as Config from "effect/Config";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -601,6 +602,42 @@ const handleUploadAction = (opts: {
Effect.flatMap(httpClient.execute),
);

const handleDevStreamError = Effect.fn("handleDevStreamError")(function* (
err: ResponseError,
chunk: string,
) {
const schema = Schema.parseJson(
Schema.Struct({ file: UploadedFileData }),
);
const parsedChunk = yield* Schema.decodeUnknown(schema)(chunk);
const key = parsedChunk.file.key;

yield* Effect.logError(
"Failed to forward callback request from dev stream",
).pipe(Effect.annotateLogs({ fileKey: key, error: err.message }));

const httpResponse = yield* HttpClientRequest.post(
"/callback-result",
).pipe(
HttpClientRequest.prependUrl(ingestUrl),
HttpClientRequest.setHeaders({
"x-uploadthing-api-key": Redacted.value(apiKey),
"x-uploadthing-version": pkgJson.version,
"x-uploadthing-be-adapter": beAdapter,
"x-uploadthing-fe-package": fePackage,
}),
HttpClientRequest.bodyJson({
fileKey: key,
error: `Failed to forward callback request from dev stream: ${err.message}`,
}),
Effect.flatMap(httpClient.execute),
);

yield* logHttpClientResponse("Reported callback error to UploadThing")(
httpResponse,
);
});

// Send metadata to UT server (non blocking as a daemon)
// In dev, keep the stream open and simulate the callback requests as
// files complete uploading
Expand All @@ -624,14 +661,14 @@ const handleUploadAction = (opts: {
HttpBody.text(chunk.payload, "application/json"),
),
httpClient.execute,
Effect.tapBoth({
onSuccess: logHttpClientResponse(
Effect.tap(
logHttpClientResponse(
"Successfully forwarded callback request from dev stream",
),
onFailure: logHttpClientError(
"Failed to forward callback request from dev stream",
),
}),
),
Effect.catchTag("ResponseError", (err) =>
handleDevStreamError(err, chunk.payload),
),
Effect.annotateLogs(chunk),
Effect.asVoid,
Effect.ignoreLogged,
Expand Down
29 changes: 22 additions & 7 deletions packages/uploadthing/src/_internal/upload-browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { unsafeCoerce } from "effect/Function";
import * as Micro from "effect/Micro";
import { hasProperty, isRecord } from "effect/Predicate";

import type { FetchContext, FetchError } from "@uploadthing/shared";
import { fetchEff, UploadThingError } from "@uploadthing/shared";
Expand Down Expand Up @@ -37,13 +38,27 @@ const uploadWithProgress = (
previousLoaded = loaded;
});
xhr.addEventListener("load", () => {
resume(
xhr.status >= 200 && xhr.status < 300
? Micro.succeed(xhr.response)
: Micro.die(
`XHR failed ${xhr.status} ${xhr.statusText} - ${JSON.stringify(xhr.response)}`,
),
);
if (xhr.status >= 200 && xhr.status < 300 && isRecord(xhr.response)) {
if (hasProperty(xhr.response, "error")) {
resume(
new UploadThingError({
code: "UPLOAD_FAILED",
message: String(xhr.response.error),
data: xhr.response as never,
}),
);
} else {
resume(Micro.succeed(xhr.response));
}
} else {
resume(
new UploadThingError({
code: "UPLOAD_FAILED",
message: `XHR failed ${xhr.status} ${xhr.statusText}`,
data: xhr.response as never,
}),
);
}
});

// Is there a case when the client would throw and
Expand Down
22 changes: 22 additions & 0 deletions playground/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
MiddlewareConfig,
NextResponse,
type NextMiddleware,
} from "next/server";

import { getSession } from "./lib/data";

export default (async (req) => {
if (req.nextUrl.pathname !== "/") {
const sesh = await getSession();
if (!sesh) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
}

return NextResponse.next();
}) satisfies NextMiddleware;

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
} satisfies MiddlewareConfig;
Loading