server crashes from uncaught exception thrown by _initProperties when passing string for Array field in mongodb
see the model schema and my post api request below.
/* /common/models/Content.json */
{
"base": "PersistedModel",
"name": "Content",
"properties": {
"alias": [{ "type": "string" }],
"categories": [{ "type": "string" }],
"contexts": [{ "type": "string" }],
"context_settings": { "type": "object" },
"title": { "type": "string" },
"type": { "type": "string" },
"translations": { "type": "object" },
"widgets": [{ "type": "object" }]
}
}
/ * POST /Contents example that crashes server */
{"widgets":"this string will crash the server when it attempts to cast it to an array"}
here is the stack output:
/Users/kaizhu/src/swagger-demo/node_modules/loopback-connector-mongodb/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
Error: could not create List from JSON string: "this string will crash the server when it attempts to cast it to an array"
at new List (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/list.js:16:13)
at List (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/list.js:9:12)
at ModelConstructor.Object.defineProperty.set [as widgets] (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model-builder.js:406:43)
at ModelConstructor.ModelBaseClass._initProperties (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model.js:139:17)
at ModelConstructor.ModelBaseClass (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model.js:42:8)
at ModelConstructor (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model-builder.js:169:22)
at ModelConstructor (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model-builder.js:169:22)
at new ModelConstructor (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/model-builder.js:169:22)
at Function.upsert (/Users/kaizhu/src/swagger-demo/node_modules/loopback-datasource-juggler/lib/dao.js:277:14)
at SharedMethod.invoke (/Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:224:17)
at HttpContext.invoke (/Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/http-context.js:253:12)
at /Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:477:9
at execStack (/Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:348:7)
at /Users/kaizhu/src/swagger-demo/node_modules/loopback/lib/application.js:319:13
my current fix is to override _initProperties to call it in a try-catch block, but this is far from ideal:
// bug - catch uncaught exceptions in remote api
mainApp.loopback_datastore_juggler.ModelBaseClass.prototype._initProperties0 = mainApp.loopback_datastore_juggler.ModelBaseClass.prototype._initProperties;
mainApp.loopback_datastore_juggler.ModelBaseClass.prototype._initProperties = function (data, options) {
try {
debugPrint('aa');
this._initProperties0(data, options);
} catch (errorCaught) {
debugPrint(errorCaught.stack);
//!! throw errorCaught;
debugPrint('bb');
//!! next(errorCaught);
}
};
the best fix i can think of is to enclose the next() call in a try-catch block in /Users/kaizhu/src/swagger-demo/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:348:7
function execStack(err) {
if(err) return next(err);
var cur = stack.shift();
if(cur) {
cur.call(scope, ctx, execStack, method);
} else {
- next();
+ try { next() } catch (errorCaught) { next(errorCaught); }
}
}
return execStack();
};
What version of strong-remoting are you using? Is it possible this issue is a duplicate of https://github.com/strongloop/strong-remoting/pull/119?
+1, It is not the bug of the strong-remoting.
2014-11-04 3:05 GMT+08:00 Miroslav Bajtoš [email protected]:
What version of strong-remoting are you using? Is it possible this issue is a duplicate of strongloop/strong-remoting#119 https://github.com/strongloop/strong-remoting/pull/119?
— Reply to this email directly or view it on GitHub https://github.com/strongloop/loopback/issues/709#issuecomment-61529775.
hmm, yes, it looks related. has strongloop/strong-remoting#119 patch been pushed to npm? if so, then i can test if it still crashes
strongloop/strong-remoting/pull/119 was released in 2.7.1. In case the problem still persist, would you mind submitting a pull request to strong-remoting fixing the problem?
I tried it , but the server also crashed when I pass an object to a property that should be an array , I used the strong-remoting 2.8.1.
the error is : Error: Items must be an array: {"url":"http://upai.com/544e394f97f5d6de1a0c5cd7.jpg"}
would you check it again?
Thx
@PradnyaBaviskar will be looking into this issue in the next few days.
It should be possible to reproduce the problem of unguarded cur.call by adding a test to test/rest.test.js with the following code:
var method = givenSharedStaticMethod();
objects.before(method.name, function(ctx, next) {
// important: move of the initial tick where
// express eventually catches all errors
process.nextTick(next);
});
objects.before(method.name, function(ctx, next) {
throw new Error('test error');
});
request(app).get(method.url)
// verify error was caught and reported in the response
@Spark-star strongloop/strong-remoting#172 fixed the crash you mentioned in your comment. Please upgrade to [email protected] and let us know if it works for you now.
This issue should be fixed by https://github.com/strongloop/strong-remoting/pull/172 and https://github.com/strongloop/loopback-datasource-juggler/pull/430.