How to detect an error in mapreduce - mongodb

Let's have an "error_test.js" file that contains:
db = db.getMongo().getDB( "mydb" );
db.mycol.insert( { hello : "world" } );
print("it is shown");
db.runCommand(
{
mapReduce: "mycol",
map: function(){ print(not_exists); },
reduce: function(key, values){},
out: { replace: "myrescol" }
}
);
print("it is shown too (after error in mapreduce!)");
If I run the file (in Windows command line), I get:
mypath>mongo error_test.js
MongoDB shell version: 2.4.0
connecting to: test
it is shown
it is shown too (after error in mapreduce!)
mypath>echo %errorlevel%
0
mypath>
So we can deduce that:
the mapreduce error doesn't stop the execution.
the mapreduce error is not shown to the user.
the mapreduce error is not translated to the exit code (0 = success) (so a caller program can't detect the error).
The only way to know of the error is by looking for the following line at "mongod.log":
Wed Jun 12 10:02:37.393 [conn14] JavaScript execution failed: ReferenceError: not_exists is not defined near '(){ print(not_exists)'
Same happens if we use the "db.doEval(my_js)" method in Java and we put the content of "error_test.js" file into the "my_js" variable: The mapreduce error is not detected (no excepcion is launched, no null value is returned, "ok : 1.0" appears in the response).
So my question is: How can I detect an error in the mapreduce? (both in a js file and in the doEval() method)
Thank you

You need to capture the return document from db.runCommand() into a variable and then check its ok value in your script - you can then throw an error or print output, etc.
print("it is shown");
var res = db.runCommand(
{
mapReduce: "mycol",
map: function(){ print(not_exists); },
reduce: function(key, values){},
out: { replace: "myrescol" }
}
);
printjson(res);
if (res.ok == 0) {
print("Oopsy!");
throw("error! " + res.errmsg + " returned from mapreduce");
}
print("it is shown too (after error in mapreduce!)");

Related

How do I get exit code after running command in integrated terminal in vscode

After running terminal.sendtext("some command"), how do I get the exit code of the command? If this is not possible, is there a way to run the command in external terminal(using something likechild_process.spawnSync()) and get the exit code?
You could do something like this
const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
});
ls.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
});
Reference : https://nodejs.org/dist/latest-v12.x/docs/api/child_process.html#child_process_event_close
You could use the new terminal exit api, see v1.71 Release Notes: terminal exit status api:
TerminalExitStatus.reason
Extension authors now have better insight into why a terminal exited
via the new TerminalExitReason API.
export enum TerminalExitReason {
Unknown = 0,
Shutdown = 1,
Process = 2,
User = 3,
Extension = 4,
}

Protractor 5.1.1- Chai assertion failure is causing process to exit with error code: 199. No reports are generating after that

