I am developing a SAPUI5-App. Is there a way to show all errors directly to the customer without having to put a try-catch-block into every callback of sapui5? He will use my app in a mobile device and wouldn´t be able to see the log.
I tried already the following code:
<script type="text/javascript">
window.onerror = function(msg, url, line, col, error) {
var extra = !col ? '' : '\ncolumn: ' + col;
extra += !error ? '' : '\nerror: ' + error;
alert("Error: " + msg + "\nurl: " + url + "\nline: " + line + extra);
return false; //true = suppress error alert
};
window.addEventListener("error", handleError, true);
function handleError(evt) {
if (evt.message) {
alert("error: "+evt.message +" at linenumber: "+evt.lineno+" of file: "+evt.filename);
} else {
alert("error: "+evt.type+" from element: "+(evt.srcElement || evt.target));
}
}
jQuery.sap.log.addLogListener({
onLogEntry : function(oLogEntry) {
if(oLogEntry.level == '1'){
alert(oLogEntry.details + ' - '+oLogEntry.message);
}
}});
</script>
But I like to actually copy the error-message from the log into an alert for the customer, so he can send me screenshots of the error in his device.
All my attempts did not show enough information or didn´t fire on every error in the console-log.
Related
My work Outlook environment is changing from online (continuous connection) to cached mode and that breaks the desktop component of an Outlook add-in designed to save and then forward draft emails to an in-house security check.
My original code would save the draft async, then use the Id and change key to then forward it. In cached mode, there is no Id until the account synchs with the Exchange.
Now broken code (does not work in cached mode)
//Save current item as draft
mailForward.SaveDraftIfNecessary = function SaveDraftIfNecessary() {
if (_item_id == null || _item_id == undefined) {
Office.context.mailbox.item.saveAsync(function (asyncResult) {
if (asyncResult.error) {
app.showNotification(_appName, "Error: " + asyncResult.error.message);
}
else {
_item_id = asyncResult.value;
}
});
}
};
I've been able to write code that can replicate the mail and send it, but I need to be able to insert the draft body into the new email body, but it does not accept it. It does not work for text or HTML.
The code for getting the body type and text is as follows:
item.body.getTypeAsync(
function (asyncResultGet) {
if (asyncResultGet.status === Office.AsyncResultStatus.Failed) {
statusUpdate("icon16", asyncResultGet.error.message);
stopSpinner();
}
else {
bodyType = asyncResultGet.value;
item.body.getAsync(bodyType,
function (asyncResultGet) {
if (asyncResultGet.status === Office.AsyncResultStatus.Failed) {
statusUpdate("icon16", asyncResultGet.error.message);
stopSpinner();
}
else {
bodyText = asyncResultGet.value;
The SOAP that I am trying to use to insert the body text. I have also tried t:NewBodyContent instead of t:Body:
' <t:Message>' +
' <t:Subject>' + _subject + '</t:Subject>' +
' <t:Body BodyType="'+ _bodyType +'">' + _bodyText + '</t:Body>' +
' <t:ToRecipients>' + _adddressesSoap + '</t:ToRecipients>' +
' </t:Message>' +
My issue was encoding. I had an inkling that might be the problem, but didn't test it out until I found this link:
HTML-encoding lost when attribute read from input field
The code I used is this:
var htmlEncode = function htmlEncode(s) {
var ntable = {
"&": "amp",
"<": "lt",
">": "gt",
"\"": "quot"
};
s = s.replace(/[&<>"]/g, function (ch) {
return "&" + ntable[ch] + ";";
});
s = s.replace(/[^ -\x7e]/g, function (ch) {
return "&#" + ch.charCodeAt(0).toString() + ";";
});
return s;
};
Thanks to the help I received here, I have a piece of javascript that toggles the visibility of a div and loads content from a php file into that toggled div:
function compare_toggle_visibility(id, line, collection)
{
var e = document.getElementById(id);
e.style.display = ((e.style.display!='none') ? 'none' : 'block');
$(e).load('http:/www.minorworksoflydgate.net/XML/XQuery/test_command_line.php'+ '?collection=' + collection + '&zone=' + id + '&line=' + line, function(responseTxt, statusTxt, xhr){
if(statusTxt == "success")
alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: " + xhr.status + ": " + xhr.statusText);
});
}
This works great with the small php files I'm using for testing, but the problem I'm running into is that my actual production file takes some time to load. So the div is toggled and exists before the code can be loaded into it. I get the alert message I put in as a test indicating that the external content loaded, but I see nothing in the div itself.
To try to fix this, I've attempted to place the toggle after the load function, but that made no difference. I also have tried to load the results of the php call into a variable and then call the results of that variable into the div when it loaded successfully, but that does not work either.
function compare_toggle_visibility(id, line, collection)
{
var e = document.getElementById(id);
e.style.display = ((e.style.display!='none') ? 'none' : 'block');
var l = load('http:/www.minorworksoflydgate.net/XML/XQuery/test_command_line.php'+ '?collection=' + collection + '&zone=' + id + '&line=' + line, function(responseTxt, statusTxt, xhr){
if(statusTxt == "success")
$(e).html(l);
if(statusTxt == "error")
alert("Error: " + xhr.status + ": " + xhr.statusText);
});
}
I'm not sure where I'm going wrong -- I'm pretty comfortable with php and XML, but less so with javascript and especially with Ajax and Jquery.
Try this
function compare_toggle_visibility(id, line, collection){ var e = document.getElementById(id);
e.style.display = ((e.style.display!='none') ? 'none' : 'block');
$.get('http:/www.minorworksoflydgate.net/XML/XQuery/test_command_line.php'+ '?collection=' + collection + '&zone=' + id + '&line=' + line, function(responseTxt){
$(e).html(responseTxt);
});
}
I've been trying to get Android push notifications working for my app for a little while now (iOS already completed) and have everything sorted out besides just getting the notification to actually show up on the Android device.
Registering the device id's, and pushing to the GCM server all seem to be working fine, but when I test what the message in the response back from GCM is returning I keep getting undefined.
All responses when pushing the message to GCM are success, correct device id's, a message id associated with it etc. Anyone able to point me in the right direction? Below you will see the code snippet with just a sample "alert" being used to display what is coming back that will end up being used as the notification in the "push".
This alert
alert('message = ' + e.message + ' payload message: ' + e.payload.message +
' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);
doesn't seem to be getting anything back to display the push.
function onDeviceReady() {
console.log('deviceready');
try {
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android' || device.platform == 'amazon-fireos') {
console.log('PN register');
pushNotification.register(successHandler, errorHandler, {
"senderID": "177718756870",
"ecb": "onNotification"
}); // required!
console.log('after PN register');
} else {
console.log('PN register');
pushNotification.register(tokenHandler, errorHandler, {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb": "onNotificationAPN"
}); // required!
console.log('after PN register');
}
}
catch (err) {
txt = "There was an error on this page.\n\n";
txt += "Error description: " + err.message + "\n\n";
console.log("ERROR", txt);
}
}
var pushNotification;
// handle GCM notifications for Android
window.onNotification = function(e) {
console.log('EVENT RECEIVED ' + e.event)
console.log("regID BEFORE CHECKS = " + e.regid);
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0)
{
console.log("regID = " + e.regid);
var data =
{
'device_id': e.regid,
'platform': device.platform,
'os_version': device.version,
'app_version': lawnmowerConfig.versionString,
'device_model': device.model
};
localStorage.setItem('push_data', JSON.stringify(data));
}
break;
case 'message':
console.log('Inside case message: ' + e.regid)
if (e.foreground)
{
// Add something to play a sound once working
}
else
{
if (e.coldstart) {
console.log("coldstart");
}
else {
console.log("not coldstart");
}
}
alert('message = ' + e.message + ' payload message: ' + e.payload.message + ' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);
break;
case 'error':
alert('GCM error = ' + e.msg);
break;
default:
// Testing using these alerts instead
alert('An unknown GCM event has occurred');
break;
}
};
function tokenHandler (result) {
console.log('push token handler');
console.log(result);
var data =
{
'device_id': result,
'platform': device.platform,
'os_version': device.version,
'app_version': lawnmowerConfig.versionString,
'device_model': device.model
};
localStorage.setItem('push_data', JSON.stringify(data));
}
function successHandler (result) {
console.log('success handler push success');
console.log("result: " + result);
}
function errorHandler (error) {
console.log('push error');
}
document.addEventListener('deviceready', onDeviceReady, true);
Registering the device id's, and pushing to the GCM server all seem to be working fine, but when I test what the message in the response back from GCM is returning I keep getting undefined.
It means that when you are testing "what the message .... is" you are referencing a variable not yet defined. In this line:
alert('message = ' + e.message + ' payload message: ' + e.payload.message +
' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);
there is no variable e.message. The data that you send from your server is attached to e.payload and value of e.event is set to message. I think your problem can be solved if you remove e.message. Something like:
alert('event = ' + e.event + ' payload message: ' + e.payload.message +
' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);
Note To detect variable issues (scope and/or declaration), debug one variable at a time. This will help you to precisely identify a problem and trace it to its origin.
I would advise you to use
alert("Payload message: " + e.payload.message);
I need to detect whether an specific .js file was served in a http response and additionally, check the domain it came from, like this:
I need to automatically detect the lack of the js file and email the incidence
I tried Net::Http, rest-client, mechanize and a lot of gems, they just return the html header. It seems I need to monitor http traffic with tools like PhantomJS and checking for the file, but is there any rubyesque way of doing this?
Thanks in advance
I ended with the phantomjs approach. A ruby script iterate over a database table and then calls this phantomjs script for each record representing an URL
This is the phantomjs script
var page = require('webpage').create(),
system = require('system'),
address,
isScript = false;
var fs = require('fs');
// main
analizePage(system.args[1]);
//open page.
//onResourceRequested event, compares domain of each one with 'my.domain.net'
//append to a log file: -1 for failed url, 1 for script presence, 0 for no script presence
function analizePage(address){
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address ' + address);
fileWriter(-1, address);
}
else
{
if (!isScript){
fileWriter(0, address);
}
else
{
fileWriter(1, address);
}
console.log('Has script: ' + isScript);
}
phantom.exit(0);
});
page.onResourceRequested = function (req) {
try {
var link = document.createElement('a');
link.setAttribute('href', req.url); //extract asset's domain from URL
if (link.hostname == 'my.domain.net') {
isScript = true;
}
} catch(e) {
console.log("PAGE OPEN ERROR: " + e);
}
};
}
function fileWriter(type, line){
try {
fs.write("scriptlog.csv", type + ',' + line + ',' + Date.now() + ',' + system.args[2] + '\n', 'a');
} catch(e) {
console.log("FILE ERROR: " + e);
}
}
How can i see javascript errors from within a Phonegap App?
Currently I am trying to use this:
window.onerror = function(msg, url, linenumber) {
console.error('Type: ' + typeof msg + '\nError message: ' + msg + '\nURL: ' + url + '\nLine Number: ' + linenumber);
return true;
};
But nothing shows up on screen or in weinre I am using.