How can I check error code in Gnome/gjs/Gio - javascript

This doesn't work (nothing happens when the directory exists):
let s_dir = Gio.file_new_for_path("./S1");
try {
s_dir.make_directory(null);
} catch(e) {
if(e == Gio.IOErrorEnum.EXISTS)
print(e);
}

Use the GLib.Error.matches() method:
let s_dir = Gio.file_new_for_path("./S1");
try {
s_dir.make_directory(null);
} catch(e) {
if (e.matches (Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)
print(e);
}

Related

How can I use multiple pm.expect in one pm.test in postman tests

I want to check if request has a string then response has to contain this string in postman. I write a test like this but it doesn't work for me. I'm not sure I can use multiple pm.expect in one test with condition. My code is
pm.test("test",function(){
var hasA = data.hasOwnProperty('A');
var hasB = data.hasOwnProperty('B');
if(hasA === true){
pm.expect(response.A).to.eql(data.A);
}
if(hasB === true){
pm.expect(response.B).to.eql(data.B);
}});
Request can contain A and B both so I cannot use if..else statement. When I run this test if hasA flag true then code doesnt go into for second if even hasB option is true. How can I handle this problem.Can anyone help?
expect is hard assertion so if one fails then it won't goto next one so you can use any of the below approach:
try if conditions like:
pm.test("test", function () {
var hasA = data.hasOwnProperty('A');
var hasB = data.hasOwnProperty('B');
var hasBoth = hasA && hasB
if (hasBoth) {
pm.expect([response.A, response.B]).to.eql([data.A, data.B]);
} else if (hasA) {
pm.expect(response.A).to.eql(data.A);
} else if (hasB){
pm.expect(response.B).to.eql(data.B);
}
});
or
try catch like :
pm.test("test", function () {
var hasA = data.hasOwnProperty('A');
var hasB = data.hasOwnProperty('B');
let error = {};
try {
if (hasA) {
pm.expect(response.A).to.eql(data.A);
}
} catch (e) {
error.A = e
}
try {
if (hasB) {
pm.expect(response.A).to.eql(data.A);
}
} catch (e) {
error.B = e
}
if(Object.keys(error).length){
pm.expect.fail(JSON.stringify(error))
}
});

parse JSON object with value passed as single quote string

I an getting aws event parameter as follow in the lambda call.
let event = { pathParameters: '{"foo":"35314"}' }
When I am trying to validate the parameter in condition , it cant find foo key on pathParameters
Here my condition check
if (event.pathParameters && event.pathParameters.foo) {
//do something
} else {
console.log('fail');
}
It going in else condition . I tried JSON.parse(JSON.strinify(event)). It did not help. I do get the Object if I do JSON.parse(event.pathParameters).
Any way to resolve the issue on root level object.
No, you can't parse the event to get access to the '{"foo": "35314}'", you need to parse the event.pathParameters value to get the actual foo and its value, 35314
let event = { pathParameters: '{"foo":"35314"}' }
if (event.pathParameters && JSON.parse(event.pathParameters).foo) {
console.log("'foo' =", JSON.parse(event.pathParameters).foo);
} else {
console.log('fail');
}
This is because the data that you are getting has JSON as stringified in pathParameters, so you'll have to parse with that key something like
JSON.parse(event.pathParameters).foo
let event = { pathParameters: '{"foo":"35314"}' } // foo is a string
if (event.pathParameters) {
try {
const { foo } = JSON.parse(event.pathParameters);
// use foo
} catch (jsonError) {
console.log('There was some json parse error', jsonError);
}
} else {
console.log('fail');
}
You need to parse the data from event.pathParameters
function doSomething(event) {
let pathParametersObj = JSON.parse(event.pathParameters);
if (pathParametersObj && pathParametersObj.foo) {
//do something
console.log('pass');
} else {
console.log('fail');
}
}
let event1 = {
pathParameters: '{"foo":"35314"}'
}
let event2 = {
pathParameters: null
}
doSomething(event1);
doSomething(event2);

Error: The quota has been exceeded. on Safari IOS 10

I'm getting this error on my iPhone's safari, when doing localStorage.setItem('user',some string here):
Error: The quota has been exceeded.
setItem#[native code]
It is not private mode! What other circumstances can make localStorage not work?
I created this class to help get around private browsing. However, storage will be blown away when you refresh the browser.
const data = {};
let hasLocalStorage = false;
if (localStorage) {
try {
const x = 'storageTest';
localStorage.setItem(x, x);
localStorage.removeItem(x);
hasLocalStorage = true;
} catch (e) {
hasLocalStorage = false;
}
}
class StorageUtilities {
setItem(key, value) {
if (hasLocalStorage) {
localStorage.setItem(key, value);
} else {
data[key] = value;
}
}
getItem(key) {
if (hasLocalStorage) {
return localStorage.getItem(key);
}
return data[key];
}
removeItem(key) {
if (hasLocalStorage) {
localStorage.removeItem(key);
} else {
data[key] = null;
}
}
}
const storageUtilities = new StorageUtilities();
export default storageUtilities;
Actually it was Private mode. Looks like it is enabled by default on new iphones.

Calling functions in an async function javascript (Meteor) Getting undefined

The issue seems to be that calling the function vote_ipcheck() and vote_cookie_check() both are throwing an error Uncaught TypeError: undefined is not a function. If I put the contents of the function inside the $.getJSON call then it's not an issue, however calling the function throws that error.
If anyone's got an idea as to why something like this is occurring, it would be great.
if (ip_check) {
$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
console.log(data.host);
var vote_ipcheck = vote_ipcheck(data.host);
var vote_cookie_check = vote_cookie_check();
if (vote_ipcheck && vote_cookie_check) {
Router.go('pollyResults', {_id: id});
} else if (vote_ipcheck == false && vote_cookie_check == false) {
update_poll();
}
});
}
function vote_cookie_check() {
// Handling the cookie business
console.log(ReactiveCookie.list());
if (ReactiveCookie.get('voted')) {
var object_voted = JSON.parse(ReactiveCookie.get('voted'));
if (id in object_voted) {
if (object_voted[id] == true) {
return true;
}
} else {
object_voted[id] = true;
ReactiveCookie.set('voted', JSON.stringify(object_voted), {days: 365});
return false;
}
} else {
var object_voted = {};
object_voted[id] = true;
ReactiveCookie.set('voted', JSON.stringify(object_voted), {days: 365});
return false;
}
}
function vote_ipcheck(ip) {
ip_voted = ip_array.indexOf(ip);
if (ip_voted > -1) {
return true;
}
else {
Polls.update({_id: id}, {$push : {already_voted : ip}});
return false;
}
}
Do not redefine vote_ipcheck and vote_cookie_check in the local scope if you want to use global functions with these names. Give the local variables different names.
var ipcheck = vote_ipcheck(data.host);
var cookie_check = vote_cookie_check();
if (ipcheck && cookie_check) {
Router.go('pollyResults', {_id: id});
} else if (ipcheck == false && cookie_check == false) {
update_poll();
}

Firefox addon: Getting tab from XMLHttpRequest

I am trying to associate an XMLHttpRequest with a tab on the browser using the following code:
function getBrowserFromChannel(aChannel) {
var notificationCallbacks =
aChannel.notificationCallbacks ?
aChannel.notificationCallbacks :
aChannel.loadGroup.notificationCallbacks;
if (!notificationCallbacks) {
console.log("no callbacks");
return (0);
}
var loadContext = notificationCallbacks.getInterface(Ci.nsILoadContext);
getInterface(Ci.nsILoadContext) fails with: "Component does not have requested interface"
Any idea how else I can get the browser?
Thanks
Try this code (from Lightbeam):
function getLoadContext(aRequest) {
try {
// first try the notification callbacks
var loadContext = aRequest.QueryInterface(Ci.nsIChannel)
.notificationCallbacks.getInterface(Ci.nsILoadContext);
return loadContext;
} catch (ex) {
// fail over to trying the load group
try {
if (!aRequest.loadGroup) return null;
var loadContext = aRequest.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext);
return loadContext;
} catch (ex) {
return null;
}
}
}
Note the license is MPL 1.1/GPL 2.0/LGPL 2.1

Categories

Resources