You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to format the response returned to client ? #624
// in my-project/server/boot/hook.jsmodule.exports=function(server){varremotes=server.remotes();// modify all returned valuesremotes.after('**',function(ctx,next){ctx.result={data: ctx.result};next();});};
Reacted by K. Volkan ÇATAK, Virendra Singh Rathore, Thiên Toán, Jeff Arnold and Nguyen Canh Toan
There is a pending pull request that will allow you to provide a custom error response handler - see strongloop/strong-remoting#114. Will that solve your problem?
@bajtos thanks for your support, but above fix doesn't resolve my issue.
I modified rest-adapter.js as you suggested
RestAdapter.errorHandler = function(options) {
options = options || {};
return function restErrorHandler(err, req, res, next) {
if (options.handler) {
return options.handler(err, req, res, next)
}
if(typeof err === 'string') {
err = new Error(err);
err.status = err.statusCode = 500;
}
res.statusCode = err.statusCode || err.status || 500;
debug('Error in %s %s: %s', req.method, req.url, err.stack);
var data = {
name: err.name,
status: res.statusCode,
message: err.message || 'An unknown error occurred'
};
for (var prop in err) {
data[prop] = err[prop];
}
data.stack = err.stack;
if (process.env.NODE_ENV === 'production' || options.disableStackTrace) {
delete data.stack;
}
res.send({ error: data });
};
};
but the after hook is still not triggered :(
hence, maybe it's just because I forgot to set "handler", more exactly, I don't know how can I set handler so that it could be used in loopback/lib/application.js
Of course it won't be triggered, this is a different mechanism.
I suppose we can add remotes.afterError hook allowing users to customise error objects, although I am not sure if that's a desirable feature. Feel free to open a new GH issue to discuss that idea.
in strong-remoting/lib/rest-adapter.js, the input data for RestAdapter.errorHandler is "this.remotes.options.errorHandler
RestAdapter.prototype.createHandler = function () {
....
// Use our own error handler to make sure the error response has
// always the format expected by remoting clients.
root.use(RestAdapter.errorHandler(this.remotes.options.errorHandler));
return root;
};
then, if I set up handler as your suggestion in /server/server.js
// in you server/server.js
var opts = app.get('remoting') || {};
opts.handler = function(...){};
app.set('remoting', opts);
following code line in function RestAdapter.errorHandler of /strong-remoting/lib/rest-adapter.js never works as our expect, specifically options.handler is never returned because "options.errorHandler" doesn't have any "handler" property.
Thanks @virendra656
In my case, I needed to intercept well meant MongoError instances that shouldn't be served with status 500, they were more 409 (ish). This worked perfecly.
Activity
ritch commentedon Oct 9, 2014
You can use a strong-remoting after hook...
ritch commentedon Oct 9, 2014
@crandmck can you add this to the docs?
projectxmaker commentedon Oct 9, 2014
@ritch: thanks
crandmck commentedon Oct 10, 2014
See #634
projectxmaker commentedon Oct 15, 2014
@ritch actually, the hook is not triggered in some case "error" response returned, just note that, it works properly in case of successful response.
for example about error response of POST /Users/login end-point
{ "error": { "name": "Error", "status": 401, "message": "login failed", "statusCode": 401, "stack": "Error: login failed\n at ...." } }I added the hook.js into /server/boot folder.
module.exports = function(server) { var remotes = server.remotes(); console.log('enter #1'); remotes.before('**', function (ctx, next, method) { console.log('enter #2'); next(); }); // modify all returned values remotes.after('**', function (ctx, next) { console.log('enter #3', ctx); next(); }); };I run server with "slc run"
in the case of built-in User, after requesting POST /Users/login with some invalid data to get error response
{ "username":"mabu", "password":"songoku" }I check the console and found only
no log for "enter #3", it means the hook is not triggered as expected, is it a bug ?
Note:
projectxmaker commentedon Oct 15, 2014
not sure is this the reason why the after hook doesn't work in error response
/loopback/node_modules/strong-remoting/lib/rest-adapter.js
RestAdapter.errorHandler = function(options) { options = options || {}; return function restErrorHandler(err, req, res, next) { if(typeof err === 'string') { err = new Error(err); err.status = err.statusCode = 500; } res.statusCode = err.statusCode || err.status || 500; debug('Error in %s %s: %s', req.method, req.url, err.stack); var data = { name: err.name, status: res.statusCode, message: err.message || 'An unknown error occurred' }; for (var prop in err) { data[prop] = err[prop]; } data.stack = err.stack; if (process.env.NODE_ENV === 'production' || options.disableStackTrace) { delete data.stack; } res.send({ error: data }); // IT SENDS ERROR DATA TO CLIENT DIRECTLY ? }; };projectxmaker commentedon Oct 15, 2014
@bajtos do you have any idea about this issue ?
bajtos commentedon Oct 15, 2014
@projectxmaker Your conclusion seems correct to me.
There is a pending pull request that will allow you to provide a custom error response handler - see strongloop/strong-remoting#114. Will that solve your problem?
projectxmaker commentedon Oct 15, 2014
@bajtos thanks for your support, but above fix doesn't resolve my issue.
I modified rest-adapter.js as you suggested
RestAdapter.errorHandler = function(options) { options = options || {}; return function restErrorHandler(err, req, res, next) { if (options.handler) { return options.handler(err, req, res, next) } if(typeof err === 'string') { err = new Error(err); err.status = err.statusCode = 500; } res.statusCode = err.statusCode || err.status || 500; debug('Error in %s %s: %s', req.method, req.url, err.stack); var data = { name: err.name, status: res.statusCode, message: err.message || 'An unknown error occurred' }; for (var prop in err) { data[prop] = err[prop]; } data.stack = err.stack; if (process.env.NODE_ENV === 'production' || options.disableStackTrace) { delete data.stack; } res.send({ error: data }); }; };but the after hook is still not triggered :(
hence, maybe it's just because I forgot to set "handler", more exactly, I don't know how can I set handler so that it could be used in loopback/lib/application.js
app.remotes = function () { if(this._remotes) { return this._remotes; } else { var options = {}; if(this.get) { options = this.get('remoting'); // HOW ? } return (this._remotes = RemoteObjects.create(options)); } }is it the reason why your solution cannot solve my issue so far ?
bajtos commentedon Oct 15, 2014
Option A:
Option B (may not work as I expect, requires strongloop/loopback-boot#54 - strong-remoting@2.1.0):
bajtos commentedon Oct 15, 2014
Of course it won't be triggered, this is a different mechanism.
I suppose we can add
remotes.afterErrorhook allowing users to customise error objects, although I am not sure if that's a desirable feature. Feel free to open a new GH issue to discuss that idea.projectxmaker commentedon Oct 15, 2014
@bajtos thanks for your support :)
it seems I have to stop thinking about Re-formating Response of Loopback API.
just thought it's just simple to do after hook as @ritch suggested, but it is to be too difficult so far O.o
projectxmaker commentedon Oct 16, 2014
@bajtos just share something I found
in strong-remoting/lib/rest-adapter.js, the input data for RestAdapter.errorHandler is "this.remotes.options.errorHandler
RestAdapter.prototype.createHandler = function () { .... // Use our own error handler to make sure the error response has // always the format expected by remoting clients. root.use(RestAdapter.errorHandler(this.remotes.options.errorHandler)); return root; };then, if I set up handler as your suggestion in /server/server.js
// in you server/server.js var opts = app.get('remoting') || {}; opts.handler = function(...){}; app.set('remoting', opts);following code line in function RestAdapter.errorHandler of /strong-remoting/lib/rest-adapter.js never works as our expect, specifically options.handler is never returned because "options.errorHandler" doesn't have any "handler" property.
RestAdapter.errorHandler = function(options) { options = options || {}; if (options.handler) return options.handler; .... };SO, I re-defined what you suggested to change in /server/server/js as following
// in you server/server.js var opts = app.get('remoting') || {}; opts.errorHandler = { 'handler': function restErrorHandler(err, req, res, next) { .... } } app.set('remoting', opts);another solution is to put this code into a hook in /server/boot/hook.js
module.exports = function(server) { var remotes = server.remotes(); var opts = remotes.options || {}; opts.errorHandler = { 'handler': function restErrorHandler(err, req, res, next) { .... } } remotes.options = opts; }thanks for your support 👍
mike-aungsan commentedon Dec 3, 2015
How to format the response returned from Remote methods to client?
How to response just a String from Remote methods to client?
virendra656 commentedon Nov 8, 2016
If you just want to format all errors then make handler in server.js.
ffflabs commentedon Dec 6, 2016
Thanks @virendra656
In my case, I needed to intercept well meant MongoError instances that shouldn't be served with status 500, they were more 409 (ish). This worked perfecly.
shallker commentedon Jun 19, 2018
As note, for Loopback 3.X