DNS.Result (DNS v0.5.0)

View Source

Standardized result types for DNS operations.

This module provides consistent error handling patterns throughout the DNS library, replacing the mix of throw/1 and {:error, reason} tuples with a unified approach.

Summary

Functions

Convert legacy throw-based functions to result-based functions.

Execute a function with a value, catching throws and converting to errors.

Get the error from a result or nil if successful.

Create a standardized error result.

Chain operations that return results.

Map over a successful result, leaving errors unchanged.

Wrap a successful result in the standard format.

Macro to wrap throw-based functions with result handling.

Extract the value from a result or return a default.

Types

t(a)

@type t(a) :: {:ok, a} | {:error, DNS.Error.t()}

Functions

__using__(opts)

(macro)

Convert legacy throw-based functions to result-based functions.

catch_throw(fun)

@spec catch_throw((-> a)) :: t(a) when a: any()

Execute a function with a value, catching throws and converting to errors.

error(arg)

@spec error(t(any())) :: DNS.Error.t() | nil

Get the error from a result or nil if successful.

error(type, module, reason, context \\ %{})

@spec error(DNS.Error.error_type(), module(), term(), map()) :: t(any())

Create a standardized error result.

flat_map(result, fun)

@spec flat_map(t(a), (a -> t(b))) :: t(b) when a: any(), b: any()

Chain operations that return results.

map(result, fun)

@spec map(t(a), (a -> b)) :: t(b) when a: any(), b: any()

Map over a successful result, leaving errors unchanged.

ok(value)

@spec ok(any()) :: t(any())

Wrap a successful result in the standard format.

result(list)

(macro)

Macro to wrap throw-based functions with result handling.

unwrap(arg, default)

@spec unwrap(t(a), a) :: a when a: any()

Extract the value from a result or return a default.