Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Improve error when http-api outdated
Sebastian Martinez committed 4 years ago
commit 7a3958ab4a9159ccd1568e0bafbe32fdb31677af
parent e2103483a9a08338c803de06a85759ad2bcb7af7
3 files changed +47 -37
modified src/api.ts
@@ -38,19 +38,19 @@ export async function get(
      }
    });
  } catch (err) {
-
    throw new ApiError(urlString, "API request failed");
+
    throw new ApiError("API request failed", urlString);
  }

  if (! response.ok) {
-
    throw new ApiError(urlString, "Not found");
+
    throw new ApiError("Not found", urlString);
  }
  return response.json();
}

-
class ApiError extends Error {
-
  url: string;
+
export class ApiError extends Error {
+
  url?: string;

-
  constructor(url: string, message: string) {
+
  constructor(message: string, url?: string) {
    super(message);

    if (Error.captureStackTrace) {
modified src/base/projects/History.svelte
@@ -3,6 +3,7 @@
  import type { Project } from "@app/project";
  import Loading from "@app/Loading.svelte";
  import { groupCommitHistory, GroupedCommitsHistory } from "@app/commit";
+
  import Message from "@app/Message.svelte";

  export let project: Project;
  export let commit: string;
@@ -66,8 +67,12 @@
  </div>
{:catch err}
  <div class="history">
-
    <div class="error error-message text-xsmall">
-
      <div>API request to <code class="text-xsmall">{err.url}</code> failed.</div>
-
    </div>
+
    <Message error>
+
      {#if err.url}
+
        API request to <code class="text-xsmall">{err.url}</code> failed.
+
      {:else}
+
        {err.message}
+
      {/if}
+
    </Message>
  </div>
{/await}
modified src/commit.ts
@@ -1,5 +1,6 @@
import type { Stats } from "@app/project";
import type { Diff } from "@app/diff";
+
import { ApiError } from "@app/api";

export interface CommitsHistory {
  headers: CommitMetadata[];
@@ -37,7 +38,7 @@ export interface Person {
}

export interface CommitContext {
-
  committer: {
+
  committer?: {
    peer: {
      id: string;
      person: Person;
@@ -92,36 +93,40 @@ export function groupCommits(commits: { header: CommitHeader; context: CommitCon
  const groupedCommits: CommitGroup[] = [];
  let groupDate: Date | undefined = undefined;

-
  commits = commits.sort((a, b) => {
-
    if (a.header.committerTime > b.header.committerTime) {
-
      return -1;
-
    } else if (a.header.committerTime < b.header.committerTime) {
-
      return 1;
+
  try {
+
    commits = commits.sort((a, b) => {
+
      if (a.header.committerTime > b.header.committerTime) {
+
        return -1;
+
      } else if (a.header.committerTime < b.header.committerTime) {
+
        return 1;
+
      }
+

+
      return 0;
+
    });
+

+
    for (const commit of commits) {
+
      const time = commit.header.committerTime * 1000;
+
      const date = new Date(time);
+
      const isNewDay =
+
        !groupedCommits.length ||
+
        !groupDate ||
+
        date.getDate() < groupDate.getDate() ||
+
        date.getMonth() < groupDate.getMonth() ||
+
        date.getFullYear() < groupDate.getFullYear();
+

+
      if (isNewDay) {
+
        groupedCommits.push({
+
          time: formatGroupTime(time),
+
          commits: [],
+
        });
+
        groupDate = date;
+
      }
+
      groupedCommits[groupedCommits.length - 1].commits.push(commit);
    }
-

-
    return 0;
-
  });
-

-
  for (const commit of commits) {
-
    const time = commit.header.committerTime * 1000;
-
    const date = new Date(time);
-
    const isNewDay =
-
      !groupedCommits.length ||
-
      !groupDate ||
-
      date.getDate() < groupDate.getDate() ||
-
      date.getMonth() < groupDate.getMonth() ||
-
      date.getFullYear() < groupDate.getFullYear();
-

-
    if (isNewDay) {
-
      groupedCommits.push({
-
        time: formatGroupTime(time),
-
        commits: [],
-
      });
-
      groupDate = date;
-
    }
-
    groupedCommits[groupedCommits.length - 1].commits.push(commit);
+
    return groupedCommits;
+
  } catch (err) {
+
    throw new ApiError("Not able to create commit history, please consider updating seed HTTP API.");
  }
-
  return groupedCommits;
}

export const formatCommitTime = (t: number): string => {