Improve error when http-api outdated

Signed-off-by: Sebastian Martinez <sebastinez@me.com>
This commit is contained in:
Sebastian Martinez
2022-04-04 18:48:57 +02:00
committed by Alexis Sellier
parent e2103483a9
commit 7a3958ab4a
3 changed files with 47 additions and 37 deletions
+5 -5
View File
@@ -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) {
+8 -3
View File
@@ -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}
+34 -29
View File
@@ -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 => {