Skip to content

Commit

Permalink
Merge pull request #192 from rpgtkoolmv/error_detail
Browse files Browse the repository at this point in the history
Show where the error is caused and let show/hide the detail of error
  • Loading branch information
krmbn0576 authored Dec 17, 2018
2 parents d2b1858 + 14e55fe commit be65f62
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ game/**/*
todo.md
corescript/**/*
corescript.zip
package-lock.json
135 changes: 110 additions & 25 deletions js/rpg_core/Graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ Graphics.printLoadingError = function(url) {
if (this._errorPrinter && !this._errorShowed) {
this._updateErrorPrinter();
this._errorPrinter.innerHTML = this._makeErrorHtml('Loading Error', 'Failed to load: ' + url);
this._errorPrinter.style.userSelect = 'text';
this._errorPrinter.style.webkitUserSelect = 'text';
this._errorPrinter.style.msUserSelect = 'text';
this._errorPrinter.style.mozUserSelect = 'text';
this._errorPrinter.oncontextmenu = null; // enable context menu
var button = document.createElement('button');
button.innerHTML = 'Retry';
button.style.fontSize = '24px';
Expand All @@ -385,6 +390,11 @@ Graphics.printLoadingError = function(url) {
Graphics.eraseLoadingError = function() {
if (this._errorPrinter && !this._errorShowed) {
this._errorPrinter.innerHTML = '';
this._errorPrinter.style.userSelect = 'none';
this._errorPrinter.style.webkitUserSelect = 'none';
this._errorPrinter.style.msUserSelect = 'none';
this._errorPrinter.style.mozUserSelect = 'none';
this._errorPrinter.oncontextmenu = function() { return false; };
this.startLoading();
}
};
Expand All @@ -405,27 +415,32 @@ Graphics.printError = function(name, message) {
if (this._errorPrinter) {
this._updateErrorPrinter();
this._errorPrinter.innerHTML = this._makeErrorHtml(name, message);
this._makeErrorMessage();
this._errorPrinter.style.userSelect = 'text';
this._errorPrinter.style.webkitUserSelect = 'text';
this._errorPrinter.style.msUserSelect = 'text';
this._errorPrinter.style.mozUserSelect = 'text';
this._errorPrinter.oncontextmenu = null; // enable context menu
if (this._errorMessage) {
this._makeErrorMessage();
}
}
this._applyCanvasFilter();
this._clearUpperCanvas();
};

/**
* Shows the stacktrace of error.
* Shows the detail of error.
*
* @static
* @method printStackTrace
* @method printErrorDetail
*/
Graphics.printStackTrace = function(stack) {
if (this._errorPrinter) {
stack = (stack || '')
.replace(/file:.*js\//g, '')
.replace(/http:.*js\//g, '')
.replace(/https:.*js\//g, '')
.replace(/chrome-extension:.*js\//g, '')
.replace(/\n/g, '<br>');
this._makeStackTrace(decodeURIComponent(stack));
Graphics.printErrorDetail = function(error) {
if (this._errorPrinter && this._showErrorDetail) {
var eventInfo = this._formatEventInfo(error);
var eventCommandInfo = this._formatEventCommandInfo(error);
var info = eventCommandInfo ? eventInfo + ", " + eventCommandInfo : eventInfo;
var stack = this._formatStackTrace(error);
this._makeErrorDetail(info, stack);
}
};

Expand All @@ -439,6 +454,16 @@ Graphics.setErrorMessage = function(message) {
this._errorMessage = message;
};

/**
* Sets whether shows the detail of error.
*
* @static
* @method setShowErrorDetail
*/
Graphics.setShowErrorDetail = function(showErrorDetail) {
this._showErrorDetail = showErrorDetail;
};

/**
* Shows the FPSMeter element.
*
Expand Down Expand Up @@ -862,16 +887,17 @@ Graphics._createErrorPrinter = function() {
*/
Graphics._updateErrorPrinter = function() {
this._errorPrinter.width = this._width * 0.9;
this._errorPrinter.height = this._errorShowed ? this._height * 0.9 : 40;
if (this._errorShowed && this._showErrorDetail) {
this._errorPrinter.height = this._height * 0.9;
} else if (this._errorShowed && this._errorMessage) {
this._errorPrinter.height = 100;
} else {
this._errorPrinter.height = 40;
}
this._errorPrinter.style.textAlign = 'center';
this._errorPrinter.style.textShadow = '1px 1px 3px #000';
this._errorPrinter.style.fontSize = '20px';
this._errorPrinter.style.zIndex = 99;
this._errorPrinter.style.userSelect = 'text';
this._errorPrinter.style.webkitUserSelect = 'text';
this._errorPrinter.style.msUserSelect = 'text';
this._errorPrinter.style.mozUserSelect = 'text';
this._errorPrinter.oncontextmenu = null; // enable context menu
this._centerElement(this._errorPrinter);
};

Expand All @@ -886,23 +912,82 @@ Graphics._makeErrorMessage = function() {
style.color = 'white';
style.textAlign = 'left';
style.fontSize = '18px';
mainMessage.innerHTML = '<hr>' + (this._errorMessage || '');
mainMessage.innerHTML = '<hr>' + this._errorMessage;
this._errorPrinter.appendChild(mainMessage);
};

/**
* @static
* @method _makeStackTrace
* @method _makeErrorDetail
* @private
*/
Graphics._makeStackTrace = function(stack) {
var stackTrace = document.createElement('div');
var style = stackTrace.style;
Graphics._makeErrorDetail = function(info, stack) {
var detail = document.createElement('div');
var style = detail.style;
style.color = 'white';
style.textAlign = 'left';
style.fontSize = '18px';
stackTrace.innerHTML = '<br><hr>' + stack + '<hr>';
this._errorPrinter.appendChild(stackTrace);
detail.innerHTML = '<br><hr>' + info + '<br><br>' + stack;
this._errorPrinter.appendChild(detail);
};

/**
* @static
* @method _formatEventInfo
* @private
*/
Graphics._formatEventInfo = function(error) {
switch (String(error.eventType)) {
case "map_event":
return "MapID: %1, MapEventID: %2, page: %3, line: %4".format(error.mapId, error.mapEventId, error.page, error.line);
case "common_event":
return "CommonEventID: %1, line: %2".format(error.commonEventId, error.line);
case "battle_event":
return "TroopID: %1, page: %2, line: %3".format(error.troopId, error.page, error.line);
case "test_event":
return "TestEvent, line: %1".format(error.line);
default:
return "No information";
}
};

/**
* @static
* @method _formatEventCommandInfo
* @private
*/
Graphics._formatEventCommandInfo = function(error) {
switch (String(error.eventCommand)) {
case "plugin_command":
return "◆Plugin Command: " + error.content;
case "script":
return "◆Script: " + error.content;
case "control_variables":
return "◆Control Variables: Script: " + error.content;
case "conditional_branch_script":
return "◆If: Script: " + error.content;
case "set_route_script":
return "◆Set Movement Route: ◇Script: " + error.content;
case "auto_route_script":
return "Autonomous Movement Custom Route: ◇Script: " + error.content;
case "other":
default:
return "";
}
};

/**
* @static
* @method _formatStackTrace
* @private
*/
Graphics._formatStackTrace = function(error) {
return decodeURIComponent((error.stack || '')
.replace(/file:.*js\//g, '')
.replace(/http:.*js\//g, '')
.replace(/https:.*js\//g, '')
.replace(/chrome-extension:.*js\//g, '')
.replace(/\n/g, '<br>'));
};

/**
Expand Down
2 changes: 1 addition & 1 deletion js/rpg_managers/SceneManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ SceneManager.onKeyDown = function(event) {
SceneManager.catchException = function(e) {
if (e instanceof Error) {
Graphics.printError(e.name, e.message);
Graphics.printStackTrace(e.stack);
Graphics.printErrorDetail(e);
console.error(e.stack);
} else {
Graphics.printError('UnknownError', e);
Expand Down
28 changes: 27 additions & 1 deletion js/rpg_objects/Game_Character.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Game_Character.prototype.initMembers = function() {
this._originalMoveRoute = null;
this._originalMoveRouteIndex = 0;
this._waitCount = 0;
this._callerEventInfo = null;
};

Game_Character.prototype.memorizeMoveRoute = function() {
Expand All @@ -80,6 +81,7 @@ Game_Character.prototype.restoreMoveRoute = function() {
this._moveRoute = this._originalMoveRoute;
this._moveRouteIndex = this._originalMoveRouteIndex;
this._originalMoveRoute = null;
this._callerEventInfo = null;
};

Game_Character.prototype.isMoveRouteForcing = function() {
Expand All @@ -102,6 +104,10 @@ Game_Character.prototype.forceMoveRoute = function(moveRoute) {
this._waitCount = 0;
};

Game_Character.prototype.setCallerEventInfo = function(callerEventInfo) {
this._callerEventInfo = callerEventInfo;
};

Game_Character.prototype.updateStop = function() {
Game_CharacterBase.prototype.updateStop.call(this);
if (this._moveRouteForcing) {
Expand Down Expand Up @@ -262,7 +268,27 @@ Game_Character.prototype.processMoveCommand = function(command) {
AudioManager.playSe(params[0]);
break;
case gc.ROUTE_SCRIPT:
eval(params[0]);
try {
eval(params[0]);
} catch (error) {
if (this._callerEventInfo) {
for (var key in this._callerEventInfo) {
error[key] = this._callerEventInfo[key];
}
error.line += this._moveRouteIndex + 1;
error.eventCommand = "set_route_script";
error.content = command.parameters[0];
} else {
error.eventType = "map_event";
error.mapId = this._mapId;
error.mapEventId = this._eventId;
error.page = this._pageIndex + 1;
error.line = this._moveRouteIndex + 1;
error.eventCommand = "auto_route_script";
error.content = command.parameters[0];
}
throw error;
}
break;
}
};
Expand Down
1 change: 1 addition & 0 deletions js/rpg_objects/Game_CommonEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Game_CommonEvent.prototype.update = function() {
if (this._interpreter) {
if (!this._interpreter.isRunning()) {
this._interpreter.setup(this.list());
this._interpreter.setEventInfo({ eventType: 'common_event', commonEventId: this._commonEventId });
}
this._interpreter.update();
}
Expand Down
5 changes: 5 additions & 0 deletions js/rpg_objects/Game_Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ Game_Event.prototype.updateParallel = function() {
if (this._interpreter) {
if (!this._interpreter.isRunning()) {
this._interpreter.setup(this.list(), this._eventId);
this._interpreter.setEventInfo(this.getEventInfo());
}
this._interpreter.update();
}
Expand All @@ -336,3 +337,7 @@ Game_Event.prototype.forceMoveRoute = function(moveRoute) {
Game_Character.prototype.forceMoveRoute.call(this, moveRoute);
this._prelockDirection = 0;
};

Game_Event.prototype.getEventInfo = function() {
return { eventType: "map_event", mapId: this._mapId, mapEventId: this._eventId, page: this._pageIndex + 1 };
};
Loading

0 comments on commit be65f62

Please sign in to comment.