From 63dde6e5cf4b213a9088c448f4439cd8a1ee1f33 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Thu, 12 May 2022 09:16:13 -0700 Subject: [PATCH] Exceptions Summary: Flipper doesn really use library specific exceptions throughout, and that's OK. Introducing SSLException as a replacement for the existing Folly Async Socket SSL exception. This exception originally thrown by rsocket. Because we had rsocket and websockets using the same code, websockets were creating and throwing this same exception. With rsocket gone, we can fully replace the usage of that exception. This is also needed as to decouple Flipper from folly async components. Reviewed By: fabiomassimo Differential Revision: D36245624 fbshipit-source-id: f5c97c5efe063280ce95be130008dee7f4e5d788 --- iOS/FlipperKit/FlipperWebSocket.mm | 6 ++-- .../Flipper/FlipperConnectionManagerImpl.cpp | 17 ++++------- xplat/Flipper/FlipperExceptions.h | 30 +++++++++++++++++++ xplat/FlipperWebSocket/WebSocketClient.cpp | 1 - xplat/FlipperWebSocket/WebSocketTLSClient.cpp | 6 ++-- 5 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 xplat/Flipper/FlipperExceptions.h diff --git a/iOS/FlipperKit/FlipperWebSocket.mm b/iOS/FlipperKit/FlipperWebSocket.mm index 29a9113bd..3389d7791 100644 --- a/iOS/FlipperKit/FlipperWebSocket.mm +++ b/iOS/FlipperKit/FlipperWebSocket.mm @@ -9,12 +9,12 @@ #import "FlipperWebSocket.h" #import +#import #import #import #import #import #import -#import #import #import #import @@ -94,9 +94,7 @@ bool FlipperWebSocket::connect(FlipperConnectionManager* manager) { } else if (event == SocketEvent::SSL_ERROR) { try { promise.set_exception( - std::make_exception_ptr(folly::AsyncSocketException( - folly::AsyncSocketException::SSL_ERROR, - "SSL handshake failed"))); + std::make_exception_ptr(SSLException("SSL handshake failed"))); } catch (...) { // set_exception() may throw an exception // In that case, just set the value to false. diff --git a/xplat/Flipper/FlipperConnectionManagerImpl.cpp b/xplat/Flipper/FlipperConnectionManagerImpl.cpp index 66e59640d..ee2dab331 100644 --- a/xplat/Flipper/FlipperConnectionManagerImpl.cpp +++ b/xplat/Flipper/FlipperConnectionManagerImpl.cpp @@ -8,13 +8,13 @@ #include "FlipperConnectionManagerImpl.h" #include #include -#include #include #include #include #include #include "ConnectionContextStore.h" #include "FireAndForgetBasedFlipperResponder.h" +#include "FlipperExceptions.h" #include "FlipperSocketProvider.h" #include "FlipperStep.h" #include "Log.h" @@ -178,16 +178,11 @@ void FlipperConnectionManagerImpl::startSync() { } } step->complete(); - } catch (const folly::AsyncSocketException& e) { - if (e.getType() == folly::AsyncSocketException::SSL_ERROR) { - auto message = std::string(e.what()) + - "\nMake sure the date and time of your device is up to date."; - log(message); - step->fail(message); - } else { - log(e.what()); - step->fail(e.what()); - } + } catch (const SSLException& e) { + auto message = std::string(e.what()) + + "\nMake sure the date and time of your device is up to date."; + log(message); + step->fail(message); failedConnectionAttempts_++; reconnect(); } catch (const std::exception& e) { diff --git a/xplat/Flipper/FlipperExceptions.h b/xplat/Flipper/FlipperExceptions.h new file mode 100644 index 000000000..385db6296 --- /dev/null +++ b/xplat/Flipper/FlipperExceptions.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +namespace facebook { +namespace flipper { + +class SSLException : public std::exception { + public: + explicit SSLException(const char* message) : msg_(message) {} + + explicit SSLException(const std::string& message) : msg_(message) {} + + virtual ~SSLException() noexcept {} + + virtual const char* what() const noexcept { + return msg_.c_str(); + } + + protected: + std::string msg_; +}; + +} // namespace flipper +} // namespace facebook diff --git a/xplat/FlipperWebSocket/WebSocketClient.cpp b/xplat/FlipperWebSocket/WebSocketClient.cpp index 5e85007ab..b828a61b3 100644 --- a/xplat/FlipperWebSocket/WebSocketClient.cpp +++ b/xplat/FlipperWebSocket/WebSocketClient.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include diff --git a/xplat/FlipperWebSocket/WebSocketTLSClient.cpp b/xplat/FlipperWebSocket/WebSocketTLSClient.cpp index be3357fcb..f4b029797 100644 --- a/xplat/FlipperWebSocket/WebSocketTLSClient.cpp +++ b/xplat/FlipperWebSocket/WebSocketTLSClient.cpp @@ -9,12 +9,12 @@ #include "WebSocketTLSClient.h" #include +#include #include #include #include #include #include -#include #include #include #include @@ -246,9 +246,7 @@ void WebSocketTLSClient::onFail( if (sslError) { try { connected_.set_exception( - std::make_exception_ptr(folly::AsyncSocketException( - folly::AsyncSocketException::SSL_ERROR, - "SSL handshake failed"))); + std::make_exception_ptr(SSLException("SSL handshake failed"))); } catch (...) { // set_exception() may throw an exception // In that case, just set the value to false.