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

@uppy/xhr-upload: allow custom error message in onAfterResponse #5578

Merged
merged 1 commit into from
Jan 7, 2025

Conversation

Murderlon
Copy link
Member

Closes #5570

@Murderlon Murderlon self-assigned this Jan 7, 2025
Copy link
Contributor

github-actions bot commented Jan 7, 2025

Diff output files
diff --git a/packages/@uppy/utils/lib/fetcher.js b/packages/@uppy/utils/lib/fetcher.js
index c273c2c..bcacbe2 100644
--- a/packages/@uppy/utils/lib/fetcher.js
+++ b/packages/@uppy/utils/lib/fetcher.js
@@ -28,6 +28,16 @@ export function fetcher(url, options) {
     }
     return new Promise(async (resolve, reject) => {
       const xhr = new XMLHttpRequest();
+      const onError = error => {
+        if (shouldRetry(xhr) && retryCount < retries) {
+          setTimeout(() => {
+            requestWithRetry(retryCount + 1).then(resolve, reject);
+          }, delay(retryCount));
+        } else {
+          timer.done();
+          reject(error);
+        }
+      };
       xhr.open(method, url, true);
       xhr.withCredentials = withCredentials;
       if (responseType) {
@@ -38,7 +48,13 @@ export function fetcher(url, options) {
         reject(new DOMException("Aborted", "AbortError"));
       });
       xhr.onload = async () => {
-        await onAfterResponse(xhr, retryCount);
+        try {
+          await onAfterResponse(xhr, retryCount);
+        } catch (err) {
+          err.request = xhr;
+          onError(err);
+          return;
+        }
         if (xhr.status >= 200 && xhr.status < 300) {
           timer.done();
           resolve(xhr);
@@ -51,16 +67,7 @@ export function fetcher(url, options) {
           reject(new NetworkError(xhr.statusText, xhr));
         }
       };
-      xhr.onerror = () => {
-        if (shouldRetry(xhr) && retryCount < retries) {
-          setTimeout(() => {
-            requestWithRetry(retryCount + 1).then(resolve, reject);
-          }, delay(retryCount));
-        } else {
-          timer.done();
-          reject(new NetworkError(xhr.statusText, xhr));
-        }
-      };
+      xhr.onerror = () => onError(new NetworkError(xhr.statusText, xhr));
       xhr.upload.onprogress = event => {
         timer.progress();
         onUploadProgress(event);
diff --git a/packages/@uppy/xhr-upload/lib/index.js b/packages/@uppy/xhr-upload/lib/index.js
index fd18a3d..323b88e 100644
--- a/packages/@uppy/xhr-upload/lib/index.js
+++ b/packages/@uppy/xhr-upload/lib/index.js
@@ -202,11 +202,9 @@ export default class XHRUpload extends BasePlugin {
           if (error.name === "AbortError") {
             return undefined;
           }
-          if (error instanceof NetworkError) {
-            const request = error.request;
-            for (const file of files) {
-              this.uppy.emit("upload-error", this.uppy.getFile(file.id), buildResponseError(request, error), request);
-            }
+          const request = error.request;
+          for (const file of files) {
+            this.uppy.emit("upload-error", this.uppy.getFile(file.id), buildResponseError(request, error), request);
           }
           throw error;
         }

@Murderlon Murderlon merged commit f3a750d into main Jan 7, 2025
16 checks passed
@Murderlon Murderlon deleted the xhr-custom-error branch January 7, 2025 13:49
@tagliala
Copy link

tagliala commented Jan 7, 2025

Thank you for the prompt response! I will test it as soon as possible

@Murderlon
Copy link
Member Author

Not released yet but soon

github-actions bot added a commit that referenced this pull request Jan 8, 2025
| Package                    | Version | Package                    | Version |
| -------------------------- | ------- | -------------------------- | ------- |
| @uppy/google-drive-picker  |   0.3.1 | @uppy/unsplash             |   4.3.0 |
| @uppy/google-photos-picker |   0.3.1 | @uppy/utils                |   6.1.1 |
| @uppy/onedrive             |   4.2.1 | @uppy/xhr-upload           |   4.3.1 |
| @uppy/provider-views       |   4.4.0 | uppy                       |  4.12.0 |
| @uppy/svelte               |   4.3.0 |                            |         |

- @uppy/unsplash,@uppy/provider-views: add utmSource option (Merlijn Vos / #5580)
- @uppy/xhr-upload: allow custom error message in onAfterResponse (Merlijn Vos / #5578)
- @uppy/onedrive: fix AsyncStore import (Merlijn Vos / #5579)
- @uppy/google-drive-picker,@uppy/google-photos-picker: Fix Google Picker plugins locale (Merlijn Vos / #5575)
@tagliala
Copy link

tagliala commented Jan 8, 2025

image

Confirmed working, thanks again. I will close the discussion and add information there

      onAfterResponse (response) {
        if (response.status !== 422) return

        throw new Error(JSON.parse(response.responseText).error)
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants