5 Error handling

 

This chapter covers

  • Preventing crashes in our game client
  • Handling errors through error propagation
  • Connecting/reconnecting to the server at runtime
  • The creation of a Future which can be awaited

Up until this point in our journey, we’ve written two relatively stable pieces of software: our game client, and our game’s server. Thanks to Rust, we’ve been able to let our guard down a little more than we might have had to if we were dealing with other languages. We haven’t had to worry about running into undefined behavior from misplaced pointers, or crashes from segfaults since we’re not using unsafe code, and the Rust borrow checker has kept us in line. We’ve also been able to avoid simple mistakes and bugs that might emerge in a non statically typed language like JavaScript. It helps that we can only do so much in our game so far: namely connect to the server and fly around. As we extend our game to be more complex though, we want to be sure that we are writing code in a way that it can continue to remain robust.

5.1 Preventing Crashes

5.1.1 Options and Results

5.1.2 Unwrapping Results

5.1.3 Handling Results

5.2 Error Propagation

5.2.1 The ? operator

5.2.2 Mapping Options to Results

5.2.3 Handling propagated errors

5.3 Corrective Action

5.3.1 Re-attempting failed connection attempts

5.3.2 Establishing a new connection when lost

5.3.3 Making our connection call async

Creating a Future

A Word About Futures

Polling for the TcpStream Connection

Using Coroutines for Concurrency

Creating a WebSocket connection through low level code

5.4 Tools