cucumber framework. When a chai assertion is failed, the process is exiting with error code:199. No reports are generated after that.
Protractor - 5.1.1
Here is my updated code,
this.Then(/^I should see process is saved in db$/, function (next) {
var sql = "select * from process where name = ?";
sql = mysql.format(sql, params.flow.procName);
console.log(sql);
dbConn.query(sql, function(err, rows, fields){
if(!err) {
procDbObj = rows;
var procName = procDbObj[0].name;
console.log(rows);
expect(procDbObj[0].name).to.equal(params.flow.procName);
expect(procDbObj[0].description).to.equal(params.flow.procDesc);
expect(procName).to.equal("AABBDCD").and.notify(next);
}
});
});
Below is the error I am seeing when an assertion is failed,
[11:23:59] E/launcher - expected 'Auto_proc_2h5c83' to equal 'AABBDCD'
[11:23:59] E/launcher - AssertionError: expected 'Auto_proc_2h5c83' to equal 'AABBDCD'
at Query._callback (C:\Users\panubrolu\workspace\ProtractorCucumber\features\step_definitions\E2E_step_definition.js:64:34)
at Query.Sequence.end (C:\Users\panubrolu\workspace\ProtractorCucumber\node_modules\mysql\lib\protocol\sequences\Sequence.js:86:24)
at Query._handleFinalResultPacket (C:\Users\panubrolu\workspace\ProtractorCucumber\node_modules\mysql\lib\protocol\sequences\Query.js:137:8)
at Query.EofPacket (C:\Users\panubrolu\workspace\ProtractorCucumber\node_modules\mysql\lib\protocol\sequences\Query.js:121:8)
at Protocol._parsePacket (C:\Users\panubrolu\workspace\ProtractorCucumber\node_modules\mysql\lib\protocol\Protocol.js:280:23)
at Parser.write (C:\Users\panubrolu\workspace\ProtractorCucumber\node_modules\mysql\lib\protocol\Parser.js:75:12)
at Protocol.write (C:\Users\panubrolu\workspace\ProtractorCucumber\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket. (C:\Users\panubrolu\workspace\ProtractorCucumber\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
[11:23:59] E/launcher - Process exited with error code 199
Any help is really appreciated. Thanks in advance.
I think there are a few problems in your code.
1: It the db-query a Promise? You are using Callback (next) and Promises ('return') at the same time. If it is a Promise then the code should be something like this
this.Then(/^I should see process is saved in db$/, function () {
var sql = "select * from process where name = ?";
sql = mysql.format(sql, params.flow.procName);
console.log(sql);
return dbConn.query(sql, function(err, rows, fields){
if(!err) {
procDbObj = rows;
var procName = procDbObj[0].name;
console.log(rows);
expect(procDbObj[0].name).to.eventually.equal(params.flow.procName);
expect(procDbObj[0].description).to.eventually.equal(params.flow.procDesc);
} else {
return Promise.reject('Query Failed, error = ' + err);
}
});
});
2: If it is a promise, did you also use chai and chai-as-promised?
3: If you are using a callback, then you should 'notify' Cucumber that the scenario is done, you can do that with expect('foo').to.equal('bar').and.notify(next);
4: You don't have a promise / callback when the query fails, see the Promise.reject('message') in the first point
Hope this will give you some info for debugging you problem.

Play-Framework: 2.3.x: play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException:

I am using play-framework 2.3.x with reactivemongo-extension JSON type. following is my code for fetch the data from db as below:
def getStoredAccessToken(authInfo: AuthInfo[User]) = {
println(">>>>>>>>>>>>>>>>>>>>>>: BEFORE"); //$doc("clientId" $eq authInfo.user.email, "userId" $eq authInfo.user._id.get)
var future = accessTokenService.findRandom(Json.obj("clientId" -> authInfo.user.email, "userId" -> authInfo.user._id.get));
println(">>>>>>>>>>>>>>>>>>>>>>: AFTER: "+future);
future.map { option => {
println("*************************** ")
println("***************************: "+option.isEmpty)
if (!option.isEmpty){
var accessToken = option.get;println(">>>>>>>>>>>>>>>>>>>>>>: BEFORE VALUE");
var value = Crypto.validateToken(accessToken.createdAt.value)
println(">>>>>>>>>>>>>>>>>>>>>>: "+value);
Some(scalaoauth2.provider.AccessToken(accessToken.accessToken, accessToken.refreshToken, authInfo.scope,
Some(value), new Date(accessToken.createdAt.value)))
}else{
Option.empty
}
}}
}
When i using BSONDao and BsonDocument for fetching the data, this code successfully run, but after converting to JSONDao i getting the following error:
Note: Some time this code will run but some it thrown an exception after converting to JSON
play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: bound must be positive
application -
Following are the logs of application full exception strack trace as below:
>>>>>>>>>>>>>>>>>>>>>>: BEFORE
>>>>>>>>>>>>>>>>>>>>>>: AFTER: scala.concurrent.impl.Promise$DefaultPromise#7f4703e3
play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: bound must be positive
application -
! #6m1520jff - Internal server error, for (POST) [/oauth2/token] ->
play.api.Application$$anon$1: Execution exception[[IllegalArgumentException: bound must be positive]]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.8.jar:2.3.8]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
at scala.Option.map(Option.scala:146) [scala-library-2.11.6.jar:na]
Caused by: java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388) ~[na:1.8.0_40]
at scala.util.Random.nextInt(Random.scala:66) ~[scala-library-2.11.6.jar:na]
The problem is solve, but i am not sure, why this produce, I think there is problem with reactivemongo-extension JSONDao library. because when i use findOne instead of findRandom the code is run successfully, but the findRandom is run good on BSON dao. Still not found what the exact problem is that, but following is the resolved code.
def getStoredAccessToken(authInfo: AuthInfo[User]) = {
println(authInfo.user.email+" ---- "+authInfo.user._id.get)
var future = accessTokenService.findOne($doc("clientId" $eq authInfo.user.email, "userId" $eq authInfo.user._id.get)); //user findOne instead of findRandom in JsonDao
future.map { option => {
if (!option.isEmpty){
var accessToken = option.get;
var value = Crypto.validateToken(accessToken.createdAt.value)
Some(scalaoauth2.provider.AccessToken(accessToken.accessToken, accessToken.refreshToken, authInfo.scope,
Some(value), new Date(accessToken.createdAt.value)))
}else{
Option.empty
}
}}
}

