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 8c3c843
Show file tree
Hide file tree
Showing 5 changed files with 65 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.
`
};
24 changes: 24 additions & 0 deletions apps/desktop/src/lib/error/typeguards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { HttpError, BackendError } from './error';

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

export function isHttpError(err: unknown): err is HttpError {
return (
(typeof err === 'object' &&
err &&
'message' in err &&
typeof err.message === 'string' &&
'code' in err &&
typeof err.code === 'string') ||
false
);
}
28 changes: 12 additions & 16 deletions apps/desktop/src/lib/notifications/toasts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { KNOWN_ERRORS } from '$lib/error/knownErrors';
import { isBackendError, isHttpError } from '$lib/error/typeguards';
import { isStr } from '@gitbutler/ui/utils/string';
import posthog from 'posthog-js';
import { writable, type Writable } from 'svelte/store';
Expand Down Expand Up @@ -31,22 +33,16 @@ 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 (isStr(error)) {
showToast({ title, error: error.toString(), 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 8c3c843

Please sign in to comment.