Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate logger in send method #127

Merged
merged 10 commits into from
Jul 22, 2024
27 changes: 18 additions & 9 deletions packages/matrix-client-server/src/account/3pid/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ const add = (clientServer: MatrixClientServer): expressAppHandler => {
clientServer.uiauthenticate(req, res, allowedFlows, (obj, userId) => {
validateParameters(res, schema, obj, clientServer.logger, (obj) => {
if (!clientSecretRegex.test((obj as RequestBody).client_secret)) {
send(res, 400, errMsg('invalidParam', 'Invalid client_secret'))
send(
res,
400,
errMsg('invalidParam', 'Invalid client_secret'),
clientServer.logger
)
return
}
if (!sidRegex.test((obj as RequestBody).sid)) {
send(res, 400, errMsg('invalidParam', 'Invalid session ID'))
send(
res,
400,
errMsg('invalidParam', 'Invalid session ID'),
clientServer.logger
)
return
}
const body = obj as RequestBody
Expand All @@ -49,12 +59,12 @@ const add = (clientServer: MatrixClientServer): expressAppHandler => {
)
.then((sessionRows) => {
if (sessionRows.length === 0) {
send(res, 400, errMsg('noValidSession'))
send(res, 400, errMsg('noValidSession'), clientServer.logger)
return
}
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!sessionRows[0].validated_at) {
send(res, 400, errMsg('sessionNotValidated'))
send(res, 400, errMsg('sessionNotValidated'), clientServer.logger)
return
}
clientServer.matrixDb
Expand All @@ -63,7 +73,7 @@ const add = (clientServer: MatrixClientServer): expressAppHandler => {
})
.then((rows) => {
if (rows.length > 0) {
send(res, 400, errMsg('threepidInUse'))
send(res, 400, errMsg('threepidInUse'), clientServer.logger)
} else {
clientServer.matrixDb
.insert('user_threepids', {
Expand All @@ -74,16 +84,15 @@ const add = (clientServer: MatrixClientServer): expressAppHandler => {
added_at: epoch()
})
.then(() => {
send(res, 200, {})
send(res, 200, {}, clientServer.logger)
})
.catch((e) => {
// istanbul ignore next
clientServer.logger.error(
'Error while inserting user_threepids',
e
'Error while inserting user_threepids'
)
// istanbul ignore next
send(res, 400, errMsg('unknown', e))
send(res, 400, errMsg('unknown', e), clientServer.logger)
})
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,28 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
const dst = (obj as RequestTokenArgs).email
const nextLink = (obj as RequestTokenArgs).next_link
if (!clientSecretRe.test(clientSecret)) {
send(res, 400, errMsg('invalidParam', 'invalid client_secret'))
send(
res,
400,
errMsg('invalidParam', 'invalid client_secret'),
clientServer.logger
)
} else if (!validEmailRe.test(dst)) {
send(res, 400, errMsg('invalidEmail'))
send(res, 400, errMsg('invalidEmail'), clientServer.logger)
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
} else if (nextLink && !isValidUrl(nextLink)) {
send(res, 400, errMsg('invalidParam', 'invalid next_link'))
send(
res,
400,
errMsg('invalidParam', 'invalid next_link'),
clientServer.logger
)
} else {
clientServer.matrixDb
.get('user_threepids', ['user_id'], { address: dst })
.then((rows) => {
if (rows.length === 0) {
send(res, 400, errMsg('threepidNotFound'))
send(res, 400, errMsg('threepidNotFound'), clientServer.logger)
} else {
clientServer.matrixDb
.get(
Expand All @@ -79,10 +89,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
.then((rows) => {
if (rows.length > 0) {
if (sendAttempt === rows[0].last_send_attempt) {
send(res, 200, {
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
})
send(
res,
200,
{
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
},
clientServer.logger
)
} else {
clientServer.matrixDb
.deleteWhere('threepid_validation_session', [
Expand Down Expand Up @@ -113,9 +128,14 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
})
.catch((err) => {
// istanbul ignore next
clientServer.logger.error('Deletion error', err)
clientServer.logger.error('Deletion error')
// istanbul ignore next
send(res, 500, errMsg('unknown', err))
send(
res,
500,
errMsg('unknown', err),
clientServer.logger
)
})
}
} else {
Expand All @@ -135,17 +155,17 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
})
.catch((err) => {
/* istanbul ignore next */
clientServer.logger.error('Send_attempt error', err)
clientServer.logger.error('Send_attempt error')
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
.catch((err) => {
/* istanbul ignore next */
clientServer.logger.error('Error getting userID :', err)
clientServer.logger.error('Error getting userID')
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,40 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
const dst = formatPhoneNumber(phoneNumber, country)
const nextLink = (obj as RequestTokenArgs).next_link
if (!clientSecretRegex.test(clientSecret)) {
send(res, 400, errMsg('invalidParam', 'Invalid client_secret'))
send(
res,
400,
errMsg('invalidParam', 'Invalid client_secret'),
clientServer.logger
)
} else if (!validCountryRegex.test(country)) {
send(res, 400, errMsg('invalidParam', 'Invalid country'))
send(
res,
400,
errMsg('invalidParam', 'Invalid country'),
clientServer.logger
)
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
} else if (nextLink && !isValidUrl(nextLink)) {
send(res, 400, errMsg('invalidParam', 'Invalid next_link'))
send(
res,
400,
errMsg('invalidParam', 'Invalid next_link'),
clientServer.logger
)
} else if (!validPhoneNumberRegex.test(dst)) {
send(res, 400, errMsg('invalidParam', 'Invalid phone number'))
send(
res,
400,
errMsg('invalidParam', 'Invalid phone number'),
clientServer.logger
)
} else {
clientServer.matrixDb
.get('user_threepids', ['user_id'], { address: dst })
.then((rows) => {
if (rows.length === 0) {
send(res, 400, errMsg('threepidNotFound'))
send(res, 400, errMsg('threepidNotFound'), clientServer.logger)
} else {
clientServer.matrixDb
.get(
Expand All @@ -87,10 +107,15 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
.then((rows) => {
if (rows.length > 0) {
if (sendAttempt === rows[0].last_send_attempt) {
send(res, 200, {
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
})
send(
res,
200,
{
sid: rows[0].session_id,
submit_url: getSubmitUrl(clientServer.conf)
},
clientServer.logger
)
} else {
clientServer.matrixDb
.deleteWhere('threepid_validation_session', [
Expand Down Expand Up @@ -121,9 +146,14 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
})
.catch((err) => {
// istanbul ignore next
console.error('Deletion error:', err)
clientServer.logger.error('Deletion error')
// istanbul ignore next
send(res, 500, errMsg('unknown', err))
send(
res,
500,
errMsg('unknown', err),
clientServer.logger
)
})
}
} else {
Expand All @@ -143,17 +173,17 @@ const RequestToken = (clientServer: MatrixClientServer): expressAppHandler => {
})
.catch((err) => {
/* istanbul ignore next */
console.error('Send_attempt error:', err)
clientServer.logger.error('Send_attempt error')
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
.catch((err) => {
/* istanbul ignore next */
console.error('Error getting userID :', err)
clientServer.logger.error('Error getting userID')
/* istanbul ignore next */
send(res, 500, errMsg('unknown', err))
send(res, 500, errMsg('unknown', err), clientServer.logger)
})
}
})
Expand Down
8 changes: 4 additions & 4 deletions packages/matrix-client-server/src/account/whoami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const whoami = (clientServer: MatrixClientServer): expressAppHandler => {
.then((rows) => {
// istanbul ignore if // might remove the istanbul ignore if an endpoint other than /register modifies the users table
if (rows.length === 0) {
send(res, 403, errMsg('invalidUsername'))
send(res, 403, errMsg('invalidUsername'), clientServer.logger)
return
}
const isGuest = rows[0].is_guest !== 0
Expand All @@ -24,13 +24,13 @@ const whoami = (clientServer: MatrixClientServer): expressAppHandler => {
if (data.device_id) {
body.device_id = data.device_id
}
send(res, 200, body)
send(res, 200, body, clientServer.logger)
})
.catch((e) => {
// istanbul ignore next
clientServer.logger.error('Error while fetching user data', e)
clientServer.logger.error('Error while fetching user data')
// istanbul ignore next
send(res, 500, errMsg('unknown'))
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
}
Expand Down
22 changes: 13 additions & 9 deletions packages/matrix-client-server/src/admin/whois.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,27 @@ const whois = (clientServer: MatrixClientServer): expressAppHandler => {
}
devices[deviceId].sessions.push(sessions[mappings[deviceId]])
})
send(res, 200, {
user_id: prms.userId,
devices
})
send(
res,
200,
{
user_id: prms.userId,
devices
},
clientServer.logger
)
})
.catch((err) => {
.catch((e) => {
// istanbul ignore next
clientServer.logger.error(
'Error retrieving user informations from the MatrixDB',
err
'Error retrieving user informations from the MatrixDB'
)
// istanbul ignore next
send(res, 500, errMsg('unknown'))
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
} else {
send(res, 400, errMsg('missingParams'))
send(res, 400, errMsg('missingParams'), clientServer.logger)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/matrix-client-server/src/devices/changeDevices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ export const changeDeviceName = (
errMsg(
'notFound',
'The current user has no device with the given ID'
)
),
clientServer.logger
)
} else {
clientServer.logger.debug('Device Name updated')
send(res, 200, {})
send(res, 200, {}, clientServer.logger)
}
})
.catch((e) => {
/* istanbul ignore next */
clientServer.logger.error('Error querying profiles:', e)
clientServer.logger.error('Error querying profiles:')
/* istanbul ignore next */
send(res, 500, errMsg('unknown', 'Error querying profiles'))
send(res, 500, errMsg('unknown', e), clientServer.logger)
})
})
})
Expand Down
Loading
Loading