Browse Source

fix(Ajax Timeout): Ajax timeout was not handled for IE and node

#479
Benjamin Giesinger 6 years ago
parent
commit
2a66245f43
2 changed files with 24 additions and 7 deletions
  1. 5 1
      src/OpenSheetMusicDisplay/AJAX.ts
  2. 19 6
      test/Common/OSMD/OSMD_Test.ts

+ 5 - 1
src/OpenSheetMusicDisplay/AJAX.ts

@@ -29,10 +29,14 @@ export class AJAX {
                         resolve(xhttp.responseText);
                     } else {
                         //reject(new Error("AJAX error: '" + xhttp.statusText + "'"));
-                        reject(new Error("Could not retrieve requested URL"));
+                        reject(new Error("Could not retrieve requested URL " + xhttp.status));
                     }
                 }
             };
+            xhttp.ontimeout = (e) => {
+                // For IE and node
+                reject(new Error("Server request Timeout"));
+            };
             xhttp.overrideMimeType(mimeType);
             xhttp.open("GET", url, true);
             xhttp.send();

+ 19 - 6
test/Common/OSMD/OSMD_Test.ts

@@ -1,6 +1,6 @@
 import chai = require("chai");
-import {OpenSheetMusicDisplay} from "../../../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay";
-import {TestUtils} from "../../Util/TestUtils";
+import { OpenSheetMusicDisplay } from "../../../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay";
+import { TestUtils } from "../../Util/TestUtils";
 
 describe("OpenSheetMusicDisplay Main Export", () => {
     let container1: HTMLElement;
@@ -59,7 +59,6 @@ describe("OpenSheetMusicDisplay Main Export", () => {
         opensheetmusicdisplay.load(xml).then(
             (_: {}) => {
                 opensheetmusicdisplay.render();
-                done();
             },
             done
         );
@@ -78,6 +77,20 @@ describe("OpenSheetMusicDisplay Main Export", () => {
         );
     });
 
+    it("Timeout from server", (done: MochaDone) => {
+        const score: string = "https://httpstat.us/408";
+        const div: HTMLElement = TestUtils.getDivElement(document);
+        const opensheetmusicdisplay: OpenSheetMusicDisplay = TestUtils.createOpenSheetMusicDisplay(div);
+        opensheetmusicdisplay.load(score).then(
+            (_: {}) => {
+                done(new Error("Unexpected response from server"));
+            },
+            (exc: Error) => {
+                done();
+            }
+        );
+    });
+
     it("load MXL Document by URL", (done: MochaDone) => {
         const url: string = "base/test/data/Mozart_Clarinet_Quintet_Excerpt.mxl";
         const div: HTMLElement = TestUtils.getDivElement(document);
@@ -201,9 +214,9 @@ describe("OpenSheetMusicDisplay Main Export", () => {
             const score: Document =
                 TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
             return osmd.load(score)
-            .then(() => {
-                osmd.render();
-            });
+                .then(() => {
+                    osmd.render();
+                });
         });
 
         it("should move cursor after instrument is hidden", () => {