View Source DataSchema.Errors (data_schema v0.4.2)

When we create a struct we either return the struct we were creating or we return this error. The error/errors that happened during struct creation are collected into this struct

Link to this section Summary

Types

t()

An error is the struct key that caused the error and either an error message or a DataSchema.Errors struct in the case of nested error.

Functions

Adds an error to the given errors struct. The error is prepended to the list of current errors.

Returns an error tuple of the path to the problematic field and the error message.

Turns the DataSchema.Errors struct into a flattened error tuple of path to field and error message

Link to this section Types

Specs

error_message() :: String.t()

Specs

path_to_error() :: [atom()]

Specs

t() :: %DataSchema.Errors{errors: [{atom(), String.t() | t()}]}

An error is the struct key that caused the error and either an error message or a DataSchema.Errors struct in the case of nested error.

Link to this section Functions

Link to this function

add_error(errors, error)

View Source

Adds an error to the given errors struct. The error is prepended to the list of current errors.

Returns an error tuple of the path to the problematic field and the error message.

Usually errors are returned as nested DataSchema.Errors structs. This was to help cater for the possibility of collecting all errors, but right now we stop casting as soon as we error on a casting function, so errors are a little confusing. This function can be used to return a flattened error.

Examples

iex> error = %DataSchema.Errors{
...>    errors: [
...>      comments: %DataSchema.Errors{
...>        errors: [author:
...>          %DataSchema.Errors{
...>            errors: [name: "There was an error!"]
...>          }
...>        ]
...>      }
...>    ]
...>   }
...> DataSchema.Errors.flatten_errors(error)
{[:comments, :author, :name], "There was an error!"}

Specs

to_error_tuple(t()) :: {:error, {path_to_error(), error_message()}}

Turns the DataSchema.Errors struct into a flattened error tuple of path to field and error message

Examples

iex> error = %DataSchema.Errors{
...>    errors: [
...>      comments: %DataSchema.Errors{
...>        errors: [author:
...>          %DataSchema.Errors{
...>            errors: [name: "There was an error!"]
...>          }
...>        ]
...>      }
...>    ]
...>   }
...> DataSchema.Errors.to_error_tuple(error)
{:error, {[:comments, :author, :name], "There was an error!"}}