Skip to content
This repository has been archived by the owner on Nov 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #4 from silathdiir/replacing_config_prefix_with_ke…
Browse files Browse the repository at this point in the history
…y_mapping

Releases `0.0.2`
  • Loading branch information
Steven committed Jan 15, 2018
2 parents 2dcd53e + 83bb10b commit 217282d
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 203 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: elixir
elixir:
- 1.5.3
otp_release:
- 19.3
- 20.2
dist: trusty
notifications:
recipients:
- [email protected]
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

## Schedule

### Release `0.0.2`

1. Removes 'prefix' configuration and adds Key Mapping.
1. Integrates with Travis CI.

### Release `0.0.1`

* Exports function `rat_error` (after calling `use RatError`).
* Exports `rat_error` configuration (see `config/*.exs` for detail).
* Implements module `RatError.Formatter` and `RatError.Structure`.
1. Exports function `rat_error` (after calling `use RatError`).
1. Exports `rat_error` configuration (see `config/*.exs` for detail).
1. Implements module `RatError.Formatter` and `RatError.Structure`.
68 changes: 37 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Rat Error

[![Build Status](https://travis-ci.org/silathdiir/rat_error.svg?branch=master)](https://travis-ci.org/silathdiir/rat_error)

Provides helper functions for error handling:

* detailed error description.
Expand All @@ -10,7 +12,7 @@ Provides helper functions for error handling:
```elixir
def deps do
[
{:rat_error, "~> 0.0.1"}
{:rat_error, "~> 0.0.2"}
]
end
```
Expand All @@ -19,9 +21,14 @@ end

```elixir
config :rat_error, RatError.Structure,
# Node Name, the default value is 'error'.
# Node Name
#
# If node is 'err', the structure is,
# The node is removed if key `node` is not existing or value is nil. Error
# fields are exposed outside, and the below configuration `keys` must be set
# to distinguish with other caller's keys.
node: :err,

# So the structure should be,
#
# %{
# err:
Expand All @@ -34,47 +41,46 @@ config :rat_error, RatError.Structure,
# module: Elixir.MyApp.Registration.UserController
# }
# }
#
# If node is nil or an empty string, the node is removed. The fields are
# exposed outside, and the below configuration 'prefix' could be set to
# distinguish with other caller's keys.
node: :error,

# Field Prefix, the default value is nil (NO prefix).
# Support Key Mapping
#
# If node is nil and prefix is 'err_', the structure is,
#
# %{
# err_code: :invalid_argument,
# err_file: "/home/dummy/my_app/web/user_controller.ex",
# err_function: {:authenticate, 1},
# err_line: 123,
# err_message: "wrong token!",
# err_module: Elixir.MyApp.Registration.UserController
# }
#
prefix: nil,

# Support Keys
# This Map must be set with the original fields (as Map keys). Map keys are
# original fields (see the below description for detail). Map values are
# custom field names (for formatting, they could be the same atoms as keys).
# The field is ignored if key is not existing or value is nil.
keys:
[
%{
# Error code defined by caller, e.g. an atom :no_entry, an integer 9 or a
# string "unexpected".
:code,
code: :err_code,

# Error file path.
:file,
file: :file,

# Error function name.
:function,
function: :function,

# Error file line.
:line,
line: :line,

# Error message of string passed in by caller.
:message,
message: :err_msg,

# Error module.
:module
]
module: :module
}

# So the structure should be,
#
# %{
# err:
# %{
# err_code: :invalid_argument,
# err_msg: "wrong token!",
# file: "/home/dummy/my_app/web/user_controller.ex",
# function: {:authenticate, 1},
# line: 123,
# module: Elixir.MyApp.Registration.UserController
# }
# }
```
65 changes: 8 additions & 57 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,61 +1,12 @@
use Mix.Config

config :rat_error, RatError.Structure,
# Node Name, the default value is 'error'.
#
# If node is 'err', the structure is,
#
# %{
# err:
# %{
# code: :invalid_argument,
# file: "/home/dummy/my_app/web/user_controller.ex",
# function: {:authenticate, 1},
# line: 123,
# message: "wrong token!",
# module: Elixir.MyApp.Registration.UserController
# }
# }
#
# If node is nil or an empty string, the node is removed. The fields are
# exposed outside, and the below configuration 'prefix' could be set to
# distinguish with other caller's keys.
node: :error,

# Field Prefix, the default value is nil (NO prefix).
#
# If node is nil and prefix is 'err_', the structure is,
#
# %{
# err_code: :invalid_argument,
# err_file: "/home/dummy/my_app/web/user_controller.ex",
# err_function: {:authenticate, 1},
# err_line: 123,
# err_message: "wrong token!",
# err_module: Elixir.MyApp.Registration.UserController
# }
#
prefix: nil,

# Support Keys
keys:
[
# Error code defined by caller, e.g. an atom :no_entry, an integer 9 or a
# string "unexpected".
:code,

# Error file path.
:file,

# Error function name.
:function,

# Error file line.
:line,

# Error message of string passed in by caller.
:message,

# Error module.
:module
]
keys: %{
code: :code,
file: :file,
function: :function,
line: :line,
message: :message,
module: :module
}
8 changes: 5 additions & 3 deletions config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use Mix.Config

config :rat_error, RatError.Structure,
node: :error,
prefix: nil,
keys: :code
node: :error,
keys: %{
code: :code,
message: :message
}
20 changes: 9 additions & 11 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use Mix.Config

config :rat_error, RatError.Structure,
node: :error,
prefix: nil,
keys:
[
:code,
:file,
:function,
:line,
:message,
:module
]
node: :error,
keys: %{
code: :code,
file: :file,
function: :function,
line: :line,
message: :message,
module: :module
}
6 changes: 3 additions & 3 deletions lib/rat_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ defmodule RatError do
Parameter 'opts' merges with the configuration in 'config/test.exs'.
iex> opts = [keys: [:code, :message]]
iex> opts = [keys: %{code: :code, message: :message}]
iex> rat_error(:wrong_password, "Wrong password!", opts)
%{error: %{code: :wrong_password, message: "Wrong password!"}}
"""
defmacro rat_error(error_code \\ nil, error_message \\ "", opts \\ []) do
quote(bind_quoted: [error_code: error_code,
quote(bind_quoted: [error_code: error_code,
error_message: error_message,
opts: opts],
opts: opts],
location: :keep) do
structure = Structure.update(@structure, opts)

Expand Down
23 changes: 14 additions & 9 deletions lib/rat_error/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ defmodule RatError.Formatter do
## Examples
iex> structure = %Structure{node: :err, keys: [:code, :message]}
iex> support_keys = %{code: :code, message: :message}
iex> structure = %Structure{node: :err, keys: support_keys}
iex> message = "Bad response!"
iex> Formatter.format(structure, __ENV__, :bad_response, message)
%{err: %{code: :bad_response, message: "Bad response!"}}
iex> structure = %Structure{keys: [:code, :message]}
iex> support_keys = %{code: :code, message: :message}
iex> structure = %Structure{keys: support_keys}
iex> message = "Out of memory!"
iex> Formatter.format(structure, __ENV__, :no_memory, message)
%{code: :no_memory, message: "Out of memory!"}
Expand All @@ -50,17 +52,16 @@ defmodule RatError.Formatter do
end
end

defp add_field(nil, _value, params), do: params
defp add_field(key, value, params), do: Map.put(params, key, value)

defp format_code(params, structure, value),
do: format_entry(params, structure, :code, value)

defp format_entry(params, structure, key, value) when is_atom(key) do
if structure.keys |> List.wrap |> Enum.member?(key) do
new_key = String.to_atom(to_string(structure.prefix) <> to_string(key))

Map.put(params, new_key, value)
else
params
end
structure.keys
|> get_field_name(key)
|> add_field(value, params)
end

defp format_env_values(params, structure, env) do
Expand All @@ -70,4 +71,8 @@ defmodule RatError.Formatter do

defp format_message(params, structure, value),
do: format_entry(params, structure, :message, value)

defp get_field_name(nil, _key), do: nil
defp get_field_name(support_keys, key) when is_map(support_keys),
do: support_keys[key]
end
Loading

0 comments on commit 217282d

Please sign in to comment.