Skip to content

Commit

Permalink
handle callback failed
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed Dec 3, 2024
1 parent 9aa5db8 commit 720b975
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
47 changes: 41 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,40 @@ const handleUploadAction = (opts: {
Effect.flatMap(httpClient.execute),
);

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

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 +659,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
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;

0 comments on commit 720b975

Please sign in to comment.