Verified Commit bc824e7e authored by aguestuser's avatar aguestuser
Browse files

[nostory] mitigate funky stuff w.r.t. safety number changes

on 10/1/2019 - 10/3/2019, we observed some funny errors (including
halting errors) after merging #100 (which introduced changed safety
number handling)

symptoms:
* lots of log statements with undefined values (traceable to
  `logAndReturn` statements having unexpected shapes after calls to
  trust safety numbers)
* calls to JSON.parse in safety number changing code in the signal
  service throwing unexpectedly

rather than provide a full fix for the bug, this MR introduces some
instrumentation to help better diagnose the problem, and -- in the
meantime -- prevent it from crashing the app.

more specifically it:
* introduces more robust `logAndReturn` statements that will show us
  whatever is happening around safety number change results
* introduces a safer implementation of `JSON.parse` that will reject a
  Promise (which itself should be caught and logged) if message
  parsing ever fails
parent ef7b0a4a
......@@ -173,7 +173,7 @@ const verify = (sock, phoneNumber, code) =>
const awaitVerificationResult = async (sock, phoneNumber) => {
return new Promise((resolve, reject) => {
sock.on('data', function handle(msg) {
const { type, data } = JSON.parse(msg)
const { type, data } = jsonParseOrReject(msg, reject)
if (isVerificationSuccess(type, data, phoneNumber)) {
sock.removeListener('data', handle)
resolve(data)
......@@ -265,7 +265,7 @@ const awaitIdentitiesOf = (sock, memberPhoneNumber) => {
return new Promise((resolve, reject) => {
// create handler
const handle = msg => {
const { type, data } = JSON.parse(msg)
const { type, data } = jsonParseOrReject(msg, reject)
if (isSignalIdentitiesOf(type, data, memberPhoneNumber)) {
sock.removeListener('data', handle)
resolve(data.identities)
......@@ -291,6 +291,14 @@ const isSignalIdentitiesOf = (msgType, msgData, memberPhoneNumber) =>
* MESSAGE PARSING
*******************/
const jsonParseOrReject= (msg, reject) => {
try {
return JSON.parse(msg)
} catch (e) {
reject(e)
}
}
// InboundMessage -> OutboundMessage
const parseOutboundSdMessage = inboundSdMessage => {
const {
......
......@@ -54,7 +54,7 @@ const loggerOf = prefix =>
: {
log: msg => console.log(`[${prefix} | ${nowTimestamp()}] ${msg}`),
logAndReturn: sbStatus => {
console.log(`[${prefix} | ${nowTimestamp()}] ${sbStatus.message}`)
console.log(`[${prefix} | ${nowTimestamp()}] ${JSON.stringify(sbStatus)}`)
return sbStatus
},
error: e => console.error(`[${prefix} | ${nowTimestamp()}] ${e.stack}`),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment