Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Return 404 when a signed PDF and its source are both gone

Claude24510d6f00fa
diff --git a/src/lib/downloads.ts b/src/lib/downloads.ts
index 9fbdc77..a94eab0 100644
--- a/src/lib/downloads.ts
+++ b/src/lib/downloads.ts
@@ -23,6 +23,7 @@ import {
} from "./services/pdf-finalize.ts";
import {
createStorageService,
+ StorageObjectNotFoundError,
type StorageService,
} from "./services/storage.ts";
import { getRecipientCertificateKey } from "./services/storage-keys.ts";
@@ -516,19 +517,31 @@ async function buildSignedPdfDownload(
listFieldsForEnvelope(envelope.id, database),
listSignersForEnvelope(envelope.id, database),
]);
- const body = await renderFinalizedEnvelopePdf(
- {
- auditEvents,
- completedAt: envelope.completedAt,
- envelope,
- fields,
- signers,
- sourcePdfKey: envelope.sourcePdfKey,
- },
- {
- storageService,
- },
- );
+ let body: Uint8Array;
+
+ try {
+ body = await renderFinalizedEnvelopePdf(
+ {
+ auditEvents,
+ completedAt: envelope.completedAt,
+ envelope,
+ fields,
+ signers,
+ sourcePdfKey: envelope.sourcePdfKey,
+ },
+ {
+ storageService,
+ },
+ );
+ } catch (error) {
+ // The signed PDF is gone and so is the source it would be rebuilt from,
+ // for example after the retention period removed both.
+ if (error instanceof StorageObjectNotFoundError) {
+ throw new DownloadAccessError("Signed PDF is unavailable.", 404);
+ }
+
+ throw error;
+ }
if (envelope.signedPdfKey) {
await storageService.putObject({