-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorreport.js
76 lines (68 loc) · 3.42 KB
/
errorreport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// (C) Copyright 2015 Martin Dougiamas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// We use this code for handling unexpected errors.
// Using JS confirm function we are sure that the user get notified in a Mobile device.
// This script should be added at the begining of the index.html and it should only use native javascript functions.
window.onerror = function(msg, url, lineNumber) {
var errorReported = false;
function reportError() {
if (!errorReported) {
errorReported = true;
var reportUrl = 'http://prototype.moodle.net/mobile/feedback/mmfeedback.php?message=' + encodeURIComponent(msg) +
'&file=' + encodeURIComponent(url) + '&line=' + lineNumber;
if (window.device) {
reportUrl = reportUrl + '&platform=' + window.device.platform + '&model=' + encodeURIComponent(window.device.model) +
'&osversion=' + window.device.version + '&cordova=' + window.device.cordova;
}
if (window.location) {
reportUrl = reportUrl + '&localurl=' + encodeURIComponent(window.location.href);
}
if (navigator.userAgent) {
reportUrl = reportUrl + '&useragent=' + encodeURIComponent(navigator.userAgent);
}
if (ydn.db.Storage) {
// Detect Storage type by default.
var db = new ydn.db.Storage('test', {}, {});
if (db && db.getType) {
reportUrl = reportUrl + '&storage=' + encodeURIComponent(db.getType());
}
}
window.open(reportUrl, '_system');
}
}
if (msg.indexOf('Can\'t find variable: cordova') == -1) {
// Use setTimeout to prevent the following error if the app crashes right at the start:
// "The connection to the server was unsuccessful. (file:///android_asset/www/index.html)"
setTimeout(function() {
if (typeof msg == "string") {
var sendError,
confirmmsg = 'Unexpected error, please accept to report the bug so we can work on fixing it ' +
'(Internet connection required).';
sendError = confirm(confirmmsg);
if (sendError) {
// Wait for device ready so we can retrieve device data. In most cases device will already be ready.
document.addEventListener('deviceready', reportError);
// Report error if device ready isn't fired after 5 seconds.
setTimeout(reportError, 5000);
}
}
// This may help debugging if we use logging apps in iOs or Android.
if (typeof console != "undefined" && typeof console.log == "function") {
console.log(msg);
}
}, 100);
}
// Let default error handler run.
return false;
};