-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
:defexception
denfiner to define an exception (#19)
- Loading branch information
Showing
5 changed files
with
161 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,26 @@ iex> User.Profile.changeset(%User.Profile{}, %{"email" => "[email protected]"}) | |
valid?: true | ||
> | ||
``` | ||
## Define an Exception | ||
|
||
In Elixir, an exception is defined as a struct that includes a special field named `__exception__`. | ||
To define an exception, use the `defexception` definer within the `typed_structor` block. | ||
|
||
```elixir | ||
defmodule HTTPException do | ||
use TypedStructor | ||
|
||
typed_structor definer: :defexception, enforce: true do | ||
field :status, non_neg_integer() | ||
end | ||
|
||
@impl Exception | ||
def message(%__MODULE__{status: status}) do | ||
"HTTP status #{status}" | ||
end | ||
end | ||
``` | ||
|
||
## Documentation | ||
|
||
To add a `@typedoc` to the struct type, just add the attribute in the typed_structor block: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule TypedStructor.Definer.Defexception do | ||
@moduledoc """ | ||
A definer to define an exception and a type for a given definition. | ||
## Additional options for `typed_structor` | ||
* `:define_struct` - if `false`, the type will be defined, but the struct will not be defined. Defaults to `true`. | ||
## Usage | ||
defmodule MyException do | ||
use TypedStructor | ||
typed_structor definer: :defexception, define_struct: false do | ||
field :message, String.t() | ||
end | ||
end | ||
""" | ||
|
||
alias TypedStructor.Definer.Defstruct | ||
|
||
@doc """ | ||
Defines an exception and a type for a given definition. | ||
""" | ||
defmacro define(definition) do | ||
quote do | ||
unquote(__MODULE__).__exception_ast__(unquote(definition)) | ||
|
||
require Defstruct | ||
Defstruct.__type_ast__(unquote(definition)) | ||
end | ||
end | ||
|
||
@doc false | ||
defmacro __exception_ast__(definition) do | ||
quote bind_quoted: [definition: definition] do | ||
if Keyword.get(definition.options, :define_struct, true) do | ||
{fields, enforce_keys} = | ||
Defstruct.__extract_fields_and_enforce_keys__(definition) | ||
|
||
@enforce_keys Enum.reverse(enforce_keys) | ||
defexception fields | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters