Created
December 3, 2016 12:04
-
-
Save BoLaMN/90ed70db9ed1836892046be68b3214f1 to your computer and use it in GitHub Desktop.
after-error.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var loggly = require('node-loggly-bulk'); | |
module.exports = function(Model, methods) { | |
var buildEvent = function(ctx) { | |
var req = ctx.req; | |
var method = ctx.method; | |
var error = ctx.error; | |
var result = ctx.result; | |
var args = ctx.args; | |
var ctorArgs = ctx.ctorArgs; | |
if (error) { | |
status = error.statusCode || error.status; | |
} else if (Object.keys(result) > 0) { | |
status = 200; | |
} else { | |
status = 204; | |
} | |
requestIp = req.ip || req._remoteAddress || ((ref = req.connection) != null ? ref.remoteAddress : void 0) || void 0; | |
locals = req.locals; | |
data = args.data; | |
user = (typeof locals.toObject === "function" ? locals.toObject() : void 0) || locals; | |
args.data = (typeof data.toObject === "function" ? data.toObject() : void 0) || data; | |
return { | |
method: req.method, | |
url: req.originalUrl, | |
sharedMethod: method.sharedClass.name, | |
method: method.name, | |
result: result || {}, | |
error: error || {}, | |
status: status, | |
user: user, | |
ip: requestIp, | |
args: { | |
params: req.params, | |
query: req.query, | |
headers: req.headers, | |
ctorArgs: ctorArgs, | |
args: args | |
} | |
}; | |
}; | |
var afterErrorCreateEvent = function(method) { | |
var config = Model.app.get('loggly'); | |
var options = { | |
token: config.token, | |
subdomain: config.subdomain, | |
json: true, | |
tags: config.tags || [] | |
}; | |
options.push(method); | |
loggly = loggly.createClient(options); | |
Model.afterRemoteError(method, function(ctx, next) { | |
loggly.log(buildEvent(ctx), next); | |
}); | |
}; | |
if (Array.isArray(methods)) { | |
methods.forEach(afterErrorCreateEvent); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment