Custom errors and the Error trait
A non-trivial project that has varied functionality is often spread across modules. With an organization, it's more informative to provide module-specific error messages and information for the user. Rust allows us to create custom error types that can help us achieve more granular error reports from our application. Without custom errors that are specific to our project, we might have to use existing error types in the standard library, which may not be relevant to our API's operations and will not give precise information to users if things go wrong with an operation in our module.
In languages that have exceptions, such as Java, the way you create custom exceptions is by inheriting from the base Exception
class and overriding its methods and member variables. While Rust doesn't have type-level inheritance, it has trait inheritance and provides us with the Error
trait that any type can implement, making the type a custom error type. This type can now be...