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: more elaborate response log when API request fails #1000

Merged
merged 6 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
50 changes: 15 additions & 35 deletions packages/uploadthing/src/internal/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import * as pkgJson from "../../package.json";
import { configProvider, IngestUrl, IsDevelopment, UTToken } from "./config";
import { formatError } from "./error-formatter";
import { handleJsonLineStream } from "./jsonl";
import { withLogFormat, withMinimalLogLevel } from "./logger";
import {
logHttpClientError,
withLogFormat,
withMinimalLogLevel,
} from "./logger";
import { getParseFn } from "./parser";
import { assertFilesMeetConfig, extractRouterConfig } from "./route-config";
import {
Expand Down Expand Up @@ -379,12 +383,8 @@ const handleCallbackRequest = (opts: {
}),
HttpClientRequest.jsonBody(payload),
Effect.flatMap(HttpClient.filterStatusOk(httpClient)),
Effect.tapErrorTag("ResponseError", ({ response: res }) =>
Effect.flatMap(res.json, (json) =>
Effect.logError(
`Failed to register callback result (${res.status})`,
).pipe(Effect.annotateLogs("error", json)),
),
Effect.tapError(
logHttpClientError("Failed to register callback result"),
),
HttpClientResponse.schemaBodyJsonScoped(CallbackResultResponse),
Effect.tap(Effect.log("Sent callback result to UploadThing")),
Expand Down Expand Up @@ -617,19 +617,7 @@ const handleUploadAction = (opts: {
Effect.logDebug("Registerred metadata").pipe(
Effect.annotateLogs("response", res),
),
onFailure: (err) =>
err._tag === "ResponseError"
? Effect.flatMap(err.response.json, (json) =>
Effect.logError(
`Failed to register metadata (${err.response.status})`,
).pipe(
Effect.annotateLogs("response", err.response),
Effect.annotateLogs("json", json),
),
)
: Effect.logError("Failed to register metadata").pipe(
Effect.annotateLogs("error", err),
),
onFailure: logHttpClientError("Failed to register metadata"),
}),
);

Expand Down Expand Up @@ -666,21 +654,13 @@ const handleUploadAction = (opts: {
Effect.annotateLogs("payload", payload),
),
onFailure: (err) =>
err._tag === "ResponseError"
? Effect.flatMap(err.response.json, (json) =>
Effect.logError(
`Failed to forward callback request from dev stream (${err.response.status})`,
).pipe(
Effect.annotateLogs("response", err.response),
Effect.annotateLogs("json", json),
Effect.annotateLogs("hook", hook),
Effect.annotateLogs("signature", signature),
Effect.annotateLogs("payload", payload),
),
)
: Effect.logError(
"Failed to forward callback request from dev stream",
).pipe(Effect.annotateLogs("error", err)),
logHttpClientError(
"Failed to forward callback request from dev stream",
)(err).pipe(
Effect.annotateLogs("hook", hook),
Effect.annotateLogs("signature", signature),
Effect.annotateLogs("payload", payload),
),
}),
Effect.ignoreLogged,
),
Expand Down
12 changes: 12 additions & 0 deletions packages/uploadthing/src/internal/logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { HttpBody, HttpClientError } from "@effect/platform";
import * as Config from "effect/Config";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
Expand Down Expand Up @@ -51,3 +52,14 @@ export const withLogFormat = Effect.gen(function* () {
),
Layer.unwrapEffect,
);

export const logHttpClientError =
(message: string) =>
(err: HttpClientError.HttpClientError | HttpBody.HttpBodyError) =>
err._tag === "ResponseError"
? Effect.flatMap(err.response.json, () =>
Effect.logError(`${message} (${err.response.status})`).pipe(
Effect.annotateLogs("response", err.response),
),
)
: Effect.logError(message).pipe(Effect.annotateLogs("error", err));
16 changes: 9 additions & 7 deletions packages/uploadthing/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import {
UPLOADTHING_VERSION,
UTToken,
} from "../internal/config";
import { withLogFormat, withMinimalLogLevel } from "../internal/logger";
import {
logHttpClientError,
withLogFormat,
withMinimalLogLevel,
} from "../internal/logger";
import type {
ACLUpdateOptions,
DeleteFilesOptions,
Expand Down Expand Up @@ -79,13 +83,11 @@ export class UTApi {
HttpClient.filterStatusOk(httpClient),
Effect.tapBoth({
onSuccess: (res) =>
Effect.logDebug(`UT Response`).pipe(
Effect.annotateLogs("res", res),
),
onFailure: (err) =>
Effect.logError("UploadThing error").pipe(
Effect.annotateLogs("error", err),
Effect.logDebug(`UploadThing API Response`).pipe(
Effect.annotateLogs("response", res),
),

onFailure: logHttpClientError("Failed to request UploadThing API"),
}),
HttpClientResponse.schemaBodyJsonScoped(responseSchema),
);
Expand Down
Loading