This is an automated email from the ASF dual-hosted git repository.

amaranhao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-fauxton.git


The following commit(s) were added to refs/heads/master by this push:
     new e9ae32c  Show error reason if file upload fails (#1279)
e9ae32c is described below

commit e9ae32cb7f93abab63416b70d6fbd01106681040
Author: Antonio Maranhao <30349380+antonio-maran...@users.noreply.github.com>
AuthorDate: Tue May 19 09:48:51 2020 -0400

    Show error reason if file upload fails (#1279)
---
 .../__tests__/doc-editor.actions.test.js           | 63 ++++++++++++++++++++++
 app/addons/documents/doc-editor/actions.js         | 12 ++---
 2 files changed, 69 insertions(+), 6 deletions(-)

diff --git 
a/app/addons/documents/doc-editor/__tests__/doc-editor.actions.test.js 
b/app/addons/documents/doc-editor/__tests__/doc-editor.actions.test.js
index 425a27f..9cfc505 100644
--- a/app/addons/documents/doc-editor/__tests__/doc-editor.actions.test.js
+++ b/app/addons/documents/doc-editor/__tests__/doc-editor.actions.test.js
@@ -66,4 +66,67 @@ describe('DocEditorActions', () => {
     );
   });
 
+  it('uploadAttachment shows error reason, if available', () => {
+    sinon.stub(FauxtonAPI, 'addNotification');
+    sinon.stub(FauxtonAPI, 'urls').callsFake((p1, p2, p3, p4, p5, p6) => {
+      return [p1, p2, p3, p4, p5, p6].join('/');
+    });
+    const params = {
+      rev: 'rev-num',
+      doc: doc,
+      files: [
+        {
+          name: 'a_file.txt',
+          length: 100
+        }
+      ]
+    };
+    const mockDispatch = sinon.stub();
+
+    const fakeRequest = {
+      send: sinon.stub(),
+      setRequestHeader: sinon.stub(),
+      open: sinon.stub()
+    };
+    fakeXMLHttpRequest.returns(fakeRequest);
+
+    // Call uploadAttachment to attach the event handlers to fakeRequest
+    Actions.uploadAttachment(params)(mockDispatch);
+    expect(fakeRequest.onload).toBeDefined();
+    const mockError = {
+      error: 'bad_request',
+      reason: 'Invalid filename'
+    };
+    // Simulate an error response
+    fakeRequest.responseText = JSON.stringify(mockError);
+    fakeRequest.status = 400;
+    mockDispatch.resetHistory();
+    // Call the onload event
+    fakeRequest.onload();
+    // Verify it includes the error details
+    sinon.assert.calledWithExactly(
+      mockDispatch,
+      {
+        options: { error: `Error uploading file. Reason: ${mockError.reason}` 
},
+        type: 'FILE_UPLOAD_ERROR'
+      }
+    );
+
+    // Make sure it calls doesn't crash if response is not JSON
+    // Simulate an error response
+    fakeRequest.responseText = 'Forbidden';
+    fakeRequest.status = 403;
+    mockDispatch.resetHistory();
+    // Call the onload event
+    fakeRequest.onload();
+    // Verify it displays the error message, without any details
+    sinon.assert.calledWithExactly(
+      mockDispatch,
+      {
+        options: { error: `Error uploading file. ` },
+        type: 'FILE_UPLOAD_ERROR'
+      }
+    );
+  });
+
 });
diff --git a/app/addons/documents/doc-editor/actions.js 
b/app/addons/documents/doc-editor/actions.js
index d04371d..95ce259 100644
--- a/app/addons/documents/doc-editor/actions.js
+++ b/app/addons/documents/doc-editor/actions.js
@@ -207,20 +207,20 @@ const uploadAttachment = (params) => (dispatch) => {
   httpRequest.onerror = () => {
     onError('Error uploading file');
   };
-  httpRequest.onload = (e) => {
+  httpRequest.onload = () => {
     if (httpRequest.status >= 200 && httpRequest.status < 300) {
       onSuccess(params.doc);
     } else {
       let errorMsg = 'Error uploading file. ';
-      if (e.responseText) {
-        try {
-          const json = JSON.parse(e.responseText);
+      try {
+        if (httpRequest.responseText) {
+          const json = JSON.parse(httpRequest.responseText);
           if (json.error) {
             errorMsg += 'Reason: ' + (json.reason || json.error);
           }
-        } catch (err) {
-          //ignore parsing error
         }
+      } catch (err) {
+        //ignore parsing error
       }
       onError(errorMsg);
     }

Reply via email to