feat(xo-server/api): emit event on call/resolution of xo method (#3770)
This commit is contained in:
parent
86425f5d51
commit
76f9017482
@ -210,6 +210,7 @@ export default class Api {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async callApiMethod(session, name, params = {}) {
|
async callApiMethod(session, name, params = {}) {
|
||||||
|
const xo = this._xo
|
||||||
const startTime = Date.now()
|
const startTime = Date.now()
|
||||||
|
|
||||||
const method = this._methods[name]
|
const method = this._methods[name]
|
||||||
@ -219,7 +220,7 @@ export default class Api {
|
|||||||
|
|
||||||
// FIXME: it can cause issues if there any property assignments in
|
// FIXME: it can cause issues if there any property assignments in
|
||||||
// XO methods called from the API.
|
// XO methods called from the API.
|
||||||
const context = Object.create(this._xo, {
|
const context = Object.create(xo, {
|
||||||
api: {
|
api: {
|
||||||
// Used by system.*().
|
// Used by system.*().
|
||||||
value: this,
|
value: this,
|
||||||
@ -231,9 +232,24 @@ export default class Api {
|
|||||||
|
|
||||||
// Fetch and inject the current user.
|
// Fetch and inject the current user.
|
||||||
const userId = session.get('user_id', undefined)
|
const userId = session.get('user_id', undefined)
|
||||||
context.user = userId && (await this._xo.getUser(userId))
|
context.user = userId && (await xo.getUser(userId))
|
||||||
const userName = context.user ? context.user.email : '(unknown user)'
|
const userName = context.user ? context.user.email : '(unknown user)'
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
userId,
|
||||||
|
method: name,
|
||||||
|
params: sensitiveValues.replace(params, '* obfuscated *'),
|
||||||
|
}
|
||||||
|
|
||||||
|
const callId = Math.random()
|
||||||
|
.toString(36)
|
||||||
|
.slice(2)
|
||||||
|
|
||||||
|
xo.emit('xo:preCall', {
|
||||||
|
...data,
|
||||||
|
callId,
|
||||||
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await checkPermission.call(context, method)
|
await checkPermission.call(context, method)
|
||||||
|
|
||||||
@ -271,23 +287,33 @@ export default class Api {
|
|||||||
)}] ==> ${kindOf(result)}`
|
)}] ==> ${kindOf(result)}`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
xo.emit('xo:postCall', {
|
||||||
|
callId,
|
||||||
|
method: name,
|
||||||
|
result,
|
||||||
|
})
|
||||||
|
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
params = sensitiveValues.replace(params, '* obfuscated *')
|
const serializedError = serializeError(error)
|
||||||
const data = {
|
|
||||||
userId,
|
xo.emit('xo:postCall', {
|
||||||
|
callId,
|
||||||
|
error: serializedError,
|
||||||
method: name,
|
method: name,
|
||||||
params,
|
})
|
||||||
|
|
||||||
|
const message = `${userName} | ${name}(${JSON.stringify(
|
||||||
|
data.params
|
||||||
|
)}) [${ms(Date.now() - startTime)}] =!> ${error}`
|
||||||
|
|
||||||
|
this._logger.error(message, {
|
||||||
|
...data,
|
||||||
duration: Date.now() - startTime,
|
duration: Date.now() - startTime,
|
||||||
error: serializeError(error),
|
error: serializedError,
|
||||||
}
|
})
|
||||||
const message = `${userName} | ${name}(${JSON.stringify(params)}) [${ms(
|
|
||||||
Date.now() - startTime
|
|
||||||
)}] =!> ${error}`
|
|
||||||
|
|
||||||
this._logger.error(message, data)
|
if (xo._config.verboseLogsOnErrors) {
|
||||||
|
|
||||||
if (this._xo._config.verboseLogsOnErrors) {
|
|
||||||
log.warn(message, { error })
|
log.warn(message, { error })
|
||||||
} else {
|
} else {
|
||||||
log.warn(
|
log.warn(
|
||||||
@ -301,7 +327,7 @@ export default class Api {
|
|||||||
if (xoError) {
|
if (xoError) {
|
||||||
throw xoError(error.params, ref => {
|
throw xoError(error.params, ref => {
|
||||||
try {
|
try {
|
||||||
return this._xo.getObject(ref).id
|
return xo.getObject(ref).id
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return ref
|
return ref
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user