Skip to content

Commit

Permalink
Improve dmloader error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ekharkunov committed May 8, 2024
1 parent 5b730aa commit d679aa4
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ var FileLoader = {
},
// do xhr request with retries
request: function(url, method, responseType, currentAttempt) {
if (typeof method === 'undefined') throw "No method specified";
if (typeof method === 'responseType') throw "No responseType specified";
if (typeof method === 'undefined') throw TypeError("No method specified");
if (typeof method === 'responseType') throw TypeError("No responseType specified");
if (typeof currentAttempt === 'undefined') currentAttempt = 0;
var obj = {
send: function() {
Expand Down Expand Up @@ -256,6 +256,9 @@ var EngineLoader = {
},
function(error) { throw error; },
function(wasm) {
if (wasm.byteLength != EngineLoader.wasm_size) {
throw "Invalid wasm size. Expected: " + EngineLoader.wasm_size + ", actual: " + wasm.byteLength;
}
var wasmInstantiate = WebAssembly.instantiate(new Uint8Array(wasm), imports).then(function(output) {
successCallback(output.instance);
}).catch(function(e) {
Expand Down Expand Up @@ -356,7 +359,7 @@ var EngineLoader = {
GameArchiveLoader.loadArchiveDescription('/archive_files.json');

// move resize callback setup here to make possible to override callback
// from outside of dmlodaer.js
// from outside of dmloader.js
if (typeof CUSTOM_PARAMETERS["resize_window_callback"] === "function") {
var callback = CUSTOM_PARAMETERS["resize_window_callback"]
callback();
Expand Down Expand Up @@ -406,7 +409,7 @@ var GameArchiveLoader = {
},

addListener: function(list, callback) {
if (typeof callback !== 'function') throw "Invalid callback registration";
if (typeof callback !== 'function') throw TypeError("Invalid callback registration");
list.push(callback);
},
notifyListeners: function(list, data) {
Expand Down Expand Up @@ -498,7 +501,7 @@ var GameArchiveLoader = {

downloadPiece: function(file, index) {
if (index < file.lastRequestedPiece) {
throw "Request out of order";
throw RangeError("Request out of order: " + file.name + ", index: " + index + ", last requestd piece: " + file.lastRequestedPiece);
}

var piece = file.pieces[index];
Expand Down Expand Up @@ -535,10 +538,10 @@ var GameArchiveLoader = {
var start = piece.offset;
var end = start + piece.data.length;
if (0 > start) {
throw "Buffer underflow";
throw RangeError("Buffer underflow. Start: " + start);
}
if (end > file.data.length) {
throw "Buffer overflow";
throw RangeError("Buffer overflow. End : " + end + ", data length: " + file.data.length);
}
file.data.set(piece.data, piece.offset);
}
Expand Down Expand Up @@ -569,7 +572,7 @@ var GameArchiveLoader = {
actualSize += file.pieces[i].dataLength;
}
if (actualSize != file.size) {
throw "Unexpected data size";
throw "Unexpected data size: " + file.name + ", expected size: " + file.size + ", actual size: " + actualSize;
}

// verify the pieces
Expand All @@ -583,13 +586,13 @@ var GameArchiveLoader = {
if (0 < i) {
var previous = pieces[i - 1];
if (previous.offset + previous.dataLength > start) {
throw "Segment underflow";
throw RangeError("Segment underflow in file: " + file.name + ", offset: " + previous.offset + previous.dataLength + " , start: " + start);
}
}
if (pieces.length - 2 > i) {
var next = pieces[i + 1];
if (end > next.offset) {
throw "Segment overflow";
throw RangeError("Segment overflow in file: " + file.name + ", offset: " + next.offset + ", end: " + end);
}
}
}
Expand Down Expand Up @@ -652,7 +655,7 @@ var ProgressUpdater = {
listeners: [],

addListener: function(callback) {
if (typeof callback !== 'function') throw "Invalid callback registration";
if (typeof callback !== 'function') throw TypeError("Invalid callback registration");
this.listeners.push(callback);
},

Expand Down

0 comments on commit d679aa4

Please sign in to comment.