Skip to content

Commit

Permalink
Network: try-with with specific exceptions
Browse files Browse the repository at this point in the history
Previously, this block had a generic try-with which is
dangerous since it can cause unwanted behaviours and
cause confusion for future developers.
  • Loading branch information
parhamsaremi committed Nov 21, 2022
1 parent d750cfe commit d16f92f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NOnion/Exceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type CircuitTruncatedException internal (reason: DestroyReason) =
type CircuitDestroyedException internal (reason: DestroyReason) =
inherit NOnionException(sprintf "Circuit got destroyed, reason %A" reason)

type CircuitDecryptionFailedException internal () =
inherit NOnionException(sprintf "Circuit Decryption Failed")

type TimeoutErrorException internal () =
inherit NOnionException("Time limit exceeded for operation")

Expand Down
3 changes: 2 additions & 1 deletion NOnion/Network/TorCircuit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ and TorCircuit
node)
| None ->
announceDeath()
failwith "Decryption failed!"

raise <| CircuitDecryptionFailedException()

decryptMessage encryptedRelayCell.EncryptedData nodes
| _ -> failwith "Unexpected state when receiving relay cell"
Expand Down
3 changes: 2 additions & 1 deletion NOnion/Network/TorGuard.fs
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,15 @@ type TorGuard private (client: TcpClient, sslStream: SslStream) =
try
do! circuit.HandleIncomingCell cell
with
| ex ->
| :? CircuitDecryptionFailedException as ex ->
sprintf
"TorGuard: exception when trying to handle incoming cell type=%i, ex=%s"
cell.Command
(ex.ToString())
|> TorLogger.Log

self.KillChildCircuits()
| ex -> return raise <| FSharpUtil.ReRaise ex
| None ->
self.KillChildCircuits()
failwithf "Unknown circuit, Id = %i" cid
Expand Down

0 comments on commit d16f92f

Please sign in to comment.