<commit_before>defmodule Kairos.UserChannel do
  use Kairos.Web, :channel
  require Logger

  alias Kairos.{Repo, User}

  def join("users:" <> user_id, _params, socket) do
    current_user = socket.assigns.current_user

    if String.to_integer(user_id) == current_user.id do
      {:ok, socket}
    else
      {:error, %{reason: "Invalid user"}}
    end
  end

  def handle_in("user:update", params, socket) do
    current_user = socket.assigns.current_user

    current_user
    |> User.update_changeset(params)
    |> Repo.update
    |> case do
      {:ok, user} ->
        {:reply, {:ok, %{user: user}}, assign(socket, :current_user, user)}
      {:error, _changeset} ->
        {:reply, {:error,%{reason: "Invalid"}}, socket}
    end
  end

  def handle_in("user:projects", _params, socket) do
    Logger.info "Requesting projects from UserChannel"

    current_user = socket.assigns.current_user

    client = ExTracker.Client.new %{access_token: current_user.settings.pivotal_traker_api_token}
    projects = client |> ExTracker.Projects.list

    {:reply, {:ok, %{projects: projects}}, socket}
  end
end<commit_msg>Rescue any possible error while fetching projects<commit_after>defmodule Kairos.UserChannel do
  use Kairos.Web, :channel
  require Logger

  alias Kairos.{Repo, User}

  def join("users:" <> user_id, _params, socket) do
    current_user = socket.assigns.current_user

    if String.to_integer(user_id) == current_user.id do
      {:ok, socket}
    else
      {:error, %{reason: "Invalid user"}}
    end
  end

  def handle_in("user:update", params, socket) do
    current_user = socket.assigns.current_user

    current_user
    |> User.update_changeset(params)
    |> Repo.update
    |> case do
      {:ok, user} ->
        {:reply, {:ok, %{user: user}}, assign(socket, :current_user, user)}
      {:error, _changeset} ->
        {:reply, {:error,%{reason: "Invalid"}}, socket}
    end
  end

  def handle_in("user:projects", _params, socket) do
    Logger.info "Requesting projects from UserChannel"

    current_user = socket.assigns.current_user

    client = ExTracker.Client.new %{access_token: current_user.settings.pivotal_traker_api_token}
    projects = client |> ExTracker.Projects.list

    {:reply, {:ok, %{projects: projects}}, socket}
  rescue
    _ ->
      {:reply, {:error, %{reason: "Error in API call"}}, socket}
  end
end
{'fraction_non_alphanumeric': 0.13223489649345163, 'fraction_numerical': 0.0, 'mean_word_length': 3.493358633776091, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4095067', 'n_tokens_mistral': 814, 'n_tokens_neox': 765, 'n_words': 188}