Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add onError handler

Marc Seitz0581b4b93e2b
diff --git a/components/upload-zone.tsx b/components/upload-zone.tsx
index 8adb03bb..99ec9a62 100644
--- a/components/upload-zone.tsx
+++ b/components/upload-zone.tsx
@@ -141,6 +141,16 @@ export default function UploadZone({
setProgress(Math.round(_progress / acceptedFiles.length));
},
+ onError: (error) => {
+ setUploads((prev) =>
+ prev.filter((upload) => upload.fileName !== file.name),
+ );
+
+ setRejectedFiles((prev) => [
+ { fileName: file.name, message: "Error uploading file" },
+ ...prev,
+ ]);
+ },
ownerId: (session?.user as CustomUser).id,
teamId: teamInfo?.currentTeam?.id as string,
numPages,
diff --git a/lib/files/tus-upload.ts b/lib/files/tus-upload.ts
index 6087cf51..f69bac74 100644
--- a/lib/files/tus-upload.ts
+++ b/lib/files/tus-upload.ts
@@ -5,6 +5,7 @@ import { decodeBase64Url } from "../utils/decode-base64url";
type ResumableUploadParams = {
file: File;
onProgress?: (bytesUploaded: number, bytesTotal: number) => void;
+ onError?: (error: Error | tus.DetailedError) => void;
ownerId: string;
teamId: string;
numPages: number;
@@ -25,6 +26,7 @@ type UploadResult = {
export function resumableUpload({
file,
onProgress,
+ onError,
ownerId,
teamId,
numPages,
@@ -54,9 +56,22 @@ export function resumableUpload({
},
chunkSize: 4 * 1024 * 1024,
onError: (error) => {
+ onError && onError(error);
console.error("Failed because: " + error);
reject(error);
},
+ onShouldRetry(error, retryAttempt, options) {
+ console.error(`Should retry upload. Attempt ${retryAttempt}`);
+ var status = error.originalResponse
+ ? error.originalResponse.getStatus()
+ : 0;
+ // Do not retry if the status is a 500.
+ if (status === 500 || status === 403) {
+ return false;
+ }
+ // For any other status code, we retry.
+ return true;
+ },
onProgress,
onSuccess: () => {
console.log("Uploaded successfully");