DEV Community

Mạnh Vũ
Mạnh Vũ

Posted on • Edited on

Elixir Tricks & Tips for debugging & developing

Intro

I will continue add tips & tricks for develop/debugging with Elixir in this topic for sharing my experiences about Elixir.

Add .iex.exs file for easy develop & debug

Add .iex.exs file to root folder of Elixir app then:

1. Add alias to avoid type much

example add

alias MyApp.Background.Worker, as: W

defmodule FillData do
  def add_sample() do
    Cache.add_room(:hello, "Hello room", nil)
    Cache.add_room_members(:hello, ["user_1", "user_2", "user_3"], nil)
  end
end
Enter fullscreen mode Exit fullscreen mode

to your .iex.exs and run with iex -S mix or iex -S mix phx.server then in Elixir shell you can type like:

W.run()

# For add sample data for developing
FillData.add_sample()  # Or add directly to .iex.exs
Enter fullscreen mode Exit fullscreen mode

2. Add function/script support for debugging.
You can add a script or function needed for debugging then run in Elixir shell.

rescue in body function

when define a function you can directly use rescue in body without try keyword.

Example:

  def server_info(endpoint, scheme) do
    address =
      endpoint
      |> make_ref(scheme)
      |> :ranch.get_addr()

    {:ok, address}
  rescue
    e -> {:error, Exception.message(e)}
  end
Enter fullscreen mode Exit fullscreen mode

(code from :swarm library)

Add support :observer

:observer is interesting tool for view system statistics, app structure like supervisor, process, ets table,...

We can add to dev mode to mix.exs file like:

defmodule MyApp.MixProject do
  # ...

  def application do
    dev_app =
      case Mix.env() do
        :dev -> [:observer, :wx]
        _ -> []
      end

    required_app = [:logger, :runtime_tools]

    [
      mod: {EmbeddedChat.Application, []},
      extra_applications: required_app ++ dev_app
    ]
  end
end
Enter fullscreen mode Exit fullscreen mode

Now, when you start dev mode you can start :observer by simple command :observer.start()

Note: :observer need graphic library to show system data then we need to start it on a OS support that (Windows/macOS/Linux with graphic). For case need view system that doesn't have graphic library like container in Kubernetes cluster, we can join to remote node from other machine and start :observer.

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Okay, let's go

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay