retry

Package Version Hex Docs

A Gleam library retries code that can fail.

Usage

gleam add retry@1
import gleam/int
import gleam/retry

pub type NetworkError {
  ServerDown
  Timeout(Int)
  InvalidStatusCode(Int)
  InvalidResponseBody(String)
}

pub fn main() {
  retry.new(max_attempts: 5, wait_time: 100)
  // Optional configuration
  |> retry.allow(allow: fn(error) {
    case error {
      InvalidStatusCode(code) if code >= 500 && code < 600 -> True
      Timeout(_) -> True
      _ -> False
    }
  })
  // Optional configuration
  |> retry.backoff(next_wait_time: int.multiply(_, 2))
  |> retry.execute(operation: fn() {
    case int.random(4) {
      0 -> Error(ServerDown)
      1 -> Error(Timeout(5000))
      2 -> Error(InvalidStatusCode(503))
      3 -> Error(InvalidResponseBody("Malformed JSON"))
      _ -> Ok("Success!")
    }
  })
}

Targets

retry supports the Erlang target.

Search Document