Skip to content

Commit

Permalink
Improve front end error handling
Browse files Browse the repository at this point in the history
- handles signing error in one place
  • Loading branch information
mtsgrd committed Jan 13, 2025
1 parent e141d2d commit a9a8da0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 59 deletions.
17 changes: 17 additions & 0 deletions apps/desktop/src/lib/error/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Error type that has both a message and a status. These errors are primarily
* thrown by AI services and the Octokit GitHub client.
*/
export interface HttpError {
message: string;
status: number;
}

/**
* Error type that has both a message and a code. These errors can be thrown
* by the back end code.
*/
export interface BackendError {
message: string;
code: string;
}
7 changes: 7 additions & 0 deletions apps/desktop/src/lib/error/knownErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const KNOWN_ERRORS: Record<string, string> = {
'errors.commit.signing_failed': `
Commit signing failed and has now been disabled. You can configure commit signing in the project settings.
Please check our [documentation](https://docs.gitbutler.com/features/virtual-branches/verifying-commits) on setting up commit signing and verification.
`
};
23 changes: 23 additions & 0 deletions apps/desktop/src/lib/error/typeguards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { HttpError, BackendError } from './error';

export function isBackendError(err: unknown): err is BackendError {
return (
typeof err === 'object' &&
err !== null &&
'message' in err &&
typeof err.message === 'string' &&
'code' in err &&
typeof err.code === 'string'
);
}

export function isHttpError(err: unknown): err is HttpError {
return (
typeof err === 'object' &&
err !== null &&
'message' in err &&
typeof err.message === 'string' &&
'code' in err &&
typeof err.code === 'string'
);
}
31 changes: 15 additions & 16 deletions apps/desktop/src/lib/notifications/toasts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { KNOWN_ERRORS } from '$lib/error/knownErrors';
import { isBackendError, isHttpError } from '$lib/error/typeguards';
import { isStr } from '@gitbutler/ui/utils/string';

Check failure on line 3 in apps/desktop/src/lib/notifications/toasts.ts

View workflow job for this annotation

GitHub Actions / lint-node

'isStr' is defined but never used. Allowed unused vars must match /^_/u
import { isErrorlike } from '@gitbutler/ui/utils/typeguards';
import posthog from 'posthog-js';
import { writable, type Writable } from 'svelte/store';
import type { MessageStyle } from '$lib/shared/InfoMessage.svelte';
Expand Down Expand Up @@ -31,22 +34,18 @@ export function showToast(toast: Toast) {
}

export function showError(title: string, error: unknown) {
// Silence GitHub octokit.js when disconnected
// TODO: Fix this elsewhere.
if (error instanceof Object) {
if (
'status' in error &&
'message' in error &&
error.status === 500 &&
error.message === 'Load failed'
)
return;
const message = 'message' in error ? error.message : String(error);
showToast({ title, error: message, style: 'error' });
}

if (isStr(error)) {
showToast({ title, error, style: 'error' });
if (isBackendError(error) && error.code in KNOWN_ERRORS) {
showToast({ title, message: KNOWN_ERRORS[error.code], error });
} else if (isHttpError(error)) {
// Silence GitHub octokit.js when disconnected. This should ideally be
// prevented using `navigator.onLine` to avoid making requests when
// working offline.
if (error.status === 500 && error.message === 'Load failed') return;
showToast({ title, error: error.message, style: 'error' });
} else if (isErrorlike(error)) {
showToast({ title, error: error.message, style: 'error' });
} else {
showToast({ title, error: String(error), style: 'error' });
}
}

Expand Down
48 changes: 5 additions & 43 deletions apps/desktop/src/lib/vbranches/branchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ export class BranchController {
});
this.posthog.capture('Commit Successful');
} catch (err: any) {
if (err.code === 'errors.commit.signing_failed') {
showSignError(err);
} else {
showError('Failed to commit changes', err);
throw err;
}
showError('Failed to commit changes', err);
this.posthog.capture('Commit Failed', err);
}
}
Expand Down Expand Up @@ -503,12 +498,7 @@ export class BranchController {
targetCommitOid
});
} catch (err: any) {
// TODO: Probably we wanna have error code checking in a more generic way
if (err.code === 'errors.commit.signing_failed') {
showSignError(err);
} else {
showError('Failed to squash commit', err);
}
showError('Failed to squash commit', err);
}
}

Expand Down Expand Up @@ -565,12 +555,7 @@ export class BranchController {
message
});
} catch (err: any) {
// TODO: Probably we wanna have error code checking in a more generic way
if (err.code === 'errors.commit.signing_failed') {
showSignError(err);
} else {
showError('Failed to change commit message', err);
}
showError('Failed to change commit message', err);
}
}

Expand All @@ -583,12 +568,7 @@ export class BranchController {
offset
});
} catch (err: any) {
// TODO: Probably we wanna have error code checking in a more generic way
if (err.code === 'errors.commit.signing_failed') {
showSignError(err);
} else {
showError('Failed to insert blank commit', err);
}
showError('Failed to insert blank commit', err);
}
}

Expand All @@ -601,25 +581,7 @@ export class BranchController {
sourceBranchId
});
} catch (err: any) {
// TODO: Probably we wanna have error code checking in a more generic way
if (err.code === 'errors.commit.signing_failed') {
showSignError(err);
} else {
showError('Failed to move commit', err);
}
showError('Failed to move commit', err);
}
}
}

function showSignError(err: any) {
showToast({
title: 'Failed to commit due to signing error',
message: `
Signing is now disabled, so subsequent commits will not fail. You can configure commit signing in the project settings.
Please check our [documentation](https://docs.gitbutler.com/features/virtual-branches/verifying-commits) on setting up commit signing and verification.
`,
error: err.message,
style: 'error'
});
}

0 comments on commit a9a8da0

Please sign in to comment.