From 9b71e8119e950b834278296a5b79ba266dc4b045 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 22 Jan 2019 10:40:07 +1100 Subject: [PATCH] Added error catching. --- .prettierignore | 2 +- libloki/local_loki_server.js | 8 ++++++- libtextsecure/http-resources.js | 36 +++++++++++++++++++------------ libtextsecure/message_receiver.js | 2 +- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/.prettierignore b/.prettierignore index 2dbd12114..eb59d7d0b 100644 --- a/.prettierignore +++ b/.prettierignore @@ -52,4 +52,4 @@ stylesheets/_intlTelInput.scss # Coverage coverage/** -.nyc_output/** \ No newline at end of file +.nyc_output/** diff --git a/libloki/local_loki_server.js b/libloki/local_loki_server.js index aa4dfcc44..3bfe40887 100644 --- a/libloki/local_loki_server.js +++ b/libloki/local_loki_server.js @@ -23,7 +23,13 @@ class LocalLokiServer extends EventEmitter { body.push(chunk); }) .on('end', () => { - body = Buffer.concat(body).toString(); + try { + body = Buffer.concat(body).toString(); + } catch (e) { + // Error occurred while converting to string + res.statusCode = 500; + res.end(); + } // Check endpoints here if (req.url === '/store') { diff --git a/libtextsecure/http-resources.js b/libtextsecure/http-resources.js index 12e736cbd..6f6a8f815 100644 --- a/libtextsecure/http-resources.js +++ b/libtextsecure/http-resources.js @@ -75,21 +75,29 @@ }; this.handleMessage = message => { - const dataPlaintext = stringToArrayBufferBase64(message); - const messageBuf = textsecure.protobuf.WebSocketMessage.decode( - dataPlaintext - ); - if ( - messageBuf.type === textsecure.protobuf.WebSocketMessage.Type.REQUEST - ) { - handleRequest( - new IncomingHttpResponse({ - verb: messageBuf.request.verb, - path: messageBuf.request.path, - body: messageBuf.request.body, - id: messageBuf.request.id, - }) + try { + const dataPlaintext = stringToArrayBufferBase64(message); + const messageBuf = textsecure.protobuf.WebSocketMessage.decode( + dataPlaintext ); + if ( + messageBuf.type === textsecure.protobuf.WebSocketMessage.Type.REQUEST + ) { + handleRequest( + new IncomingHttpResponse({ + verb: messageBuf.request.verb, + path: messageBuf.request.path, + body: messageBuf.request.body, + id: messageBuf.request.id, + }) + ); + } + } catch (error) { + const info = { + message, + error: error.message, + }; + window.log.warn('HTTP-Resources Failed to handle message:', info); } }; diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 86f0973ec..dd01d2639 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -89,7 +89,7 @@ MessageReceiver.prototype.extend({ this.localServer .start(0) .then(port => - window.log.info(`Local Server started at https://localhost:${port}`) + window.log.info(`Local Server started at localhost:${port}`) ); // TODO: Rework this socket stuff to work with online messaging