Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in Elixir : returned a bad value: :ok

I have some problem while working on Elixir. This is my mix.exs...

defmodule FlockTest.Mixfile do
  use Mix.Project

  def project do
    [app: :flock_test,
     version: "0.1.0",
     elixir: "~> 1.4",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps()]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    # Specify extra applications you'll use from Erlang/Elixir
    [
      extra_applications: [:logger],
      mod: {FlockTest, []}
    ]
  end
...
...

and this is code for FlockTest.

defmodule FlockTest do
  @moduledoc """
  Documentation for FlockTest.
  """

  def start(_type, _args) do
    IO.puts "Start Flock"
  end
end

I run this code with mix run --no-halt but this caused error like this.

=INFO REPORT==== 12-Nov-2017::17:47:39 ===
    application: logger
    exited: stopped
    type: temporary
** (Mix) Could not start application flock_test: FlockTest.start(:normal,     
[]) returned a bad value: :ok

Am I doing something wrong?

like image 867
Dennis Jung Avatar asked Nov 12 '17 08:11

Dennis Jung


1 Answers

Your application's start/2 function should start and return the top level process of the application, usually a supervisor instance. Right now you return the value returned by IO.puts, which is :ok on success. The valid return values are {:ok, pid} | {:ok, pid, state} | {:error, reason :: term} as documented here. For an example of this, you can create a new application using mix new foo --sup and check out lib/foo/application.ex. Here's how its start/2 looks like:

def start(_type, _args) do
  # List all child processes to be supervised
  children = [
    # Starts a worker by calling: A.Worker.start_link(arg)
    # {A.Worker, arg},
  ]

  # See https://hexdocs.pm/elixir/Supervisor.html
  # for other strategies and supported options
  opts = [strategy: :one_for_one, name: A.Supervisor]
  Supervisor.start_link(children, opts)
end
like image 89
Dogbert Avatar answered Sep 27 '22 22:09

Dogbert