Error in Elixir : returned a bad value: :ok
Asked Answered
I

1

6

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?

Intellectuality answered 12/11, 2017 at 8:56 Comment(0)
O
13

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
Ochone answered 12/11, 2017 at 9:5 Comment(1)
{:ok, self()}Bolivar

© 2022 - 2024 — McMap. All rights reserved.