Skuld.Comp.IThrowable protocol (skuld v0.1.26)
View SourceProtocol for unwrapping exceptions in Throw.try_catch/1.
When an exception is caught inside a computation and processed by
Throw.try_catch/1, this protocol determines how the exception is
converted to an error value.
Default Behavior
By default (via the Any fallback), exceptions are returned as-is:
raise ArgumentError, "bad input"
# try_catch returns: {:error, %ArgumentError{message: "bad input"}}Custom Unwrapping
Domain exceptions can implement this protocol to provide cleaner error values suitable for pattern matching:
defmodule MyApp.NotFoundError do
defexception [:entity, :id]
@impl true
def message(%{entity: entity, id: id}) do
"#{entity} not found: #{id}"
end
end
defimpl Skuld.Comp.IThrowable, for: MyApp.NotFoundError do
def unwrap(%{entity: entity, id: id}), do: {:not_found, entity, id}
endNow when code raises this exception:
raise MyApp.NotFoundError, entity: :user, id: 123
# try_catch returns: {:error, {:not_found, :user, 123}}This enables clean pattern matching on domain errors:
case result do
{:ok, user} -> handle_user(user)
{:error, {:not_found, :user, id}} -> handle_not_found(id)
{:error, %ArgumentError{}} -> handle_bad_input()
endWhen to Implement
Implement IThrowable for exceptions that represent domain errors -
expected failures that are part of your business logic. Examples:
- Validation failures
- Resource not found
- Permission denied
- Business rule violations
Leave the default behavior for unexpected errors - bugs or system failures where you want the full exception for debugging:
ArgumentError,KeyError, etc.- Database connection errors
- External service failures
Summary
Functions
Extract the error value from an exception.
Types
@type t() :: term()
All the types that implement this protocol.