defmodule Sqlite.DbConnection.Error do
  defexception [:message, :sqlite, :postgres]

  # TODO: Retrofit to actual SQLite error schema.

  @nonposix_errors [:closed, :timeout]

  def message(e) do
    if kw = e.postgres do
      "#{kw[:severity]} (#{kw[:code]}): #{kw[:message]}"
    else
      e.message
    end
  end

  def exception([postgres: fields]) do
    fields = Enum.into(fields, %{})
             |> Map.put(:pg_code, fields[:code])
             |> Map.update!(:code, &Sqlite.DbConnection.ErrorCode.code_to_name/1)

    %Sqlite.DbConnection.Error{postgres: fields}
  end

  def exception([tag: :ssl, action: action, reason: :timeout]) do
    %Sqlite.DbConnection.Error{message: "ssl #{action}: timeout"}
  end

  def exception([tag: :ssl, action: action, reason: reason]) do
    formatted_reason = :ssl.format_error(reason)
    %Sqlite.DbConnection.Error{message: "ssl #{action}: #{formatted_reason} - #{inspect(reason)}"}
  end

  def exception([tag: :tcp, action: action, reason: reason]) when not reason in @nonposix_errors do
    formatted_reason = :inet.format_error(reason)
    %Sqlite.DbConnection.Error{message: "tcp #{action}: #{formatted_reason} - #{inspect(reason)}"}
  end

  def exception([tag: :tcp, action: action, reason: reason]) do
    %Sqlite.DbConnection.Error{message: "tcp #{action}: #{reason}"}
  end

  def exception(arg) do
    super(arg)
  end
end

defmodule Sqlite.DbConnection.Error do
  defexception [:message, :sqlite, :postgres]

  # TODO: Retrofit to actual SQLite error schema.

  def message(e) do
    if kw = e.postgres do
      "#{kw[:severity]} (#{kw[:code]}): #{kw[:message]}"
    else
      e.message
    end
  end

  def exception([postgres: fields]) do
    fields = Enum.into(fields, %{})
             |> Map.put(:pg_code, fields[:code])
             |> Map.update!(:code, &Sqlite.DbConnection.ErrorCode.code_to_name/1)

    %Sqlite.DbConnection.Error{postgres: fields}
  end

  def exception(arg) do
    super(arg)
  end
end

Remove support for tagged exceptions. Not likely to be relevant for SQLite.
Remove support for tagged exceptions. Not likely to be relevant for SQLite.

scouten/sqlite_ecto2