Skip to content

Commit d16f92f

Browse files
committed
Network: try-with with specific exceptions
Previously, this block had a generic try-with which is dangerous since it can cause unwanted behaviours and cause confusion for future developers.
1 parent d750cfe commit d16f92f

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

NOnion/Exceptions.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type CircuitTruncatedException internal (reason: DestroyReason) =
2626
type CircuitDestroyedException internal (reason: DestroyReason) =
2727
inherit NOnionException(sprintf "Circuit got destroyed, reason %A" reason)
2828

29+
type CircuitDecryptionFailedException internal () =
30+
inherit NOnionException(sprintf "Circuit Decryption Failed")
31+
2932
type TimeoutErrorException internal () =
3033
inherit NOnionException("Time limit exceeded for operation")
3134

NOnion/Network/TorCircuit.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ and TorCircuit
336336
node)
337337
| None ->
338338
announceDeath()
339-
failwith "Decryption failed!"
339+
340+
raise <| CircuitDecryptionFailedException()
340341

341342
decryptMessage encryptedRelayCell.EncryptedData nodes
342343
| _ -> failwith "Unexpected state when receiving relay cell"

NOnion/Network/TorGuard.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,15 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
332332
try
333333
do! circuit.HandleIncomingCell cell
334334
with
335-
| ex ->
335+
| :? CircuitDecryptionFailedException as ex ->
336336
sprintf
337337
"TorGuard: exception when trying to handle incoming cell type=%i, ex=%s"
338338
cell.Command
339339
(ex.ToString())
340340
|> TorLogger.Log
341341

342342
self.KillChildCircuits()
343+
| ex -> return raise <| FSharpUtil.ReRaise ex
343344
| None ->
344345
self.KillChildCircuits()
345346
failwithf "Unknown circuit, Id = %i" cid

0 commit comments

Comments
 (0)