Skip to content

Commit be65f62

Browse files
authored
Merge pull request #192 from rpgtkoolmv/error_detail
Show where the error is caused and let show/hide the detail of error
2 parents d2b1858 + 14e55fe commit be65f62

File tree

12 files changed

+224
-38
lines changed

12 files changed

+224
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ game/**/*
5555
todo.md
5656
corescript/**/*
5757
corescript.zip
58+
package-lock.json

js/rpg_core/Graphics.js

Lines changed: 110 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ Graphics.printLoadingError = function(url) {
360360
if (this._errorPrinter && !this._errorShowed) {
361361
this._updateErrorPrinter();
362362
this._errorPrinter.innerHTML = this._makeErrorHtml('Loading Error', 'Failed to load: ' + url);
363+
this._errorPrinter.style.userSelect = 'text';
364+
this._errorPrinter.style.webkitUserSelect = 'text';
365+
this._errorPrinter.style.msUserSelect = 'text';
366+
this._errorPrinter.style.mozUserSelect = 'text';
367+
this._errorPrinter.oncontextmenu = null; // enable context menu
363368
var button = document.createElement('button');
364369
button.innerHTML = 'Retry';
365370
button.style.fontSize = '24px';
@@ -385,6 +390,11 @@ Graphics.printLoadingError = function(url) {
385390
Graphics.eraseLoadingError = function() {
386391
if (this._errorPrinter && !this._errorShowed) {
387392
this._errorPrinter.innerHTML = '';
393+
this._errorPrinter.style.userSelect = 'none';
394+
this._errorPrinter.style.webkitUserSelect = 'none';
395+
this._errorPrinter.style.msUserSelect = 'none';
396+
this._errorPrinter.style.mozUserSelect = 'none';
397+
this._errorPrinter.oncontextmenu = function() { return false; };
388398
this.startLoading();
389399
}
390400
};
@@ -405,27 +415,32 @@ Graphics.printError = function(name, message) {
405415
if (this._errorPrinter) {
406416
this._updateErrorPrinter();
407417
this._errorPrinter.innerHTML = this._makeErrorHtml(name, message);
408-
this._makeErrorMessage();
418+
this._errorPrinter.style.userSelect = 'text';
419+
this._errorPrinter.style.webkitUserSelect = 'text';
420+
this._errorPrinter.style.msUserSelect = 'text';
421+
this._errorPrinter.style.mozUserSelect = 'text';
422+
this._errorPrinter.oncontextmenu = null; // enable context menu
423+
if (this._errorMessage) {
424+
this._makeErrorMessage();
425+
}
409426
}
410427
this._applyCanvasFilter();
411428
this._clearUpperCanvas();
412429
};
413430

414431
/**
415-
* Shows the stacktrace of error.
432+
* Shows the detail of error.
416433
*
417434
* @static
418-
* @method printStackTrace
435+
* @method printErrorDetail
419436
*/
420-
Graphics.printStackTrace = function(stack) {
421-
if (this._errorPrinter) {
422-
stack = (stack || '')
423-
.replace(/file:.*js\//g, '')
424-
.replace(/http:.*js\//g, '')
425-
.replace(/https:.*js\//g, '')
426-
.replace(/chrome-extension:.*js\//g, '')
427-
.replace(/\n/g, '<br>');
428-
this._makeStackTrace(decodeURIComponent(stack));
437+
Graphics.printErrorDetail = function(error) {
438+
if (this._errorPrinter && this._showErrorDetail) {
439+
var eventInfo = this._formatEventInfo(error);
440+
var eventCommandInfo = this._formatEventCommandInfo(error);
441+
var info = eventCommandInfo ? eventInfo + ", " + eventCommandInfo : eventInfo;
442+
var stack = this._formatStackTrace(error);
443+
this._makeErrorDetail(info, stack);
429444
}
430445
};
431446

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

457+
/**
458+
* Sets whether shows the detail of error.
459+
*
460+
* @static
461+
* @method setShowErrorDetail
462+
*/
463+
Graphics.setShowErrorDetail = function(showErrorDetail) {
464+
this._showErrorDetail = showErrorDetail;
465+
};
466+
442467
/**
443468
* Shows the FPSMeter element.
444469
*
@@ -862,16 +887,17 @@ Graphics._createErrorPrinter = function() {
862887
*/
863888
Graphics._updateErrorPrinter = function() {
864889
this._errorPrinter.width = this._width * 0.9;
865-
this._errorPrinter.height = this._errorShowed ? this._height * 0.9 : 40;
890+
if (this._errorShowed && this._showErrorDetail) {
891+
this._errorPrinter.height = this._height * 0.9;
892+
} else if (this._errorShowed && this._errorMessage) {
893+
this._errorPrinter.height = 100;
894+
} else {
895+
this._errorPrinter.height = 40;
896+
}
866897
this._errorPrinter.style.textAlign = 'center';
867898
this._errorPrinter.style.textShadow = '1px 1px 3px #000';
868899
this._errorPrinter.style.fontSize = '20px';
869900
this._errorPrinter.style.zIndex = 99;
870-
this._errorPrinter.style.userSelect = 'text';
871-
this._errorPrinter.style.webkitUserSelect = 'text';
872-
this._errorPrinter.style.msUserSelect = 'text';
873-
this._errorPrinter.style.mozUserSelect = 'text';
874-
this._errorPrinter.oncontextmenu = null; // enable context menu
875901
this._centerElement(this._errorPrinter);
876902
};
877903

@@ -886,23 +912,82 @@ Graphics._makeErrorMessage = function() {
886912
style.color = 'white';
887913
style.textAlign = 'left';
888914
style.fontSize = '18px';
889-
mainMessage.innerHTML = '<hr>' + (this._errorMessage || '');
915+
mainMessage.innerHTML = '<hr>' + this._errorMessage;
890916
this._errorPrinter.appendChild(mainMessage);
891917
};
892918

893919
/**
894920
* @static
895-
* @method _makeStackTrace
921+
* @method _makeErrorDetail
896922
* @private
897923
*/
898-
Graphics._makeStackTrace = function(stack) {
899-
var stackTrace = document.createElement('div');
900-
var style = stackTrace.style;
924+
Graphics._makeErrorDetail = function(info, stack) {
925+
var detail = document.createElement('div');
926+
var style = detail.style;
901927
style.color = 'white';
902928
style.textAlign = 'left';
903929
style.fontSize = '18px';
904-
stackTrace.innerHTML = '<br><hr>' + stack + '<hr>';
905-
this._errorPrinter.appendChild(stackTrace);
930+
detail.innerHTML = '<br><hr>' + info + '<br><br>' + stack;
931+
this._errorPrinter.appendChild(detail);
932+
};
933+
934+
/**
935+
* @static
936+
* @method _formatEventInfo
937+
* @private
938+
*/
939+
Graphics._formatEventInfo = function(error) {
940+
switch (String(error.eventType)) {
941+
case "map_event":
942+
return "MapID: %1, MapEventID: %2, page: %3, line: %4".format(error.mapId, error.mapEventId, error.page, error.line);
943+
case "common_event":
944+
return "CommonEventID: %1, line: %2".format(error.commonEventId, error.line);
945+
case "battle_event":
946+
return "TroopID: %1, page: %2, line: %3".format(error.troopId, error.page, error.line);
947+
case "test_event":
948+
return "TestEvent, line: %1".format(error.line);
949+
default:
950+
return "No information";
951+
}
952+
};
953+
954+
/**
955+
* @static
956+
* @method _formatEventCommandInfo
957+
* @private
958+
*/
959+
Graphics._formatEventCommandInfo = function(error) {
960+
switch (String(error.eventCommand)) {
961+
case "plugin_command":
962+
return "◆Plugin Command: " + error.content;
963+
case "script":
964+
return "◆Script: " + error.content;
965+
case "control_variables":
966+
return "◆Control Variables: Script: " + error.content;
967+
case "conditional_branch_script":
968+
return "◆If: Script: " + error.content;
969+
case "set_route_script":
970+
return "◆Set Movement Route: ◇Script: " + error.content;
971+
case "auto_route_script":
972+
return "Autonomous Movement Custom Route: ◇Script: " + error.content;
973+
case "other":
974+
default:
975+
return "";
976+
}
977+
};
978+
979+
/**
980+
* @static
981+
* @method _formatStackTrace
982+
* @private
983+
*/
984+
Graphics._formatStackTrace = function(error) {
985+
return decodeURIComponent((error.stack || '')
986+
.replace(/file:.*js\//g, '')
987+
.replace(/http:.*js\//g, '')
988+
.replace(/https:.*js\//g, '')
989+
.replace(/chrome-extension:.*js\//g, '')
990+
.replace(/\n/g, '<br>'));
906991
};
907992

908993
/**

js/rpg_managers/SceneManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ SceneManager.onKeyDown = function(event) {
200200
SceneManager.catchException = function(e) {
201201
if (e instanceof Error) {
202202
Graphics.printError(e.name, e.message);
203-
Graphics.printStackTrace(e.stack);
203+
Graphics.printErrorDetail(e);
204204
console.error(e.stack);
205205
} else {
206206
Graphics.printError('UnknownError', e);

js/rpg_objects/Game_Character.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Game_Character.prototype.initMembers = function() {
6969
this._originalMoveRoute = null;
7070
this._originalMoveRouteIndex = 0;
7171
this._waitCount = 0;
72+
this._callerEventInfo = null;
7273
};
7374

7475
Game_Character.prototype.memorizeMoveRoute = function() {
@@ -80,6 +81,7 @@ Game_Character.prototype.restoreMoveRoute = function() {
8081
this._moveRoute = this._originalMoveRoute;
8182
this._moveRouteIndex = this._originalMoveRouteIndex;
8283
this._originalMoveRoute = null;
84+
this._callerEventInfo = null;
8385
};
8486

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

107+
Game_Character.prototype.setCallerEventInfo = function(callerEventInfo) {
108+
this._callerEventInfo = callerEventInfo;
109+
};
110+
105111
Game_Character.prototype.updateStop = function() {
106112
Game_CharacterBase.prototype.updateStop.call(this);
107113
if (this._moveRouteForcing) {
@@ -262,7 +268,27 @@ Game_Character.prototype.processMoveCommand = function(command) {
262268
AudioManager.playSe(params[0]);
263269
break;
264270
case gc.ROUTE_SCRIPT:
265-
eval(params[0]);
271+
try {
272+
eval(params[0]);
273+
} catch (error) {
274+
if (this._callerEventInfo) {
275+
for (var key in this._callerEventInfo) {
276+
error[key] = this._callerEventInfo[key];
277+
}
278+
error.line += this._moveRouteIndex + 1;
279+
error.eventCommand = "set_route_script";
280+
error.content = command.parameters[0];
281+
} else {
282+
error.eventType = "map_event";
283+
error.mapId = this._mapId;
284+
error.mapEventId = this._eventId;
285+
error.page = this._pageIndex + 1;
286+
error.line = this._moveRouteIndex + 1;
287+
error.eventCommand = "auto_route_script";
288+
error.content = command.parameters[0];
289+
}
290+
throw error;
291+
}
266292
break;
267293
}
268294
};

js/rpg_objects/Game_CommonEvent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Game_CommonEvent.prototype.update = function() {
4040
if (this._interpreter) {
4141
if (!this._interpreter.isRunning()) {
4242
this._interpreter.setup(this.list());
43+
this._interpreter.setEventInfo({ eventType: 'common_event', commonEventId: this._commonEventId });
4344
}
4445
this._interpreter.update();
4546
}

js/rpg_objects/Game_Event.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ Game_Event.prototype.updateParallel = function() {
322322
if (this._interpreter) {
323323
if (!this._interpreter.isRunning()) {
324324
this._interpreter.setup(this.list(), this._eventId);
325+
this._interpreter.setEventInfo(this.getEventInfo());
325326
}
326327
this._interpreter.update();
327328
}
@@ -336,3 +337,7 @@ Game_Event.prototype.forceMoveRoute = function(moveRoute) {
336337
Game_Character.prototype.forceMoveRoute.call(this, moveRoute);
337338
this._prelockDirection = 0;
338339
};
340+
341+
Game_Event.prototype.getEventInfo = function() {
342+
return { eventType: "map_event", mapId: this._mapId, mapEventId: this._eventId, page: this._pageIndex + 1 };
343+
};

0 commit comments

Comments
 (0)