How to remove collection named 'group'?

I accidentally created a collection named 'group'.
How do I remove it.
When I give the following in the mongo console
db.group.drop()
I get the following error
Fri Jun 7 16:36:39.630 JavaScript execution failed: TypeError: Object function ( parmsObj ){
var ret = this.runCommand( { "group" : this._groupFixParms( parmsObj ) } );
if ( ! ret.ok ){
throw "group command failed: " + tojson( ret );
}
return ret.retval;
} has no method 'drop'
The problem is that group is a method on a database object. So, db.group cannot be used to get the actual collection named group. Instead, use .getCollection():
db.getCollection('group').drop()

Is it possible to pass a javascript function in the scope parameter of the Collection.map_reduce in pymongo?

Given:
jstrMap = """
function() {
print("isPointInside = " + isPointInside);
print("polygon = " + polygon);
emit(this._id, this);
}
"""
jstrReduce = """
function(key, values) {
return values[0];
}
"""
def readJSCodeFromFile(filePath):
with open(filePath) as f:
return Code(f.read())
jsIsPointInside = readJSCodeFromFile(path.join(path.dirname(__file__), 'IsPointInside.js'))
IsPointInside.js:
function(pt, poly) {
}
And I invoke map_reduce like this:
mycoll.map_reduce(jstrMap, jstrReduce, 'results',
scope = {'isPointInside': jsIsPointInside, 'polygon': [[-77, 39], [-77,38], [-78,38], [-78,39]]})
Here is what I get on the client console:
db assertion failure, assertion: 'map invoke failed: JS Error: TypeError: isPointInside is not a function nofile_b:3', assertionCode: 9014
And the server output is:
isPointInside = null
polygon = -77,39,-77,38,-78,38,-78,39
Sun Apr 01 16:29:14 [conn11] JS Error: TypeError: isPointInside is not a function nofile_b:3
Sun Apr 01 16:29:14 [conn11] mr failed, removing collection :: caused by :: 9014 map invoke failed: JS Error: TypeError: isPointInside is not a function nofile_b:3
Debugging the python code reveals that jsIsPointInside is of type Code, as expected. str(jsIsPointInside) returns the function text, i.e. 'function(pt, poly) {\n}\n'
I do not want to populate the system.js collection, I'd like to pass the function in the scope. Is it possible at all?
Thanks.
Scope is an object where the fields are placed in the MapReduce scope as variables w/ the field name.
If you'd like to put a function in scope, you need to make it a value a field e.g.
scope = {
myFunc: function() { return "Foo";}
}