Skip to content

CHANGE: Add error handling for propogating failures #1326

Closed
@lsampras

Description

@lsampras

A change request is considered a (small) Request-For-Comment, and requires a concrete proposal and motivation.

Summary

Add error handling so that functions can return errors whenever something goes wrong e.g incorrect inputs

Details

Currently this package uses asserts & unwrap's liberally, I want functions to instead return Results so that the callers can call the underlying functions and handle any errors that they receive.

I'd like to start with a few set of commonly used public functions and add validations there which can return errors when failed...
We could later use thiserror+errorstack to build out a more graceful error handling & error stack

Motivation

What is the motivation for this change?

While unwrap's & asserts might suffice for CLI tools, when using this crate in Web applications or deployed services, especially where the input to these functions would be externallly provided... It is imperative that the application does not crash due to any weird user input...

One such case could be that we take in a lo & hi number from user & generate a random number between them using

gen_range(lo..hi)
# but this will crash if lo >= hi
due to the following assertion in library
assert!(!range.is_empty(), "cannot sample empty range");

In such scenarios it isn't the right expectation to ask the callers to validate the input for all such variants

Alternatives

Which alternatives might be considered, and why or why not?

Depend on the caller to validate all such constraints, but keeping a track of our invariants on which we assert is also tricky

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions