:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay): surface the real error when S3 returns an unreadable response (#73690)
Paul D'Ambra326d9ee277be
diff --git a/nodejs/src/session-replay/recording-rasterizer/__tests__/storage.test.ts b/nodejs/src/session-replay/recording-rasterizer/__tests__/storage.test.tsindex 049a90d1644f..51ed8e2e243b 100644--- a/nodejs/src/session-replay/recording-rasterizer/__tests__/storage.test.ts+++ b/nodejs/src/session-replay/recording-rasterizer/__tests__/storage.test.ts@@ -88,9 +88,39 @@ describe('uploadToS3', () => {expect(mockOn).not.toHaveBeenCalled()})- it('propagates upload errors', async () => {- mockDone.mockRejectedValue(new Error('AccessDenied'))- await expect(uploadToS3('/tmp/v.mp4', 'bucket', 'prefix', 'id')).rejects.toThrow('AccessDenied')+ it('reports a response the SDK could not read, leaving it retryable', async () => {+ // All the SDK reports is its parser's confusion; the raw body it choked on is on $responseBodyText.+ const undecodable = Object.assign(new Error("char 'E' is not expected.:1:1\n Deserialization error"), {+ $responseBodyText: 'Error: proxy denied for s3://secret-bucket/tenant-42.mp4',+ $response: { statusCode: 407 },+ })+ mockDone.mockRejectedValue(undecodable)++ await expect(uploadToS3('/tmp/v.mp4', 'bucket', 'prefix', 'id')).rejects.toMatchObject({+ name: 'RasterizationError',+ code: 'S3_UPLOAD_UNDECODABLE_RESPONSE',+ // Translating the failure must not make it terminal.+ retryable: true,+ cause: undecodable,+ // This message is shown to team users as ReplayObservation.error_reason, so it must carry+ // neither the bucket and key nor anything the object store put in the body.+ message: 'S3 upload failed: the object store returned an unreadable (non-XML) response (status 407)',+ })+ })++ it.each([+ {+ failure: 'a modeled service error whose body parsed fine',+ error: Object.assign(new Error('Access Denied'), {+ name: 'AccessDenied',+ $response: { statusCode: 403 },+ $metadata: { httpStatusCode: 403 },+ }),+ },+ { failure: 'a failure carrying no HTTP response', error: new Error('socket hang up') },+ ])('rethrows $failure untouched, so callers still see the SDK error', async ({ error }) => {+ mockDone.mockRejectedValue(error)+ await expect(uploadToS3('/tmp/v.mp4', 'bucket', 'prefix', 'id')).rejects.toBe(error)})})diff --git a/nodejs/src/session-replay/recording-rasterizer/storage.ts b/nodejs/src/session-replay/recording-rasterizer/storage.tsindex c5c81e5bd847..a279f56992a5 100644--- a/nodejs/src/session-replay/recording-rasterizer/storage.ts+++ b/nodejs/src/session-replay/recording-rasterizer/storage.ts@@ -4,10 +4,29 @@ import * as fs from 'fs'import { HttpsProxyAgent } from 'https-proxy-agent'import { config } from './config'+import { RasterizationError } from './errors'import { createLogger } from './logger'const log = createLogger()+// The AWS SDK attaches $responseBodyText to an error only when it could not parse the response body at+// all, and hangs the HTTP response itself on a non-enumerable $response. Both are absent from its public+// error type. That pair identifies the one failure worth translating: a proxy or gateway answering with+// plaintext or an HTML error page instead of S3's XML, where the error the SDK raises describes its own+// parser rather than the request, and the raw body is the only place the real reason survives.+type UndecodableS3Response = {+ $responseBodyText?: string+ $response?: { statusCode?: number }+}++function undecodableResponse(err: unknown): { status?: number; body: string } | null {+ const { $responseBodyText, $response } = (err ?? {}) as UndecodableS3Response+ if (typeof $responseBodyText !== 'string') {+ return null+ }+ return { status: $response?.statusCode, body: $responseBodyText.slice(0, 500) }+}+let s3Client: S3Client | null = nullfunction resolveProxyUrl(): string | null {@@ -75,7 +94,29 @@ export async function uploadToS3(upload.on('httpUploadProgress', () => onProgress())}- await upload.done()+ const target = `s3://${bucket}/${key}`+ try {+ await upload.done()+ } catch (err) {+ const undecodable = undecodableResponse(err)+ if (!undecodable) {+ throw err+ }+ // Bucket, key and the raw body stay in this log line. The thrown message reaches team users as+ // ReplayObservation.error_reason, and the body is whatever an upstream proxy or gateway chose to+ // return, so only the status code goes into it. Retryability is untouched: the workflow's retry+ // policy keeps deciding, as it does for any other upload failure.+ log.warn(+ { bucket, key, status: undecodable.status, response_body: undecodable.body },+ 'S3 upload returned an unreadable response'+ )+ throw new RasterizationError(+ `S3 upload failed: the object store returned an unreadable (non-XML) response (status ${undecodable.status ?? 'unknown'})`,+ true,+ 'S3_UPLOAD_UNDECODABLE_RESPONSE',+ err+ )+ }- return `s3://${bucket}/${key}`+ return target}