Checking with blank(param("some_parameter_name"))) fails with NPE - activeweb

I tried to check if a mandatory request parameter present with
if (blank(param("some_parameter_name"))) {
// throw SomeException
}
and it failed with NPE because:
in HTTPSupport#blank(String ... names) there is a call to if(Util.blank(param(name)))
when calling param(name) it returns RequestUtils.param(name);
in RequestUtils.param(name) it fails when calling if(name.equals("id")) because name parameter is null. Should I open an issue for that ?

This is an incorrect use of the API
Please, use like this:
if (blank("param1", "param2",...)) {
// throw SomeException
}
In other words, the method blank() expects names of parameters to check.
Check out docs: HttpSupport.html#blank

Related

How To Implement/ Handling Exception In ExpressJs (NODE.JS)

Is it possible to implement the below way to handle the exception in expressjs Framework:
actually, I am calling a method ( Fun1 ) by a passing parameter (key_id, user_id). Like below:
data = Fun1(key_id=123, user_id=234)
so, if above line returns an error then I need to call other function (Fun2) otherwise no need to call this (Fun2)
I am implementing like this way, but it's not working( Means, when try block return error then catch block, is not executing)
try{
var data = Fun1(key_id=123, user_id=234);
}
catch(err){
var data = Fun2(key_id=123, user_id=234);
}
is there any other way to handle?
If the Fun1 throws an error then the above mentioned implantation works perfect and moves to the catch block and executes the Fun2 but if it returns a value then this does not work
Syntax for throwing an error
throw new Error("<Error Message>")

DocumentDB readCollection function error

I'm trying to use the "readCollection" function, but I get an Error:
The "options" parameter must be of type "object". Actual type is:
"function".
my code:
docDbClient.readCollection(docUrl, function (err, collection) {
console.log(collection);
}, function(err){
console.log(err);
});
The docUrl var is equal to my document._self path.
This code was working before, but for some reason it not anymore without me making any changes.
From what the error says the 'option' parameter needs to be an object instead of a function, but from what I read on the documentation the 'option' parameter is optional which I don't have it with in my function.
I'm also getting the same error when I use the replaceDocument function.
http://azure.github.io/azure-documentdb-node/DocumentClient.html#readCollection
The problem is that you have an error handling function in your parameter list, which causes it to think that the second parameter is options. Remove the error handling function, and add code inside your main handler to process it if err does not come back null.

Return in pg-promise

I created a separate file with all my queries using pg-promise module for Node. While for most of them I just use req, res after a query, for one I want to return a value back. It does not work. It returns undefined.
passportLogin: (email)=> {
db.one(`SELECT userid
FROM user`)
.then( result => {
return result;
})
.catch( error => {
return error;
});
}
That code has two problems at the same time:
invalid promise use, when inside .catch you do return result, that's not how promise rejects are handled, you must either provide the error handling, or re-throw / re-reject the error.
invalid use of pg-promise library. You use method one that's supposed to reject when anything other than 1 record is returned, as per the method's documentation, and at the same time you are saying I need to catch if it returns more than one row... , which is a logical contradiction.
The result of it is as follows: your query executes successfully, and returns more than one record, which in turn makes method one reject, and then you take the rejection reason and turn it into a resolved one by doing return result. In all, your code is broken all over the place.
First, with pg-promise you are supposed to use the right method, according to the number of records you are expecting back, see The Basics.
And then handle .then/.catch according to your business logic. I can't be more specific here, because you did not provide further details on this.

Handling errors in a Tape test?

If I have a function that throws an error and I want to test for that error, I'd write something like this:
test('throws at something that is not a string', t => {
t.plan(1)
t.err(loadString(9))
})
But this always results in an actual error coming from the function while executing:
And there's the not ok 1 no plan found and not ok 2 no assertions found, which is also weird. How can I make sure it doesn't actually throw?
If you want to test if the function will throw, you have to use t.throws and pass it a function (not the error value). Here, I assume loadString is the actual function you are testing, so YOU are actually causing it to throw instead of Tape invoking it and catching the error.
Try this instead:
t.throws(function() { loadString(9); });

node assert: Test error message

Using node's assert module how can I test the message of an error?
throw new Error('Email is required!');
I'm using assert.throws to check if an error was thrown:
assert.throws(myFunction, Error);
But this does not provide the ability to check the message.
You can pass a regular expression as the second argument.
assert.throws(myFunction, /Email is required/);
You can assert a full error object, including the message without using regular expressions:
assert.throws(myFunction, new Error("Email is required"));
This way it also asserts the correct error name (class).

Resources