Bonfire.Common (Bonfire v0.9.12-social-beta.68)

View Source

A library of common utils and helpers used across Bonfire extensions.

An extension for Bonfire that contains:

Handy commands

Copyright (c) 2020 Bonfire, VoxPublica, and CommonsPub Contributors

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

Summary

Functions

Logs or raises errors based on environment.

Provides a fallback value or function when the first argument is nil.

Functions

err(msg)

Logs or raises errors based on environment.

This function handles errors differently depending on the environment:

  • In test: raises an exception
  • In dev: prints a warning
  • In production: logs a warning

Examples

# With just a message
iex> # When in dev/prod (not test), prints a warning and does not raise
iex> # Note: Only testing return value here, not side effects
iex> Process.put([:bonfire, :env], :dev)
iex> err("error message")
# Prints: [warning] error message
nil

# With just data
iex> Process.put([:bonfire, :env], :dev)
iex> err(%{key: "value"})
# Prints: [warning] An error occurred: %{key: "value"}
%{key: "value"}

# With both data and message
iex> Process.put([:bonfire, :env], :dev)
iex> err(%{key: "value"}, "Custom error message")
# Prints: [warning] Custom error message: %{key: "value"}
%{key: "value"}

In test environment, it raises an exception:

iex> Process.put([:bonfire, :env], :test)
iex> err("test error")
** (RuntimeError) test error

err(data, msg)

flood(msg)

flood(data, msg)

maybe_fallback(val, fallback)

Provides a fallback value or function when the first argument is nil.

  • If the first argument is not nil, returns the first argument as is.
  • If both arguments are nil, returns nil.
  • If the first argument is nil and the second argument is a function, calls the function and returns its result.
  • If the first argument is nil and the second argument is not a function, returns the second argument as is.

Examples

iex> maybe_fallback("value", "fallback value")
"value"

iex> maybe_fallback("", "fallback value")
"fallback value"

iex> maybe_fallback(nil, nil)
nil

iex> maybe_fallback(nil, fn -> 1+2 end)
3

iex> maybe_fallback(nil, "fallback value")
"fallback value"

iex> maybe_fallback(%Ecto.Association.NotLoaded{}, "fallback value")
"fallback value"

iex> try do
...>   maybe_fallback(%Ecto.Association.NotLoaded{}, :nil!)
...> rescue
...>   _ -> "exception raised"
...> end
"exception raised"