Skip to content

Commit

Permalink
Reinstante Toasts (#153)
Browse files Browse the repository at this point in the history
When doing things like creating objects, we probably want to see errors
e.g. from quotas being exceeded.

Fixes #149.
  • Loading branch information
spjmurray authored Feb 3, 2025
1 parent d7a7a42 commit 187e21d
Show file tree
Hide file tree
Showing 25 changed files with 121 additions and 43 deletions.
28 changes: 22 additions & 6 deletions src/lib/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as Application from '$lib/openapi/application';
import * as Identity from '$lib/openapi/identity';
import * as Region from '$lib/openapi/region';
import { token, removeCredentials } from '$lib/credentials';
import type { ToastSettings } from '@skeletonlabs/skeleton';
import type { ToastSettings, ToastStore } from '@skeletonlabs/skeleton';

import { ROOT_CONTEXT, defaultTextMapSetter, trace } from '@opentelemetry/api';
import type { Span } from '@opentelemetry/api';
Expand Down Expand Up @@ -51,10 +51,16 @@ function traceContextMiddleware(): Identity.Middleware {
resolve();
});
},
post: (ctx: Identity.ResponseContext): Promise<Response | void> => {
return new Promise((resolve) => {
post: (ctx: Identity.ResponseContext) => {
return new Promise(async (resolve) => {
span.end();
resolve();

const headers = new Headers({ traceparent: span.spanContext().traceId });
ctx.response.headers.forEach((value, key) => headers.append(key, value));

const body = await ctx.response.blob();

resolve(new Response(body, { status: ctx.response.status, headers: headers }));
});
}
};
Expand Down Expand Up @@ -179,6 +185,16 @@ export function region(tokens: InternalToken, fetchImpl?: typeof fetch): Region.

// error is a generic fallback when an exception occurs, everything else should
// be handled in a middleware, and not on a per API basis.
export function error(error: Error): void {
console.log(error);
export async function error(toaster: ToastStore, error: Error) {
const responseError = error as Identity.ResponseError;

const errorJSON = Identity.ModelErrorFromJSON(await responseError.response.json());

const toast = {
autohide: false,
message: `An error has occurred - trace ID: ${responseError.response.headers.get('traceparent')}, error: ${errorJSON.error}, description: ${errorJSON.errorDescription}`,
background: 'variant-filled-error'
};

toaster.trigger(toast);
}
3 changes: 0 additions & 3 deletions src/lib/shell/SideBar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import * as Stores from '$lib/stores';
import * as RBAC from '$lib/rbac';
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
interface Props {
token: InternalToken;
organizations: Array<Identity.OrganizationRead>;
Expand Down
5 changes: 3 additions & 2 deletions src/routes/(shell)/compute/clusters/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
let { data }: { data: PageData } = $props();
import { getModalStore } from '@skeletonlabs/skeleton';
import { getModalStore, getToastStore } from '@skeletonlabs/skeleton';
import type { ModalSettings } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
const modalStore = getModalStore();
import * as Clients from '$lib/clients';
Expand Down Expand Up @@ -75,7 +76,7 @@
Clients.compute(data.token)
.apiV1OrganizationsOrganizationIDProjectsProjectIDClustersClusterIDDelete(parameters)
.then(() => invalidate('layout:clusters'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
};
Expand Down
6 changes: 5 additions & 1 deletion src/routes/(shell)/compute/clusters/create/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator';
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import * as Compute from '$lib/openapi/compute';
Expand Down Expand Up @@ -108,7 +112,7 @@
Clients.compute(data.token)
.apiV1OrganizationsOrganizationIDProjectsProjectIDClustersPost(parameters)
.then(() => window.location.assign('/compute/clusters'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
let firewallRuleValid: boolean = $state(false);
Expand Down
5 changes: 3 additions & 2 deletions src/routes/(shell)/identity/groups/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
let { data }: { data: PageData } = $props();
import type { ModalSettings } from '@skeletonlabs/skeleton';
import { getModalStore } from '@skeletonlabs/skeleton';
import { getModalStore, getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
const modalStore = getModalStore();
import * as Clients from '$lib/clients';
Expand Down Expand Up @@ -53,7 +54,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDGroupsGroupidDelete(parameters)
.then(() => invalidate('page:groups'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
};
Expand Down
6 changes: 5 additions & 1 deletion src/routes/(shell)/identity/groups/create/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
let { data }: { data: PageData } = $props();
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import * as Identity from '$lib/openapi/identity';
Expand Down Expand Up @@ -46,7 +50,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDGroupsPost(parameters)
.then(() => window.location.assign('/identity/groups'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
</script>

Expand Down
6 changes: 5 additions & 1 deletion src/routes/(shell)/identity/groups/view/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
let { data }: { data: PageData } = $props();
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import type { ShellPageSettings } from '$lib/layouts/types.ts';
Expand Down Expand Up @@ -49,7 +53,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDGroupsGroupidPut(parameters)
.then(() => window.location.assign('/identity/groups'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
</script>

Expand Down
5 changes: 3 additions & 2 deletions src/routes/(shell)/identity/oauth2providers/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
let { data }: { data: PageData } = $props();
import type { ModalSettings } from '@skeletonlabs/skeleton';
import { getModalStore } from '@skeletonlabs/skeleton';
import { getModalStore, getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
const modalStore = getModalStore();
import * as Clients from '$lib/clients';
Expand Down Expand Up @@ -52,7 +53,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDOauth2providersProviderIDDelete(parameters)
.then(() => invalidate('layout:oauth2providers'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
let { data }: { data: PageData } = $props();
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import * as Identity from '$lib/openapi/identity';
Expand Down Expand Up @@ -50,7 +54,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDOauth2providersPost(parameters)
.then(() => window.location.assign('/identity/oauth2providers'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
var callback: string = browser
Expand Down
6 changes: 5 additions & 1 deletion src/routes/(shell)/identity/organizations/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
let { data }: { data: PageData } = $props();
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import * as Identity from '$lib/openapi/identity';
Expand Down Expand Up @@ -37,7 +41,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDPut(parameters)
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
let providerType = $derived.by(() => {
Expand Down
5 changes: 3 additions & 2 deletions src/routes/(shell)/identity/projects/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
let { data }: { data: PageData } = $props();
import type { ModalSettings } from '@skeletonlabs/skeleton';
import { getModalStore } from '@skeletonlabs/skeleton';
import { getModalStore, getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
const modalStore = getModalStore();
import * as Clients from '$lib/clients';
Expand Down Expand Up @@ -53,7 +54,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDProjectsProjectIDDelete(parameters)
.then(() => invalidate('layout:projects'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
};
Expand Down
6 changes: 5 additions & 1 deletion src/routes/(shell)/identity/projects/create/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
let { data }: { data: PageData } = $props();
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import * as Identity from '$lib/openapi/identity';
Expand Down Expand Up @@ -46,7 +50,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDProjectsPost(parameters)
.then(() => window.location.assign('/identity/projects'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
</script>

Expand Down
6 changes: 5 additions & 1 deletion src/routes/(shell)/identity/projects/view/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
let { data }: { data: PageData } = $props();
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import type { ShellPageSettings } from '$lib/layouts/types.ts';
Expand Down Expand Up @@ -49,7 +53,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDProjectsProjectIDPut(parameters)
.then(() => window.location.assign('/identity/projects'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
</script>

Expand Down
6 changes: 5 additions & 1 deletion src/routes/(shell)/identity/quotas/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
let { data }: { data: PageData } = $props();
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import type { ShellPageSettings } from '$lib/layouts/types.ts';
Expand Down Expand Up @@ -30,7 +34,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDQuotasPut(parameters)
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
type QuotaMetadata = {
Expand Down
5 changes: 3 additions & 2 deletions src/routes/(shell)/identity/serviceaccounts/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
let { data }: { data: PageData } = $props();
import { getModalStore } from '@skeletonlabs/skeleton';
import { getModalStore, getToastStore } from '@skeletonlabs/skeleton';
import type { ModalSettings } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
const modalStore = getModalStore();
import * as Clients from '$lib/clients';
Expand Down Expand Up @@ -54,7 +55,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDServiceaccountsServiceAccountIDDelete(parameters)
.then(() => invalidate('layout:serviceaccounts'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
let { data }: { data: PageData } = $props();
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import * as Identity from '$lib/openapi/identity';
Expand Down Expand Up @@ -48,7 +52,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDServiceaccountsPost(parameters)
.then((v: Identity.ServiceAccountCreate) => (serviceAccount = v))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
let { data }: { data: PageData } = $props();
import { getToastStore } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
import * as Clients from '$lib/clients';
import * as Identity from '$lib/openapi/identity';
Expand Down Expand Up @@ -43,7 +47,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDServiceaccountsServiceAccountIDPut(parameters)
.then(() => window.location.assign('/identity/serviceaccounts'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
let newServiceAccount: Identity.ServiceAccountCreate | undefined = $state();
Expand All @@ -57,7 +61,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDServiceaccountsServiceAccountIDRotatePost(parameters)
.then((v: Identity.ServiceAccountCreate) => (newServiceAccount = v))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
</script>

Expand Down
5 changes: 3 additions & 2 deletions src/routes/(shell)/identity/users/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
let { data }: { data: PageData } = $props();
import { getModalStore } from '@skeletonlabs/skeleton';
import { getModalStore, getToastStore } from '@skeletonlabs/skeleton';
import type { ModalSettings } from '@skeletonlabs/skeleton';
const toastStore = getToastStore();
const modalStore = getModalStore();
import * as Clients from '$lib/clients';
Expand Down Expand Up @@ -56,7 +57,7 @@
Clients.identity(data.token)
.apiV1OrganizationsOrganizationIDUsersUserIDDelete(parameters)
.then(() => invalidate('layout:users'))
.catch((e: Error) => Clients.error(e));
.catch((e: Error) => Clients.error(toastStore, e));
}
};
Expand Down
Loading

0 comments on commit 187e21d

Please sign in to comment.