Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Improve mutation error handling

Hakan Shehu071d15ae7240
diff --git a/apps/desktop/src/main/mutations/accounts/account-logout.ts b/apps/desktop/src/main/mutations/accounts/account-logout.ts
index 3f3ad278..8ce444ca 100644
--- a/apps/desktop/src/main/mutations/accounts/account-logout.ts
+++ b/apps/desktop/src/main/mutations/accounts/account-logout.ts
@@ -1,6 +1,7 @@
import { databaseService } from '@/main/data/database-service';
import { accountService } from '@/main/services/account-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
AccountLogoutMutationInput,
AccountLogoutMutationOutput,
@@ -19,9 +20,10 @@ export class AccountLogoutMutationHandler
.executeTakeFirst();
if (!account) {
- return {
- success: false,
- };
+ throw new MutationError(
+ 'account_not_found',
+ 'Account was not found or has been logged out already. Try closing the app and opening it again.'
+ );
}
await accountService.logoutAccount(account);
diff --git a/apps/desktop/src/main/mutations/accounts/account-update.ts b/apps/desktop/src/main/mutations/accounts/account-update.ts
index af28290a..3b08f8d5 100644
--- a/apps/desktop/src/main/mutations/accounts/account-update.ts
+++ b/apps/desktop/src/main/mutations/accounts/account-update.ts
@@ -8,6 +8,7 @@ import {
AccountUpdateMutationInput,
AccountUpdateMutationOutput,
} from '@/shared/mutations/accounts/account-update';
+import { MutationError } from '@/shared/mutations';
export class AccountUpdateMutationHandler
implements MutationHandler<AccountUpdateMutationInput>
@@ -22,7 +23,10 @@ export class AccountUpdateMutationHandler
.executeTakeFirst();
if (!account) {
- throw new Error('Account not found!');
+ throw new MutationError(
+ 'account_not_found',
+ 'Account not found or has been logged out already. Try closing the app and opening it again.'
+ );
}
const server = await databaseService.appDatabase
@@ -32,7 +36,10 @@ export class AccountUpdateMutationHandler
.executeTakeFirst();
if (!server) {
- throw new Error('Account not found');
+ throw new MutationError(
+ 'server_not_found',
+ `The server ${account.server} associated with this account was not found. Try closing the app and opening it again.`
+ );
}
const { data } = await httpClient.put<AccountUpdateOutput>(
@@ -58,7 +65,10 @@ export class AccountUpdateMutationHandler
.executeTakeFirst();
if (!updatedAccount) {
- throw new Error('Account not found');
+ throw new MutationError(
+ 'account_not_found',
+ 'Account not found or has been logged out already. Try closing the app and opening it again.'
+ );
}
eventBus.publish({
diff --git a/apps/desktop/src/main/mutations/accounts/email-login.ts b/apps/desktop/src/main/mutations/accounts/email-login.ts
index d64399ce..9588b298 100644
--- a/apps/desktop/src/main/mutations/accounts/email-login.ts
+++ b/apps/desktop/src/main/mutations/accounts/email-login.ts
@@ -9,6 +9,7 @@ import {
EmailLoginMutationOutput,
} from '@/shared/mutations/accounts/email-login';
import { Account } from '@/shared/types/accounts';
+import { MutationError } from '@/shared/mutations';
export class EmailLoginMutationHandler
implements MutationHandler<EmailLoginMutationInput>
@@ -23,9 +24,10 @@ export class EmailLoginMutationHandler
.executeTakeFirst();
if (!server) {
- return {
- success: false,
- };
+ throw new MutationError(
+ 'server_not_found',
+ `Server ${input.server} was not found! Try using a different server.`
+ );
}
const { data } = await httpClient.post<LoginOutput>(
@@ -57,7 +59,10 @@ export class EmailLoginMutationHandler
.executeTakeFirst();
if (!createdAccount) {
- throw new Error('Failed to create account!');
+ throw new MutationError(
+ 'account_login_failed',
+ 'Failed to login with email and password! Please try again.'
+ );
}
account = {
@@ -93,7 +98,10 @@ export class EmailLoginMutationHandler
});
if (!account) {
- throw new Error('Failed to create account!');
+ throw new MutationError(
+ 'account_login_failed',
+ 'Failed to login with email and password! Please try again.'
+ );
}
eventBus.publish({
@@ -118,7 +126,6 @@ export class EmailLoginMutationHandler
}
return {
- success: true,
account,
workspaces: data.workspaces,
};
diff --git a/apps/desktop/src/main/mutations/accounts/email-register.ts b/apps/desktop/src/main/mutations/accounts/email-register.ts
index dc134296..bb4bf592 100644
--- a/apps/desktop/src/main/mutations/accounts/email-register.ts
+++ b/apps/desktop/src/main/mutations/accounts/email-register.ts
@@ -9,6 +9,7 @@ import {
EmailRegisterMutationOutput,
} from '@/shared/mutations/accounts/email-register';
import { Account } from '@/shared/types/accounts';
+import { MutationError } from '@/shared/mutations';
export class EmailRegisterMutationHandler
implements MutationHandler<EmailRegisterMutationInput>
@@ -23,9 +24,10 @@ export class EmailRegisterMutationHandler
.executeTakeFirst();
if (!server) {
- return {
- success: false,
- };
+ throw new MutationError(
+ 'server_not_found',
+ `Server ${input.server} was not found! Try using a different server.`
+ );
}
const { data } = await httpClient.post<LoginOutput>(
@@ -58,7 +60,9 @@ export class EmailRegisterMutationHandler
.executeTakeFirst();
if (!createdAccount) {
- throw new Error('Failed to create account!');
+ throw new Error(
+ 'Failed to create account! Please try again with a different email.'
+ );
}
account = {
@@ -94,7 +98,10 @@ export class EmailRegisterMutationHandler
});
if (!account) {
- throw new Error('Failed to create account!');
+ throw new MutationError(
+ 'account_register_failed',
+ 'Failed to create account! Please try again with a different email.'
+ );
}
eventBus.publish({
@@ -119,7 +126,6 @@ export class EmailRegisterMutationHandler
}
return {
- success: true,
account,
workspaces: data.workspaces,
};
diff --git a/apps/desktop/src/main/mutations/avatars/avatar-upload.ts b/apps/desktop/src/main/mutations/avatars/avatar-upload.ts
index 45bdd081..a55727ac 100644
--- a/apps/desktop/src/main/mutations/avatars/avatar-upload.ts
+++ b/apps/desktop/src/main/mutations/avatars/avatar-upload.ts
@@ -9,6 +9,7 @@ import {
AvatarUploadMutationInput,
AvatarUploadMutationOutput,
} from '@/shared/mutations/avatars/avatar-upload';
+import { MutationError } from '@/shared/mutations';
interface AvatarUploadResponse {
id: string;
@@ -28,10 +29,10 @@ export class AvatarUploadMutationHandler
.executeTakeFirst();
if (!credentials) {
- return {
- status: 'error',
- id: null,
- };
+ throw new MutationError(
+ 'account_not_found',
+ 'Account not found or has been logged out already. Try closing the app and opening it again.'
+ );
}
const filePath = input.filePath;
@@ -51,7 +52,6 @@ export class AvatarUploadMutationHandler
);
return {
- status: 'success',
id: data.id,
};
}
diff --git a/apps/desktop/src/main/mutations/channels/channel-create.ts b/apps/desktop/src/main/mutations/channels/channel-create.ts
index d01ddaf7..3bd88669 100644
--- a/apps/desktop/src/main/mutations/channels/channel-create.ts
+++ b/apps/desktop/src/main/mutations/channels/channel-create.ts
@@ -7,6 +7,7 @@ import {
ChannelCreateMutationInput,
ChannelCreateMutationOutput,
} from '@/shared/mutations/channels/channel-create';
+import { MutationError } from '@/shared/mutations';
export class ChannelCreateMutationHandler
implements MutationHandler<ChannelCreateMutationInput>
@@ -25,7 +26,10 @@ export class ChannelCreateMutationHandler
.executeTakeFirst();
if (!space) {
- throw new Error('Space not found');
+ throw new MutationError(
+ 'space_not_found',
+ 'Space not found or has been deleted.'
+ );
}
const id = generateId(IdType.Channel);
diff --git a/apps/desktop/src/main/mutations/channels/channel-update.ts b/apps/desktop/src/main/mutations/channels/channel-update.ts
index 09dfe463..8806aa7b 100644
--- a/apps/desktop/src/main/mutations/channels/channel-update.ts
+++ b/apps/desktop/src/main/mutations/channels/channel-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
ChannelUpdateMutationInput,
ChannelUpdateMutationOutput,
@@ -11,12 +12,15 @@ export class ChannelUpdateMutationHandler
async handleMutation(
input: ChannelUpdateMutationInput
): Promise<ChannelUpdateMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.channelId,
input.userId,
(attributes) => {
if (attributes.type !== 'channel') {
- throw new Error('Node is not a channel');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Something went wrong while updating the channel.'
+ );
}
attributes.name = input.name;
@@ -26,8 +30,36 @@ export class ChannelUpdateMutationHandler
}
);
- return {
- success: true,
- };
+ if (result === 'not_found') {
+ throw new MutationError(
+ 'channel_not_found',
+ 'Channel not found or has been deleted.'
+ );
+ }
+
+ if (result === 'invalid_attributes') {
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Something went wrong while updating the channel.'
+ );
+ }
+
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this channel."
+ );
+ }
+
+ if (result === 'success') {
+ return {
+ success: true,
+ };
+ }
+
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the channel.'
+ );
}
}
diff --git a/apps/desktop/src/main/mutations/collaborators/node-collaborator-create.ts b/apps/desktop/src/main/mutations/collaborators/node-collaborator-create.ts
index 76978370..ec2f40da 100644
--- a/apps/desktop/src/main/mutations/collaborators/node-collaborator-create.ts
+++ b/apps/desktop/src/main/mutations/collaborators/node-collaborator-create.ts
@@ -6,6 +6,7 @@ import {
NodeCollaboratorCreateMutationInput,
NodeCollaboratorCreateMutationOutput,
} from '@/shared/mutations/collaborators/node-collaborator-create';
+import { MutationError } from '@/shared/mutations';
export class NodeCollaboratorCreateMutationHandler
implements MutationHandler<NodeCollaboratorCreateMutationInput>
@@ -13,12 +14,30 @@ export class NodeCollaboratorCreateMutationHandler
async handleMutation(
input: NodeCollaboratorCreateMutationInput
): Promise<NodeCollaboratorCreateMutationOutput> {
- await nodeService.updateNode(input.nodeId, input.userId, (attributes) => {
- for (const collaboratorId of input.collaboratorIds) {
- set(attributes, `collaborators.${collaboratorId}`, input.role);
+ const result = await nodeService.updateNode(
+ input.nodeId,
+ input.userId,
+ (attributes) => {
+ for (const collaboratorId of input.collaboratorIds) {
+ set(attributes, `collaborators.${collaboratorId}`, input.role);
+ }
+ return attributes;
}
- return attributes;
- });
+ );
+
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to add collaborators to this node."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while adding collaborators to the node.'
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/collaborators/node-collaborator-delete.ts b/apps/desktop/src/main/mutations/collaborators/node-collaborator-delete.ts
index 1f510736..de0522e3 100644
--- a/apps/desktop/src/main/mutations/collaborators/node-collaborator-delete.ts
+++ b/apps/desktop/src/main/mutations/collaborators/node-collaborator-delete.ts
@@ -6,6 +6,7 @@ import {
NodeCollaboratorDeleteMutationInput,
NodeCollaboratorDeleteMutationOutput,
} from '@/shared/mutations/collaborators/node-collaborator-delete';
+import { MutationError } from '@/shared/mutations';
export class NodeCollaboratorDeleteMutationHandler
implements MutationHandler<NodeCollaboratorDeleteMutationInput>
@@ -13,10 +14,28 @@ export class NodeCollaboratorDeleteMutationHandler
async handleMutation(
input: NodeCollaboratorDeleteMutationInput
): Promise<NodeCollaboratorDeleteMutationOutput> {
- await nodeService.updateNode(input.nodeId, input.userId, (attributes) => {
- unset(attributes, `collaborators.${input.collaboratorId}`);
- return attributes;
- });
+ const result = await nodeService.updateNode(
+ input.nodeId,
+ input.userId,
+ (attributes) => {
+ unset(attributes, `collaborators.${input.collaboratorId}`);
+ return attributes;
+ }
+ );
+
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to remove collaborators from this node."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while removing collaborators from the node.'
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/collaborators/node-collaborator-update.ts b/apps/desktop/src/main/mutations/collaborators/node-collaborator-update.ts
index f0136b63..91addc89 100644
--- a/apps/desktop/src/main/mutations/collaborators/node-collaborator-update.ts
+++ b/apps/desktop/src/main/mutations/collaborators/node-collaborator-update.ts
@@ -6,6 +6,7 @@ import {
NodeCollaboratorUpdateMutationInput,
NodeCollaboratorUpdateMutationOutput,
} from '@/shared/mutations/collaborators/node-collaborator-update';
+import { MutationError } from '@/shared/mutations';
export class NodeCollaboratorUpdateMutationHandler
implements MutationHandler<NodeCollaboratorUpdateMutationInput>
@@ -13,10 +14,28 @@ export class NodeCollaboratorUpdateMutationHandler
async handleMutation(
input: NodeCollaboratorUpdateMutationInput
): Promise<NodeCollaboratorUpdateMutationOutput> {
- await nodeService.updateNode(input.nodeId, input.userId, (attributes) => {
- set(attributes, `collaborators.${input.collaboratorId}`, input.role);
- return attributes;
- });
+ const result = await nodeService.updateNode(
+ input.nodeId,
+ input.userId,
+ (attributes) => {
+ set(attributes, `collaborators.${input.collaboratorId}`, input.role);
+ return attributes;
+ }
+ );
+
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update collaborators for this node."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating collaborators for the node.'
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/databases/database-update.ts b/apps/desktop/src/main/mutations/databases/database-update.ts
index 0af934b2..65ddb46e 100644
--- a/apps/desktop/src/main/mutations/databases/database-update.ts
+++ b/apps/desktop/src/main/mutations/databases/database-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
DatabaseUpdateMutationInput,
DatabaseUpdateMutationOutput,
@@ -11,12 +12,15 @@ export class DatabaseUpdateMutationHandler
async handleMutation(
input: DatabaseUpdateMutationInput
): Promise<DatabaseUpdateMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Node is not a database');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a database'
+ );
}
attributes.name = input.name;
@@ -26,6 +30,20 @@ export class DatabaseUpdateMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this database."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the database.'
+ );
+ }
+
return {
success: true,
};
diff --git a/apps/desktop/src/main/mutations/databases/field-create.ts b/apps/desktop/src/main/mutations/databases/field-create.ts
index 16d35b3d..7e56fee7 100644
--- a/apps/desktop/src/main/mutations/databases/field-create.ts
+++ b/apps/desktop/src/main/mutations/databases/field-create.ts
@@ -11,6 +11,7 @@ import {
FieldCreateMutationInput,
FieldCreateMutationOutput,
} from '@/shared/mutations/databases/field-create';
+import { MutationError } from '@/shared/mutations';
export class FieldCreateMutationHandler
implements MutationHandler<FieldCreateMutationInput>
@@ -19,7 +20,7 @@ export class FieldCreateMutationHandler
input: FieldCreateMutationInput
): Promise<FieldCreateMutationOutput> {
const fieldId = generateId(IdType.Field);
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
@@ -44,6 +45,20 @@ export class FieldCreateMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to create a field in this database."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while creating the field.'
+ );
+ }
+
return {
id: fieldId,
};
diff --git a/apps/desktop/src/main/mutations/databases/field-delete.ts b/apps/desktop/src/main/mutations/databases/field-delete.ts
index 9cdaca21..df1ed2a0 100644
--- a/apps/desktop/src/main/mutations/databases/field-delete.ts
+++ b/apps/desktop/src/main/mutations/databases/field-delete.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
FieldDeleteMutationInput,
FieldDeleteMutationOutput,
@@ -11,12 +12,19 @@ export class FieldDeleteMutationHandler
async handleMutation(
input: FieldDeleteMutationInput
): Promise<FieldDeleteMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Invalid node type');
+ throw new MutationError('invalid_attributes', 'Invalid node type');
+ }
+
+ if (!attributes.fields[input.fieldId]) {
+ throw new MutationError(
+ 'field_not_found',
+ 'The field you are trying to delete does not exist.'
+ );
}
delete attributes.fields[input.fieldId];
@@ -25,6 +33,20 @@ export class FieldDeleteMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to delete this field."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while deleting the field.'
+ );
+ }
+
return {
id: input.fieldId,
};
diff --git a/apps/desktop/src/main/mutations/databases/field-name-update.ts b/apps/desktop/src/main/mutations/databases/field-name-update.ts
index 32abd34b..efc56108 100644
--- a/apps/desktop/src/main/mutations/databases/field-name-update.ts
+++ b/apps/desktop/src/main/mutations/databases/field-name-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
FieldNameUpdateMutationInput,
FieldNameUpdateMutationOutput,
@@ -11,17 +12,20 @@ export class FieldNameUpdateMutationHandler
async handleMutation(
input: FieldNameUpdateMutationInput
): Promise<FieldNameUpdateMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Invalid node type');
+ throw new MutationError('invalid_attributes', 'Invalid node type');
}
const field = attributes.fields[input.fieldId];
if (!field) {
- throw new Error('Field not found');
+ throw new MutationError(
+ 'field_not_found',
+ 'The field you are trying to update does not exist.'
+ );
}
field.name = input.name;
@@ -29,6 +33,20 @@ export class FieldNameUpdateMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this field."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the field.'
+ );
+ }
+
return {
id: input.fieldId,
};
diff --git a/apps/desktop/src/main/mutations/databases/select-option-create.ts b/apps/desktop/src/main/mutations/databases/select-option-create.ts
index 7d17bb0b..bade3395 100644
--- a/apps/desktop/src/main/mutations/databases/select-option-create.ts
+++ b/apps/desktop/src/main/mutations/databases/select-option-create.ts
@@ -11,6 +11,7 @@ import {
SelectOptionCreateMutationInput,
SelectOptionCreateMutationOutput,
} from '@/shared/mutations/databases/select-option-create';
+import { MutationError } from '@/shared/mutations';
export class SelectOptionCreateMutationHandler
implements MutationHandler<SelectOptionCreateMutationInput>
@@ -19,21 +20,30 @@ export class SelectOptionCreateMutationHandler
input: SelectOptionCreateMutationInput
): Promise<SelectOptionCreateMutationOutput> {
const id = generateId(IdType.SelectOption);
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Node is not a database');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a database'
+ );
}
const field = attributes.fields[input.fieldId];
if (!field) {
- throw new Error('Field not found');
+ throw new MutationError(
+ 'field_not_found',
+ 'The field you are trying to create a select option in does not exist.'
+ );
}
if (field.type !== 'select' && field.type !== 'multiSelect') {
- throw new Error('Field is not a select');
+ throw new MutationError(
+ 'invalid_field_type',
+ 'The field you are trying to create a select option in is not a "Select" or "Multi-Select" field.'
+ );
}
if (!field.options) {
@@ -57,6 +67,20 @@ export class SelectOptionCreateMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to create a select option in this field."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while creating the select option.'
+ );
+ }
+
return {
id: id,
};
diff --git a/apps/desktop/src/main/mutations/databases/select-option-delete.ts b/apps/desktop/src/main/mutations/databases/select-option-delete.ts
index 63f9bd24..6a063ff9 100644
--- a/apps/desktop/src/main/mutations/databases/select-option-delete.ts
+++ b/apps/desktop/src/main/mutations/databases/select-option-delete.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
SelectOptionDeleteMutationInput,
SelectOptionDeleteMutationOutput,
@@ -11,25 +12,44 @@ export class SelectOptionDeleteMutationHandler
async handleMutation(
input: SelectOptionDeleteMutationInput
): Promise<SelectOptionDeleteMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Node is not a database');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a database'
+ );
}
const field = attributes.fields[input.fieldId];
if (!field) {
- throw new Error('Field not found');
+ throw new MutationError(
+ 'field_not_found',
+ 'The field you are trying to delete a select option from does not exist.'
+ );
}
if (field.type !== 'select' && field.type !== 'multiSelect') {
- throw new Error('Field is not a select');
+ throw new MutationError(
+ 'invalid_field_type',
+ 'The field you are trying to delete a select option from is not a "Select" or "Multi-Select" field.'
+ );
}
if (!field.options) {
- field.options = {};
+ throw new MutationError(
+ 'select_option_not_found',
+ 'The field you are trying to delete a select option from does not have any select options.'
+ );
+ }
+
+ if (!field.options[input.optionId]) {
+ throw new MutationError(
+ 'select_option_not_found',
+ 'The select option you are trying to delete does not exist.'
+ );
}
delete field.options[input.optionId];
@@ -38,6 +58,20 @@ export class SelectOptionDeleteMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to delete this select option."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while deleting the select option.'
+ );
+ }
+
return {
id: input.optionId,
};
diff --git a/apps/desktop/src/main/mutations/databases/select-option-update.ts b/apps/desktop/src/main/mutations/databases/select-option-update.ts
index 5c35f60e..34fb11a1 100644
--- a/apps/desktop/src/main/mutations/databases/select-option-update.ts
+++ b/apps/desktop/src/main/mutations/databases/select-option-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
SelectOptionUpdateMutationInput,
SelectOptionUpdateMutationOutput,
@@ -11,21 +12,30 @@ export class SelectOptionUpdateMutationHandler
async handleMutation(
input: SelectOptionUpdateMutationInput
): Promise<SelectOptionUpdateMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Node is not a database');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a database'
+ );
}
const field = attributes.fields[input.fieldId];
if (!field) {
- throw new Error('Field not found');
+ throw new MutationError(
+ 'field_not_found',
+ 'The field you are trying to update a select option in does not exist.'
+ );
}
if (field.type !== 'select' && field.type !== 'multiSelect') {
- throw new Error('Field is not a select');
+ throw new MutationError(
+ 'invalid_field_type',
+ 'The field you are trying to update a select option in is not a "Select" or "Multi-Select" field.'
+ );
}
if (!field.options) {
@@ -34,7 +44,10 @@ export class SelectOptionUpdateMutationHandler
const option = field.options[input.optionId];
if (!option) {
- throw new Error('Option not found');
+ throw new MutationError(
+ 'select_option_not_found',
+ 'The select option you are trying to update does not exist.'
+ );
}
option.name = input.name;
@@ -43,6 +56,20 @@ export class SelectOptionUpdateMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this select option."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the select option.'
+ );
+ }
+
return {
id: input.optionId,
};
diff --git a/apps/desktop/src/main/mutations/databases/view-create.ts b/apps/desktop/src/main/mutations/databases/view-create.ts
index a3a12fc7..896ab255 100644
--- a/apps/desktop/src/main/mutations/databases/view-create.ts
+++ b/apps/desktop/src/main/mutations/databases/view-create.ts
@@ -11,6 +11,7 @@ import {
ViewCreateMutationInput,
ViewCreateMutationOutput,
} from '@/shared/mutations/databases/view-create';
+import { MutationError } from '@/shared/mutations';
export class ViewCreateMutationHandler
implements MutationHandler<ViewCreateMutationInput>
@@ -19,12 +20,15 @@ export class ViewCreateMutationHandler
input: ViewCreateMutationInput
): Promise<ViewCreateMutationOutput> {
const id = generateId(IdType.View);
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Node is not a database');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a database'
+ );
}
const maxIndex = Object.values(attributes.views)
@@ -47,6 +51,20 @@ export class ViewCreateMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to create a view in this database."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while creating the view.'
+ );
+ }
+
return {
id: id,
};
diff --git a/apps/desktop/src/main/mutations/databases/view-delete.ts b/apps/desktop/src/main/mutations/databases/view-delete.ts
index 8ade17bb..883dd8fa 100644
--- a/apps/desktop/src/main/mutations/databases/view-delete.ts
+++ b/apps/desktop/src/main/mutations/databases/view-delete.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
ViewDeleteMutationInput,
ViewDeleteMutationOutput,
@@ -11,12 +12,22 @@ export class ViewDeleteMutationHandler
async handleMutation(
input: ViewDeleteMutationInput
): Promise<ViewDeleteMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Node is not a database');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a database'
+ );
+ }
+
+ if (!attributes.views[input.viewId]) {
+ throw new MutationError(
+ 'view_not_found',
+ 'The view you are trying to delete does not exist.'
+ );
}
delete attributes.views[input.viewId];
@@ -24,6 +35,20 @@ export class ViewDeleteMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to delete this view."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while deleting the view.'
+ );
+ }
+
return {
id: input.viewId,
};
diff --git a/apps/desktop/src/main/mutations/databases/view-name-update.ts b/apps/desktop/src/main/mutations/databases/view-name-update.ts
index 53e4ee71..b87e3cc0 100644
--- a/apps/desktop/src/main/mutations/databases/view-name-update.ts
+++ b/apps/desktop/src/main/mutations/databases/view-name-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
ViewNameUpdateMutationInput,
ViewNameUpdateMutationOutput,
@@ -11,17 +12,23 @@ export class ViewNameUpdateMutationHandler
async handleMutation(
input: ViewNameUpdateMutationInput
): Promise<ViewNameUpdateMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Invalid node type');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a database'
+ );
}
const view = attributes.views[input.viewId];
if (!view) {
- throw new Error('View not found');
+ throw new MutationError(
+ 'view_not_found',
+ 'The view you are trying to update the name of does not exist.'
+ );
}
view.name = input.name;
@@ -29,6 +36,20 @@ export class ViewNameUpdateMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this view."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the view.'
+ );
+ }
+
return {
id: input.viewId,
};
diff --git a/apps/desktop/src/main/mutations/databases/view-update.ts b/apps/desktop/src/main/mutations/databases/view-update.ts
index 63bb875b..2f7ec4fc 100644
--- a/apps/desktop/src/main/mutations/databases/view-update.ts
+++ b/apps/desktop/src/main/mutations/databases/view-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
ViewUpdateMutationInput,
ViewUpdateMutationOutput,
@@ -11,12 +12,22 @@ export class ViewUpdateMutationHandler
async handleMutation(
input: ViewUpdateMutationInput
): Promise<ViewUpdateMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.databaseId,
input.userId,
(attributes) => {
if (attributes.type !== 'database') {
- throw new Error('Node is not a database');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a database'
+ );
+ }
+
+ if (!attributes.views[input.view.id]) {
+ throw new MutationError(
+ 'view_not_found',
+ 'The view you are trying to update does not exist.'
+ );
}
attributes.views[input.view.id] = input.view;
@@ -24,6 +35,20 @@ export class ViewUpdateMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this view."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the view.'
+ );
+ }
+
return {
id: input.view.id,
};
diff --git a/apps/desktop/src/main/mutations/files/file-create.ts b/apps/desktop/src/main/mutations/files/file-create.ts
index c81ddd5e..6d90c465 100644
--- a/apps/desktop/src/main/mutations/files/file-create.ts
+++ b/apps/desktop/src/main/mutations/files/file-create.ts
@@ -12,6 +12,7 @@ import {
FileCreateMutationInput,
FileCreateMutationOutput,
} from '@/shared/mutations/files/file-create';
+import { MutationError } from '@/shared/mutations';
export class FileCreateMutationHandler
implements MutationHandler<FileCreateMutationInput>
@@ -21,7 +22,10 @@ export class FileCreateMutationHandler
): Promise<FileCreateMutationOutput> {
const metadata = fileService.getFileMetadata(input.filePath);
if (!metadata) {
- throw new Error('Invalid file');
+ throw new MutationError(
+ 'invalid_file',
+ 'File is invalid or could not be read.'
+ );
}
const fileId = generateId(IdType.File);
diff --git a/apps/desktop/src/main/mutations/files/file-download.ts b/apps/desktop/src/main/mutations/files/file-download.ts
index 0ea1f684..fdbc1b1a 100644
--- a/apps/desktop/src/main/mutations/files/file-download.ts
+++ b/apps/desktop/src/main/mutations/files/file-download.ts
@@ -2,6 +2,7 @@ import { databaseService } from '@/main/data/database-service';
import { MutationHandler } from '@/main/types';
import { mapNode } from '@/main/utils';
import { eventBus } from '@/shared/lib/event-bus';
+import { MutationError } from '@/shared/mutations';
import {
FileDownloadMutationInput,
FileDownloadMutationOutput,
@@ -24,16 +25,18 @@ export class FileDownloadMutationHandler
.executeTakeFirst();
if (!node) {
- return {
- success: false,
- };
+ throw new MutationError(
+ 'node_not_found',
+ 'The file you are trying to download does not exist.'
+ );
}
const file = mapNode(node);
if (file.attributes.type !== 'file') {
- return {
- success: false,
- };
+ throw new MutationError(
+ 'invalid_attributes',
+ 'The node you are trying to download is not a file.'
+ );
}
const download = await workspaceDatabase
diff --git a/apps/desktop/src/main/mutations/folders/folder-update.ts b/apps/desktop/src/main/mutations/folders/folder-update.ts
index 0f2c5aac..dba63675 100644
--- a/apps/desktop/src/main/mutations/folders/folder-update.ts
+++ b/apps/desktop/src/main/mutations/folders/folder-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
FolderUpdateMutationInput,
FolderUpdateMutationOutput,
@@ -11,16 +12,34 @@ export class FolderUpdateMutationHandler
async handleMutation(
input: FolderUpdateMutationInput
): Promise<FolderUpdateMutationOutput> {
- await nodeService.updateNode(input.folderId, input.userId, (attributes) => {
- if (attributes.type !== 'folder') {
- throw new Error('Node is not a folder');
+ const result = await nodeService.updateNode(
+ input.folderId,
+ input.userId,
+ (attributes) => {
+ if (attributes.type !== 'folder') {
+ throw new MutationError('invalid_attributes', 'Node is not a folder');
+ }
+
+ attributes.name = input.name;
+ attributes.avatar = input.avatar;
+
+ return attributes;
}
+ );
- attributes.name = input.name;
- attributes.avatar = input.avatar;
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this folder."
+ );
+ }
- return attributes;
- });
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the folder.'
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/messages/message-create.ts b/apps/desktop/src/main/mutations/messages/message-create.ts
index 250eb4d1..ab49a5d5 100644
--- a/apps/desktop/src/main/mutations/messages/message-create.ts
+++ b/apps/desktop/src/main/mutations/messages/message-create.ts
@@ -17,6 +17,7 @@ import {
MessageCreateMutationInput,
MessageCreateMutationOutput,
} from '@/shared/mutations/messages/message-create';
+import { MutationError } from '@/shared/mutations';
export class MessageCreateMutationHandler
implements MutationHandler<MessageCreateMutationInput>
@@ -37,7 +38,10 @@ export class MessageCreateMutationHandler
const path = block.attrs?.path;
const metadata = fileService.getFileMetadata(path);
if (!metadata) {
- throw new Error('Invalid file');
+ throw new MutationError(
+ 'invalid_file',
+ 'File attachment is invalid or could not be read.'
+ );
}
const fileId = generateId(IdType.File);
@@ -105,7 +109,10 @@ export class MessageCreateMutationHandler
);
if (!reference || reference.type !== 'message') {
- throw new Error('Reference node not found');
+ throw new MutationError(
+ 'node_not_found',
+ 'Referenced message not found or has been deleted.'
+ );
}
const messageAttributes: MessageAttributes = {
diff --git a/apps/desktop/src/main/mutations/messages/message-reaction-create.ts b/apps/desktop/src/main/mutations/messages/message-reaction-create.ts
index d55c1d72..d73866d9 100644
--- a/apps/desktop/src/main/mutations/messages/message-reaction-create.ts
+++ b/apps/desktop/src/main/mutations/messages/message-reaction-create.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
MessageReactionCreateMutationInput,
MessageReactionCreateMutationOutput,
@@ -11,12 +12,15 @@ export class MessageReactionCreateMutationHandler
async handleMutation(
input: MessageReactionCreateMutationInput
): Promise<MessageReactionCreateMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.messageId,
input.userId,
(attributes) => {
if (attributes.type !== 'message') {
- throw new Error('Node is not a message');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a message'
+ );
}
const reactionsUsers = attributes.reactions[input.reaction] ?? [];
@@ -30,6 +34,20 @@ export class MessageReactionCreateMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to react to this message."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while reacting to the message.'
+ );
+ }
+
return {
success: true,
};
diff --git a/apps/desktop/src/main/mutations/messages/message-reaction-delete.ts b/apps/desktop/src/main/mutations/messages/message-reaction-delete.ts
index 38678f08..a8a36430 100644
--- a/apps/desktop/src/main/mutations/messages/message-reaction-delete.ts
+++ b/apps/desktop/src/main/mutations/messages/message-reaction-delete.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
MessageReactionDeleteMutationInput,
MessageReactionDeleteMutationOutput,
@@ -11,12 +12,15 @@ export class MessageReactionDeleteMutationHandler
async handleMutation(
input: MessageReactionDeleteMutationInput
): Promise<MessageReactionDeleteMutationOutput> {
- await nodeService.updateNode(
+ const result = await nodeService.updateNode(
input.messageId,
input.userId,
(attributes) => {
if (attributes.type !== 'message') {
- throw new Error('Node is not a message');
+ throw new MutationError(
+ 'invalid_attributes',
+ 'Node is not a message'
+ );
}
const reactionUsers = attributes.reactions[input.reaction] ?? [];
@@ -35,6 +39,20 @@ export class MessageReactionDeleteMutationHandler
}
);
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to react to this message."
+ );
+ }
+
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while deleting the reaction.'
+ );
+ }
+
return {
success: true,
};
diff --git a/apps/desktop/src/main/mutations/pages/page-content-update.ts b/apps/desktop/src/main/mutations/pages/page-content-update.ts
index 90021b39..858cdd03 100644
--- a/apps/desktop/src/main/mutations/pages/page-content-update.ts
+++ b/apps/desktop/src/main/mutations/pages/page-content-update.ts
@@ -7,6 +7,7 @@ import {
PageContentUpdateMutationInput,
PageContentUpdateMutationOutput,
} from '@/shared/mutations/pages/page-content-update';
+import { MutationError } from '@/shared/mutations';
export class PageContentUpdateMutationHandler
implements MutationHandler<PageContentUpdateMutationInput>
@@ -14,34 +15,52 @@ export class PageContentUpdateMutationHandler
async handleMutation(
input: PageContentUpdateMutationInput
): Promise<PageContentUpdateMutationOutput> {
- await nodeService.updateNode(input.pageId, input.userId, (attributes) => {
- if (attributes.type !== 'page') {
- throw new Error('Invalid node type');
- }
+ const result = await nodeService.updateNode(
+ input.pageId,
+ input.userId,
+ (attributes) => {
+ if (attributes.type !== 'page') {
+ throw new MutationError('invalid_attributes', 'Invalid node type');
+ }
- const blocksMap = new Map<string, Block>();
- if (attributes.content) {
- for (const [key, value] of Object.entries(attributes.content)) {
- blocksMap.set(key, value);
+ const blocksMap = new Map<string, Block>();
+ if (attributes.content) {
+ for (const [key, value] of Object.entries(attributes.content)) {
+ blocksMap.set(key, value);
+ }
}
+
+ const blocks = mapContentsToBlocks(
+ input.pageId,
+ input.content.content ?? [],
+ blocksMap
+ );
+
+ attributes.content = blocks.reduce(
+ (acc, block) => {
+ acc[block.id] = block;
+ return acc;
+ },
+ {} as Record<string, Block>
+ );
+
+ return attributes;
}
+ );
- const blocks = mapContentsToBlocks(
- input.pageId,
- input.content.content ?? [],
- blocksMap
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this page."
);
+ }
- attributes.content = blocks.reduce(
- (acc, block) => {
- acc[block.id] = block;
- return acc;
- },
- {} as Record<string, Block>
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the page content.'
);
-
- return attributes;
- });
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/pages/page-update.ts b/apps/desktop/src/main/mutations/pages/page-update.ts
index 8557057c..07c225cb 100644
--- a/apps/desktop/src/main/mutations/pages/page-update.ts
+++ b/apps/desktop/src/main/mutations/pages/page-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
PageUpdateMutationInput,
PageUpdateMutationOutput,
@@ -11,16 +12,34 @@ export class PageUpdateMutationHandler
async handleMutation(
input: PageUpdateMutationInput
): Promise<PageUpdateMutationOutput> {
- await nodeService.updateNode(input.pageId, input.userId, (attributes) => {
- if (attributes.type !== 'page') {
- throw new Error('Node is not a page');
+ const result = await nodeService.updateNode(
+ input.pageId,
+ input.userId,
+ (attributes) => {
+ if (attributes.type !== 'page') {
+ throw new MutationError('invalid_attributes', 'Node is not a page');
+ }
+
+ attributes.name = input.name;
+ attributes.avatar = input.avatar;
+
+ return attributes;
}
+ );
- attributes.name = input.name;
- attributes.avatar = input.avatar;
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this page."
+ );
+ }
- return attributes;
- });
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the page.'
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/records/record-avatar-update.ts b/apps/desktop/src/main/mutations/records/record-avatar-update.ts
index 28f5f7e8..35853df6 100644
--- a/apps/desktop/src/main/mutations/records/record-avatar-update.ts
+++ b/apps/desktop/src/main/mutations/records/record-avatar-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
RecordAvatarUpdateMutationInput,
RecordAvatarUpdateMutationOutput,
@@ -11,14 +12,25 @@ export class RecordAvatarUpdateMutationHandler
async handleMutation(
input: RecordAvatarUpdateMutationInput
): Promise<RecordAvatarUpdateMutationOutput> {
- await nodeService.updateNode(input.recordId, input.userId, (attributes) => {
- if (attributes.type !== 'record') {
- throw new Error('Invalid node type');
+ const result = await nodeService.updateNode(
+ input.recordId,
+ input.userId,
+ (attributes) => {
+ if (attributes.type !== 'record') {
+ throw new MutationError('invalid_attributes', 'Invalid node type');
+ }
+
+ attributes.avatar = input.avatar;
+ return attributes;
}
+ );
- attributes.avatar = input.avatar;
- return attributes;
- });
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this record."
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/records/record-content-update.ts b/apps/desktop/src/main/mutations/records/record-content-update.ts
index ad578fc9..cc1b098e 100644
--- a/apps/desktop/src/main/mutations/records/record-content-update.ts
+++ b/apps/desktop/src/main/mutations/records/record-content-update.ts
@@ -7,6 +7,7 @@ import {
RecordContentUpdateMutationInput,
RecordContentUpdateMutationOutput,
} from '@/shared/mutations/records/record-content-update';
+import { MutationError } from '@/shared/mutations';
export class RecordContentUpdateMutationHandler
implements MutationHandler<RecordContentUpdateMutationInput>
@@ -14,34 +15,52 @@ export class RecordContentUpdateMutationHandler
async handleMutation(
input: RecordContentUpdateMutationInput
): Promise<RecordContentUpdateMutationOutput> {
- await nodeService.updateNode(input.recordId, input.userId, (attributes) => {
- if (attributes.type !== 'record') {
- throw new Error('Invalid node type');
- }
+ const result = await nodeService.updateNode(
+ input.recordId,
+ input.userId,
+ (attributes) => {
+ if (attributes.type !== 'record') {
+ throw new MutationError('invalid_attributes', 'Invalid node type');
+ }
- const blocksMap = new Map<string, Block>();
- if (attributes.content) {
- for (const [key, value] of Object.entries(attributes.content)) {
- blocksMap.set(key, value);
+ const blocksMap = new Map<string, Block>();
+ if (attributes.content) {
+ for (const [key, value] of Object.entries(attributes.content)) {
+ blocksMap.set(key, value);
+ }
}
+
+ const blocks = mapContentsToBlocks(
+ input.recordId,
+ input.content.content ?? [],
+ blocksMap
+ );
+
+ attributes.content = blocks.reduce(
+ (acc, block) => {
+ acc[block.id] = block;
+ return acc;
+ },
+ {} as Record<string, Block>
+ );
+
+ return attributes;
}
+ );
- const blocks = mapContentsToBlocks(
- input.recordId,
- input.content.content ?? [],
- blocksMap
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this record."
);
+ }
- attributes.content = blocks.reduce(
- (acc, block) => {
- acc[block.id] = block;
- return acc;
- },
- {} as Record<string, Block>
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the record content.'
);
-
- return attributes;
- });
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/records/record-field-value-delete.ts b/apps/desktop/src/main/mutations/records/record-field-value-delete.ts
index 633589c0..50fd7826 100644
--- a/apps/desktop/src/main/mutations/records/record-field-value-delete.ts
+++ b/apps/desktop/src/main/mutations/records/record-field-value-delete.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
RecordFieldValueDeleteMutationInput,
RecordFieldValueDeleteMutationOutput,
@@ -11,14 +12,32 @@ export class RecordFieldValueDeleteMutationHandler
async handleMutation(
input: RecordFieldValueDeleteMutationInput
): Promise<RecordFieldValueDeleteMutationOutput> {
- await nodeService.updateNode(input.recordId, input.userId, (attributes) => {
- if (attributes.type !== 'record') {
- throw new Error('Invalid node type');
+ const result = await nodeService.updateNode(
+ input.recordId,
+ input.userId,
+ (attributes) => {
+ if (attributes.type !== 'record') {
+ throw new MutationError('invalid_attributes', 'Invalid node type');
+ }
+
+ delete attributes.fields[input.fieldId];
+ return attributes;
}
+ );
+
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to delete this field value."
+ );
+ }
- delete attributes.fields[input.fieldId];
- return attributes;
- });
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while deleting the field value.'
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/records/record-field-value-set.ts b/apps/desktop/src/main/mutations/records/record-field-value-set.ts
index 209bbffa..91be3f83 100644
--- a/apps/desktop/src/main/mutations/records/record-field-value-set.ts
+++ b/apps/desktop/src/main/mutations/records/record-field-value-set.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
RecordFieldValueSetMutationInput,
RecordFieldValueSetMutationOutput,
@@ -11,14 +12,32 @@ export class RecordFieldValueSetMutationHandler
async handleMutation(
input: RecordFieldValueSetMutationInput
): Promise<RecordFieldValueSetMutationOutput> {
- await nodeService.updateNode(input.recordId, input.userId, (attributes) => {
- if (attributes.type !== 'record') {
- throw new Error('Invalid node type');
+ const result = await nodeService.updateNode(
+ input.recordId,
+ input.userId,
+ (attributes) => {
+ if (attributes.type !== 'record') {
+ throw new MutationError('invalid_attributes', 'Invalid node type');
+ }
+
+ attributes.fields[input.fieldId] = input.value;
+ return attributes;
}
+ );
+
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to set this field value."
+ );
+ }
- attributes.fields[input.fieldId] = input.value;
- return attributes;
- });
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while setting the field value.'
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/records/record-name-update.ts b/apps/desktop/src/main/mutations/records/record-name-update.ts
index 8e441a3e..0e7b4c4a 100644
--- a/apps/desktop/src/main/mutations/records/record-name-update.ts
+++ b/apps/desktop/src/main/mutations/records/record-name-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
RecordNameUpdateMutationInput,
RecordNameUpdateMutationOutput,
@@ -11,14 +12,32 @@ export class RecordNameUpdateMutationHandler
async handleMutation(
input: RecordNameUpdateMutationInput
): Promise<RecordNameUpdateMutationOutput> {
- await nodeService.updateNode(input.recordId, input.userId, (attributes) => {
- if (attributes.type !== 'record') {
- throw new Error('Invalid node type');
+ const result = await nodeService.updateNode(
+ input.recordId,
+ input.userId,
+ (attributes) => {
+ if (attributes.type !== 'record') {
+ throw new MutationError('invalid_attributes', 'Invalid node type');
+ }
+
+ attributes.name = input.name;
+ return attributes;
}
+ );
+
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this record."
+ );
+ }
- attributes.name = input.name;
- return attributes;
- });
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the record name.'
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/servers/server-create.ts b/apps/desktop/src/main/mutations/servers/server-create.ts
index e8f1b863..f6d4a606 100644
--- a/apps/desktop/src/main/mutations/servers/server-create.ts
+++ b/apps/desktop/src/main/mutations/servers/server-create.ts
@@ -2,6 +2,7 @@ import { databaseService } from '@/main/data/database-service';
import { serverService } from '@/main/services/server-service';
import { MutationHandler } from '@/main/types';
import { eventBus } from '@/shared/lib/event-bus';
+import { MutationError } from '@/shared/mutations';
import {
ServerCreateMutationInput,
ServerCreateMutationOutput,
@@ -13,11 +14,25 @@ export class ServerCreateMutationHandler
async handleMutation(
input: ServerCreateMutationInput
): Promise<ServerCreateMutationOutput> {
+ const existingServer = await databaseService.appDatabase
+ .selectFrom('servers')
+ .selectAll()
+ .where('domain', '=', input.domain)
+ .executeTakeFirst();
+
+ if (existingServer) {
+ throw new MutationError(
+ 'server_already_exists',
+ 'A server with this domain already exists.'
+ );
+ }
+
const config = await serverService.fetchServerConfig(input.domain);
- if (!config) {
- return {
- success: false,
- };
+ if (config === null) {
+ throw new MutationError(
+ 'invalid_server_domain',
+ 'Could not fetch server configuration. Please make sure the domain is correct.'
+ );
}
const createdAt = new Date();
diff --git a/apps/desktop/src/main/mutations/spaces/space-create.ts b/apps/desktop/src/main/mutations/spaces/space-create.ts
index cd6134c3..c5b1e40e 100644
--- a/apps/desktop/src/main/mutations/spaces/space-create.ts
+++ b/apps/desktop/src/main/mutations/spaces/space-create.ts
@@ -13,6 +13,7 @@ import {
SpaceCreateMutationInput,
SpaceCreateMutationOutput,
} from '@/shared/mutations/spaces/space-create';
+import { MutationError } from '@/shared/mutations';
export class SpaceCreateMutationHandler
implements MutationHandler<SpaceCreateMutationInput>
@@ -27,11 +28,14 @@ export class SpaceCreateMutationHandler
.executeTakeFirst();
if (!workspace) {
- throw new Error('Workspace not found');
+ throw new MutationError('workspace_not_found', 'Workspace not found');
}
if (workspace.role === 'guest' || workspace.role === 'none') {
- throw new Error('You are not allowed to create spaces in this workspace');
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to create spaces in this workspace."
+ );
}
const spaceId = generateId(IdType.Space);
diff --git a/apps/desktop/src/main/mutations/spaces/space-update.ts b/apps/desktop/src/main/mutations/spaces/space-update.ts
index 01a71d08..226754e3 100644
--- a/apps/desktop/src/main/mutations/spaces/space-update.ts
+++ b/apps/desktop/src/main/mutations/spaces/space-update.ts
@@ -1,5 +1,6 @@
import { nodeService } from '@/main/services/node-service';
import { MutationHandler } from '@/main/types';
+import { MutationError } from '@/shared/mutations';
import {
SpaceUpdateMutationInput,
SpaceUpdateMutationOutput,
@@ -11,17 +12,35 @@ export class SpaceUpdateMutationHandler
async handleMutation(
input: SpaceUpdateMutationInput
): Promise<SpaceUpdateMutationOutput> {
- await nodeService.updateNode(input.id, input.userId, (attributes) => {
- if (attributes.type !== 'space') {
- throw new Error('Node is not a space');
+ const result = await nodeService.updateNode(
+ input.id,
+ input.userId,
+ (attributes) => {
+ if (attributes.type !== 'space') {
+ throw new MutationError('invalid_attributes', 'Node is not a space');
+ }
+
+ attributes.name = input.name;
+ attributes.description = input.description;
+ attributes.avatar = input.avatar;
+
+ return attributes;
}
+ );
- attributes.name = input.name;
- attributes.description = input.description;
- attributes.avatar = input.avatar;
+ if (result === 'unauthorized') {
+ throw new MutationError(
+ 'unauthorized',
+ "You don't have permission to update this space."
+ );
+ }
- return attributes;
- });
+ if (result !== 'success') {
+ throw new MutationError(
+ 'unknown',
+ 'Something went wrong while updating the space.'
+ );
+ }
return {
success: true,
diff --git a/apps/desktop/src/main/mutations/workspaces/workspace-create.ts b/apps/desktop/src/main/mutations/workspaces/workspace-create.ts
index b65f2cb9..dc130af6 100644
--- a/apps/desktop/src/main/mutations/workspaces/workspace-create.ts
+++ b/apps/desktop/src/main/mutations/workspaces/workspace-create.ts
@@ -8,6 +8,7 @@ import {
WorkspaceCreateMutationInput,
WorkspaceCreateMutationOutput,
} from '@/shared/mutations/workspaces/workspace-create';
+import { MutationError } from '@/shared/mutations';
export class WorkspaceCreateMutationHandler
implements MutationHandler<WorkspaceCreateMutationInput>
@@ -22,7 +23,7 @@ export class WorkspaceCreateMutationHandler
.executeTakeFirst();
if (!account) {
- throw new Error('Account not found!');
+ throw new MutationError('account_not_found', 'Account not found!');
}
const server = await databaseService.appDatabase
@@ -32,7 +33,10 @@ export class WorkspaceCreateMutationHandler
.executeTakeFirst();
if (!server) {
- throw new Error('Account not found');
+ throw new MutationError(
+ 'server_not_found',
+ 'The server associated with this account was not found.'
+ );
}
const { data } = await httpClient.post<WorkspaceOutput>(
@@ -65,7 +69,7 @@ export class WorkspaceCreateMutationHandler
.executeTakeFirst();
if (!createdWorkspace) {
- throw new Error('Failed to create workspace!');
+ throw new MutationError('unknown', 'Failed to create workspace!');
}
eventBus.publish({
diff --git a/apps/desktop/src/main/mutations/workspaces/workspace-update.ts b/apps/desktop/src/main/mutations/workspaces/workspace-update.ts
index 7c073cbb..c908af5b 100644
--- a/apps/desktop/src/main/mutations/workspaces/workspace-update.ts
+++ b/apps/desktop/src/main/mutations/workspaces/workspace-update.ts
@@ -2,6 +2,7 @@ import { databaseService } from '@/main/data/database-service';
import { MutationHandler } from '@/main/types';
import { eventBus } from '@/shared/lib/event-bus';
import { httpClient } from '@/shared/lib/http-client';
+import { MutationError } from '@/shared/mutations';
import {
WorkspaceUpdateMutationInput,
WorkspaceUpdateMutationOutput,
@@ -21,7 +22,7 @@ export class WorkspaceUpdateMutationHandler
.executeTakeFirst();
if (!account) {
- throw new Error('Account not found!');
+ throw new MutationError('account_not_found', 'Account not found!');
}
const server = await databaseService.appDatabase
@@ -31,7 +32,10 @@ export class WorkspaceUpdateMutationHandler
.executeTakeFirst();
if (!server) {
- throw new Error('Account not found');
+ throw new MutationError(
+ 'server_not_found',
+ 'The server associated with this account was not found.'
+ );
}
const { data } = await httpClient.put<Workspace>(
@@ -66,7 +70,7 @@ export class WorkspaceUpdateMutationHandler
.executeTakeFirst();
if (!updatedWorkspace) {
- throw new Error('Failed to update workspace!');
+ throw new MutationError('unknown', 'Failed to update workspace!');
}
eventBus.publish({
diff --git a/apps/desktop/src/main/mutations/workspaces/workspace-user-role-update.ts b/apps/desktop/src/main/mutations/workspaces/workspace-user-role-update.ts
index 0a6ed6d8..30bd7dbb 100644
--- a/apps/desktop/src/main/mutations/workspaces/workspace-user-role-update.ts
+++ b/apps/desktop/src/main/mutations/workspaces/workspace-user-role-update.ts
@@ -7,6 +7,7 @@ import {
WorkspaceUserRoleUpdateMutationInput,
WorkspaceUserRoleUpdateMutationOutput,
} from '@/shared/mutations/workspaces/workspace-user-role-update';
+import { MutationError } from '@/shared/mutations';
export class WorkspaceUserRoleUpdateMutationHandler
implements MutationHandler<WorkspaceUserRoleUpdateMutationInput>
@@ -21,9 +22,7 @@ export class WorkspaceUserRoleUpdateMutationHandler
.executeTakeFirst();
if (!workspace) {
- return {
- success: false,
- };
+ throw new MutationError('workspace_not_found', 'Workspace not found');
}
const account = await databaseService.appDatabase
@@ -33,9 +32,10 @@ export class WorkspaceUserRoleUpdateMutationHandler
.executeTakeFirst();
if (!account) {
- return {
- success: false,
- };
+ throw new MutationError(
+ 'account_not_found',
+ 'The account associated with this workspace was not found.'
+ );
}
const server = await databaseService.appDatabase
@@ -45,9 +45,10 @@ export class WorkspaceUserRoleUpdateMutationHandler
.executeTakeFirst();
if (!server) {
- return {
- success: false,
- };
+ throw new MutationError(
+ 'server_not_found',
+ 'The server associated with this account was not found.'
+ );
}
await httpClient.put<WorkspaceUserRoleUpdateOutput>(
diff --git a/apps/desktop/src/main/mutations/workspaces/workspace-users-invite.ts b/apps/desktop/src/main/mutations/workspaces/workspace-users-invite.ts
index f743eedb..b3f83c89 100644
--- a/apps/desktop/src/main/mutations/workspaces/workspace-users-invite.ts
+++ b/apps/desktop/src/main/mutations/workspaces/workspace-users-invite.ts
@@ -7,6 +7,7 @@ import {
WorkspaceUsersInviteMutationInput,
WorkspaceUsersInviteMutationOutput,
} from '@/shared/mutations/workspaces/workspace-users-invite';
+import { MutationError } from '@/shared/mutations';
export class WorkspaceUsersInviteMutationHandler
implements MutationHandler<WorkspaceUsersInviteMutationInput>
@@ -21,9 +22,7 @@ export class WorkspaceUsersInviteMutationHandler
.executeTakeFirst();
if (!workspace) {
- return {
- success: false,
- };
+ throw new MutationError('workspace_not_found', 'Workspace not found');
}
const account = await databaseService.appDatabase
@@ -33,9 +32,10 @@ export class WorkspaceUsersInviteMutationHandler
.executeTakeFirst();
if (!account) {
- return {
- success: false,
- };
+ throw new MutationError(
+ 'account_not_found',
+ 'The account associated with this workspace was not found.'
+ );
}
const server = await databaseService.appDatabase
@@ -45,9 +45,10 @@ export class WorkspaceUsersInviteMutationHandler
.executeTakeFirst();
if (!server) {
- return {
- success: false,
- };
+ throw new MutationError(
+ 'server_not_found',
+ 'The server associated with this account was not found.'
+ );
}
await httpClient.post<WorkspaceUsersInviteOutput>(
diff --git a/apps/desktop/src/main/services/mutation-service.ts b/apps/desktop/src/main/services/mutation-service.ts
index a7c86a5f..9895f3ee 100644
--- a/apps/desktop/src/main/services/mutation-service.ts
+++ b/apps/desktop/src/main/services/mutation-service.ts
@@ -1,25 +1,50 @@
import { createLogger } from '@/main/logger';
import { mutationHandlerMap } from '@/main/mutations';
import { MutationHandler } from '@/main/types';
-import { MutationInput, MutationMap } from '@/shared/mutations';
+import {
+ MutationError,
+ MutationInput,
+ MutationResult,
+} from '@/shared/mutations';
class MutationService {
private readonly logger = createLogger('mutation-service');
public async executeMutation<T extends MutationInput>(
input: T
- ): Promise<MutationMap[T['type']]['output']> {
+ ): Promise<MutationResult<T>> {
const handler = mutationHandlerMap[
input.type
] as unknown as MutationHandler<T>;
this.logger.trace(`Executing mutation: ${input.type}`);
- if (!handler) {
- throw new Error(`No handler found for mutation type: ${input.type}`);
- }
+ try {
+ if (!handler) {
+ throw new Error(`No handler found for mutation type: ${input.type}`);
+ }
+
+ const output = await handler.handleMutation(input);
+ return { success: true, output };
+ } catch (error) {
+ if (error instanceof MutationError) {
+ return {
+ success: false,
+ error: {
+ code: error.code,
+ message: error.message,
+ },
+ };
+ }
- return handler.handleMutation(input);
+ return {
+ success: false,
+ error: {
+ code: 'unknown',
+ message: 'Something went wrong trying to execute the mutation.',
+ },
+ };
+ }
}
}
diff --git a/apps/desktop/src/main/services/node-service.ts b/apps/desktop/src/main/services/node-service.ts
index d9dee999..fc56deb1 100644
--- a/apps/desktop/src/main/services/node-service.ts
+++ b/apps/desktop/src/main/services/node-service.ts
@@ -41,6 +41,13 @@ export type CreateNodeInput = {
download?: CreateDownload;
};
+export type UpdateNodeResult =
+ | 'success'
+ | 'not_found'
+ | 'unauthorized'
+ | 'failed'
+ | 'invalid_attributes';
+
class NodeService {
private readonly logger = createLogger('node-service');
@@ -245,23 +252,23 @@ class NodeService {
nodeId: string,
userId: string,
updater: (attributes: NodeAttributes) => NodeAttributes
- ) {
+ ): Promise<UpdateNodeResult> {
let count = 0;
while (count++ < 20) {
- const updated = await this.tryUpdateNode(nodeId, userId, updater);
- if (updated) {
- return;
+ const result = await this.tryUpdateNode(nodeId, userId, updater);
+ if (result) {
+ return result;
}
}
- throw new Error('Failed to update node');
+ return 'failed';
}
private async tryUpdateNode(
nodeId: string,
userId: string,
updater: (attributes: NodeAttributes) => NodeAttributes
- ): Promise<boolean> {
+ ): Promise<UpdateNodeResult | null> {
this.logger.trace(`Updating node ${nodeId}`);
const workspace = await this.fetchWorkspace(userId);
@@ -272,14 +279,14 @@ class NodeService {
const ancestorRows = await fetchNodeAncestors(workspaceDatabase, nodeId);
const nodeRow = ancestorRows.find((ancestor) => ancestor.id === nodeId);
if (!nodeRow) {
- throw new Error('Node not found');
+ return 'not_found';
}
const ancestors = ancestorRows.map(mapNode);
const node = mapNode(nodeRow);
if (!node) {
- throw new Error('Node not found');
+ return 'not_found';
}
const context = new NodeMutationContext(
@@ -296,11 +303,11 @@ class NodeService {
const model = registry.getNodeModel(node.type);
if (!model.schema.safeParse(updatedAttributes).success) {
- throw new Error('Invalid attributes');
+ return 'invalid_attributes';
}
if (!model.canUpdate(context, node, updatedAttributes)) {
- throw new Error('Insufficient permissions');
+ return 'unauthorized';
}
const ydoc = new YDoc();
@@ -312,7 +319,7 @@ class NodeService {
for (const previousTransaction of previousTransactions) {
if (previousTransaction.data === null) {
- throw new Error('Node has been deleted');
+ return 'not_found';
}
ydoc.applyUpdate(previousTransaction.data);
@@ -398,7 +405,11 @@ class NodeService {
this.logger.trace(`Failed to create transaction for node ${nodeId}`);
}
- return updatedNode !== undefined;
+ if (updatedNode) {
+ return 'success';
+ }
+
+ return null;
}
public async deleteNode(nodeId: string, userId: string) {
diff --git a/apps/desktop/src/renderer/components/accounts/account-logout.tsx b/apps/desktop/src/renderer/components/accounts/account-logout.tsx
index 1ebdfa5a..4f46a7dc 100644
--- a/apps/desktop/src/renderer/components/accounts/account-logout.tsx
+++ b/apps/desktop/src/renderer/components/accounts/account-logout.tsx
@@ -51,11 +51,10 @@ export const AccountLogout = ({ onCancel, onLogout }: AccountLogoutProps) => {
onSuccess() {
onLogout();
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to logout',
- description:
- 'Something went wrong trying to logout. Please try again.',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/accounts/account-update.tsx b/apps/desktop/src/renderer/components/accounts/account-update.tsx
index c8218321..09d3fd0c 100644
--- a/apps/desktop/src/renderer/components/accounts/account-update.tsx
+++ b/apps/desktop/src/renderer/components/accounts/account-update.tsx
@@ -66,11 +66,10 @@ export const AccountUpdate = ({ account }: { account: Account }) => {
variant: 'default',
});
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to update account',
- description:
- 'Something went wrong updating account. Please try again!',
+ description: error.message,
variant: 'destructive',
});
},
@@ -122,16 +121,15 @@ export const AccountUpdate = ({ account }: { account: Account }) => {
filePath: filePath,
},
onSuccess(output) {
- if (output.status === 'success' && output.id) {
+ if (output.id) {
form.setValue('avatar', output.id);
}
setIsFileDialogOpen(false);
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to upload avatar',
- description:
- 'Something went wrong trying to upload avatar. Please try again.',
+ description: error.message,
variant: 'destructive',
});
setIsFileDialogOpen(false);
diff --git a/apps/desktop/src/renderer/components/accounts/email-login.tsx b/apps/desktop/src/renderer/components/accounts/email-login.tsx
index 47a504ee..e2704892 100644
--- a/apps/desktop/src/renderer/components/accounts/email-login.tsx
+++ b/apps/desktop/src/renderer/components/accounts/email-login.tsx
@@ -47,22 +47,13 @@ export const EmailLogin = ({ server }: EmailLoginProps) => {
server: server.domain,
},
onSuccess(output) {
- if (output.success) {
- const userId = output.workspaces[0]?.id ?? '';
- navigate(`/${output.account.id}/${userId}`);
- } else {
- toast({
- title: 'Failed to login',
- description: 'Invalid email or password.',
- variant: 'destructive',
- });
- }
+ const userId = output.workspaces[0]?.id ?? '';
+ navigate(`/${output.account.id}/${userId}`);
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to login',
- description:
- 'Something went wrong trying to login. Please try again.',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/accounts/email-register.tsx b/apps/desktop/src/renderer/components/accounts/email-register.tsx
index 98d1005f..fe2a8ac7 100644
--- a/apps/desktop/src/renderer/components/accounts/email-register.tsx
+++ b/apps/desktop/src/renderer/components/accounts/email-register.tsx
@@ -50,22 +50,13 @@ export const EmailRegister = ({ server }: EmailRegisterProps) => {
server: server.domain,
},
onSuccess(output) {
- if (output.success) {
- const userId = output.workspaces[0]?.id ?? '';
- navigate(`/${output.account.id}/${userId}`);
- } else {
- toast({
- title: 'Failed to register',
- description: 'Email already in use.',
- variant: 'destructive',
- });
- }
+ const userId = output.workspaces[0]?.id ?? '';
+ navigate(`/${output.account.id}/${userId}`);
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to register',
- description:
- 'Something went wrong trying to register. Please try again.',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/avatars/avatar-upload.tsx b/apps/desktop/src/renderer/components/avatars/avatar-upload.tsx
index 14b78a32..f8721f5f 100644
--- a/apps/desktop/src/renderer/components/avatars/avatar-upload.tsx
+++ b/apps/desktop/src/renderer/components/avatars/avatar-upload.tsx
@@ -87,16 +87,13 @@ export const AvatarUpload = ({ onUpload }: AvatarUploadProps) => {
filePath: filePath,
},
onSuccess(output) {
- if (output.status === 'success' && output.id) {
- onUpload(output.id);
- }
+ onUpload(output.id);
setIsFileDialogOpen(false);
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to upload avatar',
- description:
- 'Something went wrong trying to upload avatar. Please try again.',
+ description: error.message,
variant: 'destructive',
});
setIsFileDialogOpen(false);
diff --git a/apps/desktop/src/renderer/components/channels/channel-create-dialog.tsx b/apps/desktop/src/renderer/components/channels/channel-create-dialog.tsx
index 0f08bbf7..c0dfc07b 100644
--- a/apps/desktop/src/renderer/components/channels/channel-create-dialog.tsx
+++ b/apps/desktop/src/renderer/components/channels/channel-create-dialog.tsx
@@ -1,4 +1,4 @@
-import { generateId,IdType } from '@colanode/core';
+import { generateId, IdType } from '@colanode/core';
import { ChannelForm } from '@/renderer/components/channels/channel-form';
import {
@@ -10,6 +10,7 @@ import {
} from '@/renderer/components/ui/dialog';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface ChannelCreateDialogProps {
spaceId: string;
@@ -63,6 +64,13 @@ export const ChannelCreateDialog = ({
onOpenChange(false);
workspace.openInMain(output.id);
},
+ onError(error) {
+ toast({
+ title: 'Failed to create channel',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/channels/channel-delete-dialog.tsx b/apps/desktop/src/renderer/components/channels/channel-delete-dialog.tsx
index 4acda462..43452ace 100644
--- a/apps/desktop/src/renderer/components/channels/channel-delete-dialog.tsx
+++ b/apps/desktop/src/renderer/components/channels/channel-delete-dialog.tsx
@@ -10,6 +10,7 @@ import {
import { Button } from '@/renderer/components/ui/button';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface ChannelDeleteDialogProps {
open: boolean;
@@ -53,6 +54,13 @@ export const ChannelDeleteDialog = ({
onOpenChange(false);
workspace.closeNode(nodeId);
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete channel',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/channels/channel-update-dialog.tsx b/apps/desktop/src/renderer/components/channels/channel-update-dialog.tsx
index 2c3c4e9a..ee509fde 100644
--- a/apps/desktop/src/renderer/components/channels/channel-update-dialog.tsx
+++ b/apps/desktop/src/renderer/components/channels/channel-update-dialog.tsx
@@ -71,6 +71,13 @@ export const ChannelUpdateDialog = ({
variant: 'default',
});
},
+ onError(error) {
+ toast({
+ title: 'Failed to update channel',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/chats/chat-create-popover.tsx b/apps/desktop/src/renderer/components/chats/chat-create-popover.tsx
index f155f2c7..5f1eab9f 100644
--- a/apps/desktop/src/renderer/components/chats/chat-create-popover.tsx
+++ b/apps/desktop/src/renderer/components/chats/chat-create-popover.tsx
@@ -9,6 +9,7 @@ import {
import { UserSearch } from '@/renderer/components/users/user-search';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
export const ChatCreatePopover = () => {
const workspace = useWorkspace();
@@ -37,6 +38,13 @@ export const ChatCreatePopover = () => {
workspace.openInMain(output.id);
setOpen(false);
},
+ onError(error) {
+ toast({
+ title: 'Failed to create chat',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/collaborators/node-collaborator-create.tsx b/apps/desktop/src/renderer/components/collaborators/node-collaborator-create.tsx
index 01090e7f..4fd4482b 100644
--- a/apps/desktop/src/renderer/components/collaborators/node-collaborator-create.tsx
+++ b/apps/desktop/src/renderer/components/collaborators/node-collaborator-create.tsx
@@ -58,11 +58,10 @@ export const NodeCollaboratorCreate = ({
onSuccess() {
setUsers([]);
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to add collaborators',
- description:
- 'Something went wrong trying to add collaborators. Please try again.',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/collaborators/node-collaborator.tsx b/apps/desktop/src/renderer/components/collaborators/node-collaborator.tsx
index fb9844ba..8288db5a 100644
--- a/apps/desktop/src/renderer/components/collaborators/node-collaborator.tsx
+++ b/apps/desktop/src/renderer/components/collaborators/node-collaborator.tsx
@@ -6,6 +6,7 @@ import { NodeCollaboratorRoleDropdown } from '@/renderer/components/collaborator
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
import { useQuery } from '@/renderer/hooks/use-query';
+import { toast } from '@/renderer/hooks/use-toast';
interface NodeCollaboratorProps {
nodeId: string;
@@ -65,6 +66,13 @@ export const NodeCollaborator = ({
role: newRole,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update collaborator',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/databases/database-create-dialog.tsx b/apps/desktop/src/renderer/components/databases/database-create-dialog.tsx
index 3e6bf6c2..8a487e78 100644
--- a/apps/desktop/src/renderer/components/databases/database-create-dialog.tsx
+++ b/apps/desktop/src/renderer/components/databases/database-create-dialog.tsx
@@ -1,4 +1,4 @@
-import { generateId,IdType } from '@colanode/core';
+import { generateId, IdType } from '@colanode/core';
import { DatabaseForm } from '@/renderer/components/databases/database-form';
import {
@@ -10,6 +10,7 @@ import {
} from '@/renderer/components/ui/dialog';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface DatabaseCreateDialogProps {
spaceId: string;
@@ -63,6 +64,13 @@ export const DatabaseCreateDialog = ({
onOpenChange(false);
workspace.openInMain(output.id);
},
+ onError(error) {
+ toast({
+ title: 'Failed to create database',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/databases/database-delete-dialog.tsx b/apps/desktop/src/renderer/components/databases/database-delete-dialog.tsx
index ec1bb0bd..21321ba4 100644
--- a/apps/desktop/src/renderer/components/databases/database-delete-dialog.tsx
+++ b/apps/desktop/src/renderer/components/databases/database-delete-dialog.tsx
@@ -10,6 +10,7 @@ import {
import { Button } from '@/renderer/components/ui/button';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface DatabaseDeleteDialogProps {
open: boolean;
@@ -53,6 +54,13 @@ export const DatabaseDeleteDialog = ({
onOpenChange(false);
workspace.closeNode(nodeId);
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete database',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/databases/database-update-dialog.tsx b/apps/desktop/src/renderer/components/databases/database-update-dialog.tsx
index 7c49901a..a39613d1 100644
--- a/apps/desktop/src/renderer/components/databases/database-update-dialog.tsx
+++ b/apps/desktop/src/renderer/components/databases/database-update-dialog.tsx
@@ -71,6 +71,13 @@ export const DatabaseUpdateDialog = ({
variant: 'default',
});
},
+ onError(error) {
+ toast({
+ title: 'Failed to update database',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/databases/database.tsx b/apps/desktop/src/renderer/components/databases/database.tsx
index 2ef9efc3..d4093352 100644
--- a/apps/desktop/src/renderer/components/databases/database.tsx
+++ b/apps/desktop/src/renderer/components/databases/database.tsx
@@ -9,6 +9,7 @@ import React from 'react';
import { DatabaseContext } from '@/renderer/contexts/database';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface DatabaseProps {
database: DatabaseNode;
@@ -46,6 +47,13 @@ export const Database = ({ database, role, children }: DatabaseProps) => {
fieldType: type,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to create field',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
renameField: (id, name) => {
@@ -61,6 +69,13 @@ export const Database = ({ database, role, children }: DatabaseProps) => {
name,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update field',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
deleteField: (id) => {
@@ -75,6 +90,13 @@ export const Database = ({ database, role, children }: DatabaseProps) => {
fieldId: id,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete field',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
createSelectOption: (fieldId, name, color) => {
@@ -91,6 +113,13 @@ export const Database = ({ database, role, children }: DatabaseProps) => {
color,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to create select option',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
updateSelectOption: (fieldId, attributes) => {
@@ -108,6 +137,13 @@ export const Database = ({ database, role, children }: DatabaseProps) => {
color: attributes.color,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update select option',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
deleteSelectOption: (fieldId, optionId) => {
@@ -123,6 +159,13 @@ export const Database = ({ database, role, children }: DatabaseProps) => {
optionId,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete select option',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
}}
diff --git a/apps/desktop/src/renderer/components/databases/fields/field-create-popover.tsx b/apps/desktop/src/renderer/components/databases/fields/field-create-popover.tsx
index e2e577a0..2d0e0d95 100644
--- a/apps/desktop/src/renderer/components/databases/fields/field-create-popover.tsx
+++ b/apps/desktop/src/renderer/components/databases/fields/field-create-popover.tsx
@@ -25,6 +25,7 @@ import { Spinner } from '@/renderer/components/ui/spinner';
import { useDatabase } from '@/renderer/contexts/database';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
const formSchema = z.object({
name: z.string(),
@@ -80,6 +81,13 @@ export const FieldCreatePopover = () => {
setOpen(false);
form.reset();
},
+ onError(error) {
+ toast({
+ title: 'Failed to create field',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
};
diff --git a/apps/desktop/src/renderer/components/databases/fields/select-field-options.tsx b/apps/desktop/src/renderer/components/databases/fields/select-field-options.tsx
index 3284b734..bbd0001c 100644
--- a/apps/desktop/src/renderer/components/databases/fields/select-field-options.tsx
+++ b/apps/desktop/src/renderer/components/databases/fields/select-field-options.tsx
@@ -19,6 +19,7 @@ import { useDatabase } from '@/renderer/contexts/database';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
import { getRandomSelectOptionColor } from '@/shared/lib/databases';
+import { toast } from '@/renderer/hooks/use-toast';
interface SelectFieldOptionsProps {
field: SelectFieldAttributes | MultiSelectFieldAttributes;
@@ -125,6 +126,13 @@ export const SelectFieldOptions = ({
setColor(getRandomSelectOptionColor());
onSelect(output.id);
},
+ onError(error) {
+ toast({
+ title: 'Failed to create select option',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
className="flex flex-row items-center gap-2"
diff --git a/apps/desktop/src/renderer/components/databases/tables/table-view-name-cell.tsx b/apps/desktop/src/renderer/components/databases/tables/table-view-name-cell.tsx
index bacc2533..77e52ef4 100644
--- a/apps/desktop/src/renderer/components/databases/tables/table-view-name-cell.tsx
+++ b/apps/desktop/src/renderer/components/databases/tables/table-view-name-cell.tsx
@@ -6,6 +6,7 @@ import React from 'react';
import { Spinner } from '@/renderer/components/ui/spinner';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface NameEditorProps {
initialValue: string;
@@ -71,6 +72,13 @@ export const TableViewNameCell = ({ record }: TableViewNameCellProps) => {
onSuccess() {
setIsEditing(false);
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
};
diff --git a/apps/desktop/src/renderer/components/databases/view-create-dialog.tsx b/apps/desktop/src/renderer/components/databases/view-create-dialog.tsx
index 9b04a53a..cf46d45b 100644
--- a/apps/desktop/src/renderer/components/databases/view-create-dialog.tsx
+++ b/apps/desktop/src/renderer/components/databases/view-create-dialog.tsx
@@ -142,6 +142,13 @@ export const ViewCreateDialog = ({
form.reset();
onOpenChange(false);
},
+ onError(error) {
+ toast({
+ title: 'Failed to create view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
};
diff --git a/apps/desktop/src/renderer/components/databases/view-delete-dialog.tsx b/apps/desktop/src/renderer/components/databases/view-delete-dialog.tsx
index 03066f55..994fc504 100644
--- a/apps/desktop/src/renderer/components/databases/view-delete-dialog.tsx
+++ b/apps/desktop/src/renderer/components/databases/view-delete-dialog.tsx
@@ -11,6 +11,7 @@ import { Button } from '@/renderer/components/ui/button';
import { useDatabase } from '@/renderer/contexts/database';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface ViewDeleteDialogProps {
id: string;
@@ -59,6 +60,13 @@ export const ViewDeleteDialog = ({
onSuccess() {
onOpenChange(false);
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/databases/view.tsx b/apps/desktop/src/renderer/components/databases/view.tsx
index 69436bd6..a44ce0b9 100644
--- a/apps/desktop/src/renderer/components/databases/view.tsx
+++ b/apps/desktop/src/renderer/components/databases/view.tsx
@@ -25,6 +25,7 @@ import {
getFieldFilterOperators,
} from '@/shared/lib/databases';
import { ViewField } from '@/shared/types/databases';
+import { toast } from '@/renderer/hooks/use-toast';
interface ViewProps {
view: ViewAttributes;
@@ -86,6 +87,13 @@ export const View = ({ view }: ViewProps) => {
databaseId: database.id,
view: viewCopy,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
updateAvatar: (avatar: string) => {
@@ -103,6 +111,13 @@ export const View = ({ view }: ViewProps) => {
databaseId: database.id,
view: viewCopy,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
setFieldDisplay: (id: string, display: boolean) => {
@@ -132,6 +147,13 @@ export const View = ({ view }: ViewProps) => {
databaseId: database.id,
view: viewCopy,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
resizeField: (id: string, width: number) => {
@@ -161,6 +183,13 @@ export const View = ({ view }: ViewProps) => {
databaseId: database.id,
view: viewCopy,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
resizeName: (width: number) => {
@@ -182,6 +211,13 @@ export const View = ({ view }: ViewProps) => {
databaseId: database.id,
view: viewCopy,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
moveField: (id: string, after: string) => {
@@ -216,6 +252,13 @@ export const View = ({ view }: ViewProps) => {
databaseId: database.id,
view: viewCopy,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
isFieldFilterOpened: (fieldId: string) =>
@@ -258,6 +301,13 @@ export const View = ({ view }: ViewProps) => {
setOpenedFieldFilters((prev) => [...prev, fieldId]);
setIsSearchBarOpened(true);
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
updateFilter: (id: string, filter: ViewFilterAttributes) => {
@@ -282,6 +332,13 @@ export const View = ({ view }: ViewProps) => {
onSuccess() {
setIsSearchBarOpened(true);
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
removeFilter: (id: string) => {
@@ -306,6 +363,13 @@ export const View = ({ view }: ViewProps) => {
onSuccess() {
setIsSearchBarOpened(true);
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
initFieldSort: (fieldId: string, direction: SortDirection) => {
@@ -343,6 +407,13 @@ export const View = ({ view }: ViewProps) => {
setIsSearchBarOpened(true);
setIsSortsOpened(true);
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
updateSort: (id: string, sort: ViewSortAttributes) => {
@@ -368,6 +439,13 @@ export const View = ({ view }: ViewProps) => {
setIsSearchBarOpened(true);
setIsSortsOpened(true);
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
removeSort: (id: string) => {
@@ -392,6 +470,13 @@ export const View = ({ view }: ViewProps) => {
onSuccess() {
setIsSearchBarOpened(true);
},
+ onError(error) {
+ toast({
+ title: 'Failed to update view',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
openSearchBar: () => {
@@ -433,6 +518,13 @@ export const View = ({ view }: ViewProps) => {
onSuccess: (output) => {
workspace.openInModal(output.id);
},
+ onError(error) {
+ toast({
+ title: 'Failed to create record',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
}}
diff --git a/apps/desktop/src/renderer/components/files/file-delete-dialog.tsx b/apps/desktop/src/renderer/components/files/file-delete-dialog.tsx
index 4f6c6744..3e9e9066 100644
--- a/apps/desktop/src/renderer/components/files/file-delete-dialog.tsx
+++ b/apps/desktop/src/renderer/components/files/file-delete-dialog.tsx
@@ -10,6 +10,7 @@ import {
import { Button } from '@/renderer/components/ui/button';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface FileDeleteDialogProps {
nodeId: string;
@@ -52,6 +53,13 @@ export const FileDeleteDialog = ({
onSuccess() {
onOpenChange(false);
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete file',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/files/file-download.tsx b/apps/desktop/src/renderer/components/files/file-download.tsx
index 0cdec37e..b947603b 100644
--- a/apps/desktop/src/renderer/components/files/file-download.tsx
+++ b/apps/desktop/src/renderer/components/files/file-download.tsx
@@ -3,6 +3,7 @@ import { Download } from 'lucide-react';
import { Spinner } from '@/renderer/components/ui/spinner';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface FileDownloadProps {
id: string;
@@ -34,6 +35,13 @@ export const FileDownload = ({ id, downloadProgress }: FileDownloadProps) => {
userId: workspace.userId,
fileId: id,
},
+ onError(error) {
+ toast({
+ title: 'Failed to download file',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/folders/folder-body.tsx b/apps/desktop/src/renderer/components/folders/folder-body.tsx
index ac271d60..1399b7ab 100644
--- a/apps/desktop/src/renderer/components/folders/folder-body.tsx
+++ b/apps/desktop/src/renderer/components/folders/folder-body.tsx
@@ -24,6 +24,7 @@ import { ScrollArea } from '@/renderer/components/ui/scroll-area';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
import { FolderLayoutType } from '@/shared/types/folders';
+import { toast } from '@/renderer/hooks/use-toast';
export type FolderLayout = {
value: FolderLayoutType;
@@ -103,6 +104,13 @@ export const FolderBody = ({ folder }: FolderBodyProps) => {
filePath,
parentId: folder.id,
},
+ onError(error) {
+ toast({
+ title: 'Failed to upload file',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
isDialogOpenedRef.current = false;
diff --git a/apps/desktop/src/renderer/components/folders/folder-create-dialog.tsx b/apps/desktop/src/renderer/components/folders/folder-create-dialog.tsx
index 81007335..91258d20 100644
--- a/apps/desktop/src/renderer/components/folders/folder-create-dialog.tsx
+++ b/apps/desktop/src/renderer/components/folders/folder-create-dialog.tsx
@@ -1,4 +1,4 @@
-import { generateId,IdType } from '@colanode/core';
+import { generateId, IdType } from '@colanode/core';
import { FolderForm } from '@/renderer/components/folders/folder-form';
import {
@@ -10,6 +10,7 @@ import {
} from '@/renderer/components/ui/dialog';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface FolderCreateDialogProps {
spaceId: string;
@@ -64,6 +65,13 @@ export const FolderCreateDialog = ({
onOpenChange(false);
workspace.openInMain(output.id);
},
+ onError(error) {
+ toast({
+ title: 'Failed to create folder',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/folders/folder-delete-dialog.tsx b/apps/desktop/src/renderer/components/folders/folder-delete-dialog.tsx
index 6500cbc1..17fb7657 100644
--- a/apps/desktop/src/renderer/components/folders/folder-delete-dialog.tsx
+++ b/apps/desktop/src/renderer/components/folders/folder-delete-dialog.tsx
@@ -10,6 +10,7 @@ import {
import { Button } from '@/renderer/components/ui/button';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface FolderDeleteDialogProps {
open: boolean;
@@ -53,6 +54,13 @@ export const FolderDeleteDialog = ({
onOpenChange(false);
workspace.closeNode(nodeId);
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete folder',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/folders/folder-update-dialog.tsx b/apps/desktop/src/renderer/components/folders/folder-update-dialog.tsx
index 48d2d54a..73ab24c7 100644
--- a/apps/desktop/src/renderer/components/folders/folder-update-dialog.tsx
+++ b/apps/desktop/src/renderer/components/folders/folder-update-dialog.tsx
@@ -69,6 +69,13 @@ export const FolderUpdateDialog = ({
variant: 'default',
});
},
+ onError(error) {
+ toast({
+ title: 'Failed to update folder',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/messages/message-actions.tsx b/apps/desktop/src/renderer/components/messages/message-actions.tsx
index b6e35c75..ec6591e4 100644
--- a/apps/desktop/src/renderer/components/messages/message-actions.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-actions.tsx
@@ -9,6 +9,7 @@ import { Separator } from '@/renderer/components/ui/separator';
import { useConversation } from '@/renderer/contexts/conversation';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
const MessageAction = ({ children }: { children: React.ReactNode }) => {
return (
@@ -49,6 +50,13 @@ export const MessageActions = ({ message }: MessageActionsProps) => {
userId: workspace.userId,
reaction,
},
+ onError(error) {
+ toast({
+ title: 'Failed to add reaction',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
[isPending, mutate, workspace.userId, message.id]
@@ -85,6 +93,13 @@ export const MessageActions = ({ message }: MessageActionsProps) => {
userId: workspace.userId,
reaction,
},
+ onError(error) {
+ toast({
+ title: 'Failed to add reaction',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/messages/message-create.tsx b/apps/desktop/src/renderer/components/messages/message-create.tsx
index 50693095..72c9eaa3 100644
--- a/apps/desktop/src/renderer/components/messages/message-create.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-create.tsx
@@ -79,6 +79,13 @@ export const MessageCreate = React.forwardRef<MessageCreateRefProps>(
messageEditorRef.current.focus();
}
},
+ onError(error) {
+ toast({
+ title: 'Failed to create message',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}, [
conversation.id,
diff --git a/apps/desktop/src/renderer/components/messages/message-delete-button.tsx b/apps/desktop/src/renderer/components/messages/message-delete-button.tsx
index fbae52c2..3e1e1c00 100644
--- a/apps/desktop/src/renderer/components/messages/message-delete-button.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-delete-button.tsx
@@ -13,6 +13,7 @@ import {
import { Button } from '@/renderer/components/ui/button';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface MessageDeleteButtonProps {
id: string;
@@ -55,6 +56,13 @@ export const MessageDeleteButton = ({ id }: MessageDeleteButtonProps) => {
onSuccess() {
setShowDeleteModal(false);
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete message',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/messages/message-reactions.tsx b/apps/desktop/src/renderer/components/messages/message-reactions.tsx
index 832598f8..8e333739 100644
--- a/apps/desktop/src/renderer/components/messages/message-reactions.tsx
+++ b/apps/desktop/src/renderer/components/messages/message-reactions.tsx
@@ -4,6 +4,7 @@ import { EmojiElement } from '@/renderer/components/emojis/emoji-element';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
import { cn } from '@/shared/lib/utils';
+import { toast } from '@/renderer/hooks/use-toast';
interface MessageReactionsProps {
message: MessageNode;
@@ -60,6 +61,13 @@ export const MessageReactions = ({ message }: MessageReactionsProps) => {
userId: workspace.userId,
reaction: reaction.reaction,
},
+ onError(error) {
+ toast({
+ title: 'Failed to remove reaction',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
} else {
mutate({
@@ -69,6 +77,13 @@ export const MessageReactions = ({ message }: MessageReactionsProps) => {
userId: workspace.userId,
reaction: reaction.reaction,
},
+ onError(error) {
+ toast({
+ title: 'Failed to add reaction',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}
}}
diff --git a/apps/desktop/src/renderer/components/pages/page-body.tsx b/apps/desktop/src/renderer/components/pages/page-body.tsx
index ecdf5111..609ac97d 100644
--- a/apps/desktop/src/renderer/components/pages/page-body.tsx
+++ b/apps/desktop/src/renderer/components/pages/page-body.tsx
@@ -6,6 +6,7 @@ import { Document } from '@/renderer/components/documents/document';
import { ScrollArea } from '@/renderer/components/ui/scroll-area';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface PageBodyProps {
page: PageNode;
@@ -26,6 +27,13 @@ export const PageBody = ({ page, role }: PageBodyProps) => {
pageId: page.id,
content,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update page',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
[mutate, workspace.userId, page.id]
diff --git a/apps/desktop/src/renderer/components/pages/page-create-dialog.tsx b/apps/desktop/src/renderer/components/pages/page-create-dialog.tsx
index 3ab901bc..3bd5f2b3 100644
--- a/apps/desktop/src/renderer/components/pages/page-create-dialog.tsx
+++ b/apps/desktop/src/renderer/components/pages/page-create-dialog.tsx
@@ -1,4 +1,4 @@
-import { generateId,IdType } from '@colanode/core';
+import { generateId, IdType } from '@colanode/core';
import { PageForm } from '@/renderer/components/pages/page-form';
import {
@@ -10,6 +10,7 @@ import {
} from '@/renderer/components/ui/dialog';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface PageCreateDialogProps {
spaceId: string;
@@ -64,6 +65,13 @@ export const PageCreateDialog = ({
onOpenChange(false);
workspace.openInMain(output.id);
},
+ onError(error) {
+ toast({
+ title: 'Failed to create page',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/pages/page-delete-dialog.tsx b/apps/desktop/src/renderer/components/pages/page-delete-dialog.tsx
index cc3c9a37..89918b77 100644
--- a/apps/desktop/src/renderer/components/pages/page-delete-dialog.tsx
+++ b/apps/desktop/src/renderer/components/pages/page-delete-dialog.tsx
@@ -10,6 +10,7 @@ import {
import { Button } from '@/renderer/components/ui/button';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface PageDeleteDialogProps {
open: boolean;
@@ -53,6 +54,13 @@ export const PageDeleteDialog = ({
onOpenChange(false);
workspace.closeNode(nodeId);
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete page',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/pages/page-update-dialog.tsx b/apps/desktop/src/renderer/components/pages/page-update-dialog.tsx
index 48ed5153..5aca6f8b 100644
--- a/apps/desktop/src/renderer/components/pages/page-update-dialog.tsx
+++ b/apps/desktop/src/renderer/components/pages/page-update-dialog.tsx
@@ -69,6 +69,13 @@ export const PageUpdateDialog = ({
variant: 'default',
});
},
+ onError(error) {
+ toast({
+ title: 'Failed to update page',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
/>
diff --git a/apps/desktop/src/renderer/components/records/record-avatar.tsx b/apps/desktop/src/renderer/components/records/record-avatar.tsx
index 12f77d49..840cdf4c 100644
--- a/apps/desktop/src/renderer/components/records/record-avatar.tsx
+++ b/apps/desktop/src/renderer/components/records/record-avatar.tsx
@@ -4,6 +4,7 @@ import { Button } from '@/renderer/components/ui/button';
import { useRecord } from '@/renderer/contexts/record';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
export const RecordAvatar = () => {
const workspace = useWorkspace();
@@ -37,6 +38,13 @@ export const RecordAvatar = () => {
avatar,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update record avatar',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/records/record-body.tsx b/apps/desktop/src/renderer/components/records/record-body.tsx
index eece384a..1cf5aaa4 100644
--- a/apps/desktop/src/renderer/components/records/record-body.tsx
+++ b/apps/desktop/src/renderer/components/records/record-body.tsx
@@ -15,6 +15,7 @@ import { ScrollArea } from '@/renderer/components/ui/scroll-area';
import { Separator } from '@/renderer/components/ui/separator';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface RecordBodyProps {
record: RecordNode;
@@ -44,6 +45,13 @@ export const RecordBody = ({
recordId: record.id,
content,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update record',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
[mutate]
diff --git a/apps/desktop/src/renderer/components/records/record-delete-dialog.tsx b/apps/desktop/src/renderer/components/records/record-delete-dialog.tsx
index d7b038d0..65410691 100644
--- a/apps/desktop/src/renderer/components/records/record-delete-dialog.tsx
+++ b/apps/desktop/src/renderer/components/records/record-delete-dialog.tsx
@@ -10,6 +10,7 @@ import {
import { Button } from '@/renderer/components/ui/button';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
interface RecordDeleteDialogProps {
open: boolean;
@@ -53,6 +54,13 @@ export const RecordDeleteDialog = ({
onOpenChange(false);
workspace.closeNode(nodeId);
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete record',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
>
diff --git a/apps/desktop/src/renderer/components/records/record-name.tsx b/apps/desktop/src/renderer/components/records/record-name.tsx
index 9337d1b3..dadbd465 100644
--- a/apps/desktop/src/renderer/components/records/record-name.tsx
+++ b/apps/desktop/src/renderer/components/records/record-name.tsx
@@ -2,6 +2,7 @@ import { SmartTextInput } from '@/renderer/components/ui/smart-text-input';
import { useRecord } from '@/renderer/contexts/record';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
export const RecordName = () => {
const workspace = useWorkspace();
@@ -28,6 +29,13 @@ export const RecordName = () => {
name: value,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update record name',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
}}
className="font-heading border-b border-none pl-1 text-4xl font-bold shadow-none focus-visible:ring-0"
diff --git a/apps/desktop/src/renderer/components/records/record-provider.tsx b/apps/desktop/src/renderer/components/records/record-provider.tsx
index 50ebe471..d352f310 100644
--- a/apps/desktop/src/renderer/components/records/record-provider.tsx
+++ b/apps/desktop/src/renderer/components/records/record-provider.tsx
@@ -4,6 +4,7 @@ import React from 'react';
import { RecordContext } from '@/renderer/contexts/record';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
export const RecordProvider = ({
record,
@@ -41,6 +42,13 @@ export const RecordProvider = ({
value,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to update record field value',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
removeFieldValue: (field) => {
@@ -51,6 +59,13 @@ export const RecordProvider = ({
fieldId: field.id,
userId: workspace.userId,
},
+ onError(error) {
+ toast({
+ title: 'Failed to delete record field value',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
},
getBooleanValue: (field) => {
diff --git a/apps/desktop/src/renderer/components/servers/server-create-dialog.tsx b/apps/desktop/src/renderer/components/servers/server-create-dialog.tsx
index 5d1561c5..22e996b5 100644
--- a/apps/desktop/src/renderer/components/servers/server-create-dialog.tsx
+++ b/apps/desktop/src/renderer/components/servers/server-create-dialog.tsx
@@ -62,11 +62,10 @@ export const ServerCreateDialog = ({
onSuccess() {
onOpenChange(false);
},
- onError() {
+ onError(error) {
toast({
- title: 'Failed to login',
- description:
- 'Something went wrong adding the server. Please make sure the domain is correct.',
+ title: 'Failed to add server',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/spaces/space-create-dialog.tsx b/apps/desktop/src/renderer/components/spaces/space-create-dialog.tsx
index f56393f4..0ab42cc5 100644
--- a/apps/desktop/src/renderer/components/spaces/space-create-dialog.tsx
+++ b/apps/desktop/src/renderer/components/spaces/space-create-dialog.tsx
@@ -24,6 +24,7 @@ import { Spinner } from '@/renderer/components/ui/spinner';
import { Textarea } from '@/renderer/components/ui/textarea';
import { useWorkspace } from '@/renderer/contexts/workspace';
import { useMutation } from '@/renderer/hooks/use-mutation';
+import { toast } from '@/renderer/hooks/use-toast';
const formSchema = z.object({
name: z.string().min(3, 'Name must be at least 3 characters long.'),
@@ -76,6 +77,13 @@ export const SpaceCreateDialog = ({
onOpenChange(false);
form.reset();
},
+ onError(error) {
+ toast({
+ title: 'Failed to create space',
+ description: error.message,
+ variant: 'destructive',
+ });
+ },
});
};
diff --git a/apps/desktop/src/renderer/components/spaces/space-delete-form.tsx b/apps/desktop/src/renderer/components/spaces/space-delete-form.tsx
index a1457b73..868493b0 100644
--- a/apps/desktop/src/renderer/components/spaces/space-delete-form.tsx
+++ b/apps/desktop/src/renderer/components/spaces/space-delete-form.tsx
@@ -40,11 +40,10 @@ export const SpaceDeleteForm = ({ id }: SpaceDeleteFormProps) => {
variant: 'default',
});
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to delete space',
- description:
- 'Something went wrong deleting space. Please try again!',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/spaces/space-update-form.tsx b/apps/desktop/src/renderer/components/spaces/space-update-form.tsx
index 027c7002..6e8dbf4b 100644
--- a/apps/desktop/src/renderer/components/spaces/space-update-form.tsx
+++ b/apps/desktop/src/renderer/components/spaces/space-update-form.tsx
@@ -63,10 +63,10 @@ export const SpaceUpdateForm = ({ space }: SpaceUpdateFormProps) => {
variant: 'default',
});
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to update space',
- description: 'Something went wrong updating space. Please try again!',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/workspaces/workspace-create.tsx b/apps/desktop/src/renderer/components/workspaces/workspace-create.tsx
index 1126dea8..f02f3aa9 100644
--- a/apps/desktop/src/renderer/components/workspaces/workspace-create.tsx
+++ b/apps/desktop/src/renderer/components/workspaces/workspace-create.tsx
@@ -41,11 +41,10 @@ export const WorkspaceCreate = () => {
onSuccess(output) {
navigate(`/${account.id}/${output.id}`);
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to create workspace',
- description:
- 'Somewthing went wrong creating the workspace. Please try again!',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/workspaces/workspace-form.tsx b/apps/desktop/src/renderer/components/workspaces/workspace-form.tsx
index 5b18fa1c..ee43bc2a 100644
--- a/apps/desktop/src/renderer/components/workspaces/workspace-form.tsx
+++ b/apps/desktop/src/renderer/components/workspaces/workspace-form.tsx
@@ -107,16 +107,13 @@ export const WorkspaceForm = ({
filePath: filePath,
},
onSuccess(output) {
- if (output.status === 'success' && output.id) {
- form.setValue('avatar', output.id);
- }
+ form.setValue('avatar', output.id);
setIsFileDialogOpen(false);
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to upload avatar',
- description:
- 'Something went wrong trying to upload avatar. Please try again.',
+ description: error.message,
variant: 'destructive',
});
setIsFileDialogOpen(false);
diff --git a/apps/desktop/src/renderer/components/workspaces/workspace-update.tsx b/apps/desktop/src/renderer/components/workspaces/workspace-update.tsx
index 6b47b348..a4864dbf 100644
--- a/apps/desktop/src/renderer/components/workspaces/workspace-update.tsx
+++ b/apps/desktop/src/renderer/components/workspaces/workspace-update.tsx
@@ -33,11 +33,10 @@ export const WorkspaceUpdate = () => {
variant: 'default',
});
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to update workspace',
- description:
- 'Something went wrong updating workspace. Please try again!',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/workspaces/workspace-user-invite.tsx b/apps/desktop/src/renderer/components/workspaces/workspace-user-invite.tsx
index 66795e31..fc522067 100644
--- a/apps/desktop/src/renderer/components/workspaces/workspace-user-invite.tsx
+++ b/apps/desktop/src/renderer/components/workspaces/workspace-user-invite.tsx
@@ -91,11 +91,10 @@ export const WorkspaceUserInvite = ({
setEmails([]);
setInput('');
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to invite users',
- description:
- 'Something went wrong inviting userts to workspace. Please try again!',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/components/workspaces/workspace-user-role-dropdown.tsx b/apps/desktop/src/renderer/components/workspaces/workspace-user-role-dropdown.tsx
index 2a8821ed..1ac5a1b2 100644
--- a/apps/desktop/src/renderer/components/workspaces/workspace-user-role-dropdown.tsx
+++ b/apps/desktop/src/renderer/components/workspaces/workspace-user-role-dropdown.tsx
@@ -107,11 +107,10 @@ export const WorkspaceUserRoleDropdown = ({
role: role.value,
userId: workspace.userId,
},
- onError() {
+ onError(error) {
toast({
title: 'Failed to update role',
- description:
- 'Something went wrong updating user role. Please try again!',
+ description: error.message,
variant: 'destructive',
});
},
diff --git a/apps/desktop/src/renderer/editor/commands/file.tsx b/apps/desktop/src/renderer/editor/commands/file.tsx
index b408eba2..96ffa8e4 100644
--- a/apps/desktop/src/renderer/editor/commands/file.tsx
+++ b/apps/desktop/src/renderer/editor/commands/file.tsx
@@ -42,7 +42,7 @@ export const FileCommand: EditorCommand = {
generateIndex: false,
});
- if (!output.id) {
+ if (!output.success) {
return;
}
@@ -53,7 +53,7 @@ export const FileCommand: EditorCommand = {
.insertContent({
type: NodeTypes.File,
attrs: {
- id: output.id,
+ id: output.output.id,
},
})
.run();
diff --git a/apps/desktop/src/renderer/editor/commands/folder.tsx b/apps/desktop/src/renderer/editor/commands/folder.tsx
index 4df6363a..0081989a 100644
--- a/apps/desktop/src/renderer/editor/commands/folder.tsx
+++ b/apps/desktop/src/renderer/editor/commands/folder.tsx
@@ -25,7 +25,7 @@ export const FolderCommand: EditorCommand = {
generateIndex: false,
});
- if (!output.id) {
+ if (!output.success) {
return;
}
@@ -36,7 +36,7 @@ export const FolderCommand: EditorCommand = {
.insertContent({
type: NodeTypes.Folder,
attrs: {
- id: output.id,
+ id: output.output.id,
},
})
.run();
diff --git a/apps/desktop/src/renderer/editor/commands/page.tsx b/apps/desktop/src/renderer/editor/commands/page.tsx
index 3160420c..d4bd4641 100644
--- a/apps/desktop/src/renderer/editor/commands/page.tsx
+++ b/apps/desktop/src/renderer/editor/commands/page.tsx
@@ -24,7 +24,7 @@ export const PageCommand: EditorCommand = {
generateIndex: false,
});
- if (!output.id) {
+ if (!output.success) {
return;
}
@@ -35,7 +35,7 @@ export const PageCommand: EditorCommand = {
.insertContent({
type: NodeTypes.Page,
attrs: {
- id: output.id,
+ id: output.output.id,
},
})
.run();
diff --git a/apps/desktop/src/renderer/hooks/use-mutation.tsx b/apps/desktop/src/renderer/hooks/use-mutation.tsx
index e598987b..ed59faf5 100644
--- a/apps/desktop/src/renderer/hooks/use-mutation.tsx
+++ b/apps/desktop/src/renderer/hooks/use-mutation.tsx
@@ -1,11 +1,16 @@
import React from 'react';
-import { MutationInput, MutationMap } from '@/shared/mutations';
+import {
+ MutationError,
+ MutationErrorData,
+ MutationInput,
+ MutationMap,
+} from '@/shared/mutations';
interface MutationOptions<T extends MutationInput> {
input: T;
onSuccess?: (output: MutationMap[T['type']]['output']) => void;
- onError?: (error: Error) => void;
+ onError?: (error: MutationErrorData) => void;
}
export const useMutation = () => {
@@ -15,12 +20,22 @@ export const useMutation = () => {
async <T extends MutationInput>(options: MutationOptions<T>) => {
setIsPending(true);
try {
- const output = await window.colanode.executeMutation(options.input);
- options.onSuccess?.(output);
- return output;
+ const result = await window.colanode.executeMutation(options.input);
+ if (result.success) {
+ options.onSuccess?.(result.output);
+ return;
+ }
+
+ throw new MutationError(result.error.code, result.error.message);
} catch (error) {
- options.onError?.(error as Error);
- throw error;
+ if (error instanceof MutationError) {
+ options.onError?.(error);
+ } else {
+ options.onError?.({
+ code: 'unknown',
+ message: 'Something went wrong trying to execute the mutation.',
+ });
+ }
} finally {
setIsPending(false);
}
diff --git a/apps/desktop/src/shared/mutations/accounts/email-login.ts b/apps/desktop/src/shared/mutations/accounts/email-login.ts
index 79d29db4..d794d9af 100644
--- a/apps/desktop/src/shared/mutations/accounts/email-login.ts
+++ b/apps/desktop/src/shared/mutations/accounts/email-login.ts
@@ -9,20 +9,11 @@ export type EmailLoginMutationInput = {
password: string;
};
-export type EmailLoginMutationOutput =
- | EmailLoginMutationSuccessOutput
- | EmailLoginMutationErrorOutput;
-
-export type EmailLoginMutationSuccessOutput = {
- success: true;
+export type EmailLoginMutationOutput = {
account: Account;
workspaces: WorkspaceOutput[];
};
-export type EmailLoginMutationErrorOutput = {
- success: false;
-};
-
declare module '@/shared/mutations' {
interface MutationMap {
email_login: {
diff --git a/apps/desktop/src/shared/mutations/accounts/email-register.ts b/apps/desktop/src/shared/mutations/accounts/email-register.ts
index 0a4caec5..7cab0ed6 100644
--- a/apps/desktop/src/shared/mutations/accounts/email-register.ts
+++ b/apps/desktop/src/shared/mutations/accounts/email-register.ts
@@ -10,20 +10,11 @@ export type EmailRegisterMutationInput = {
password: string;
};
-export type EmailRegisterMutationOutput =
- | EmailRegisterMutationSuccessOutput
- | EmailRegisterMutationErrorOutput;
-
-export type EmailRegisterMutationSuccessOutput = {
- success: true;
+export type EmailRegisterMutationOutput = {
account: Account;
workspaces: WorkspaceOutput[];
};
-export type EmailRegisterMutationErrorOutput = {
- success: false;
-};
-
declare module '@/shared/mutations' {
interface MutationMap {
email_register: {
diff --git a/apps/desktop/src/shared/mutations/accounts/errors.ts b/apps/desktop/src/shared/mutations/accounts/errors.ts
deleted file mode 100644
index 5e1e9ba8..00000000
--- a/apps/desktop/src/shared/mutations/accounts/errors.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-export type AccountNotFoundError = {
- type: 'account_not_found';
- message: string;
-};
-
-declare module '@/shared/mutations' {
- interface MutationErrorMap {
- account_not_found: AccountNotFoundError;
- }
-}
diff --git a/apps/desktop/src/shared/mutations/avatars/avatar-upload.ts b/apps/desktop/src/shared/mutations/avatars/avatar-upload.ts
index acb47f1c..758631bf 100644
--- a/apps/desktop/src/shared/mutations/avatars/avatar-upload.ts
+++ b/apps/desktop/src/shared/mutations/avatars/avatar-upload.ts
@@ -5,8 +5,7 @@ export type AvatarUploadMutationInput = {
};
export type AvatarUploadMutationOutput = {
- status: 'success' | 'error' | 'canceled';
- id: string | null;
+ id: string;
};
declare module '@/shared/mutations' {
diff --git a/apps/desktop/src/shared/mutations/index.ts b/apps/desktop/src/shared/mutations/index.ts
index c01210de..49aa06d1 100644
--- a/apps/desktop/src/shared/mutations/index.ts
+++ b/apps/desktop/src/shared/mutations/index.ts
@@ -1,7 +1,53 @@
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface MutationMap {}
-// eslint-disable-next-line @typescript-eslint/no-empty-object-type
-export interface MutationErrorMap {}
-
export type MutationInput = MutationMap[keyof MutationMap]['input'];
+
+export type MutationErrorData = {
+ code: MutationErrorCode;
+ message: string;
+};
+
+export type SuccessMutationResult<T extends MutationInput> = {
+ success: true;
+ output: MutationMap[T['type']]['output'];
+};
+
+export type ErrorMutationResult = {
+ success: false;
+ error: MutationErrorData;
+};
+
+export type MutationResult<T extends MutationInput> =
+ | SuccessMutationResult<T>
+ | ErrorMutationResult;
+
+export class MutationError extends Error {
+ constructor(
+ public code: MutationErrorCode,
+ message: string
+ ) {
+ super(message);
+ }
+}
+
+export type MutationErrorCode =
+ | 'unknown'
+ | 'account_not_found'
+ | 'account_login_failed'
+ | 'account_register_failed'
+ | 'server_not_found'
+ | 'invalid_attributes'
+ | 'space_not_found'
+ | 'channel_not_found'
+ | 'unauthorized'
+ | 'invalid_file'
+ | 'node_not_found'
+ | 'view_not_found'
+ | 'field_not_found'
+ | 'invalid_field_type'
+ | 'select_option_not_found'
+ | 'message_not_found'
+ | 'invalid_server_domain'
+ | 'server_already_exists'
+ | 'workspace_not_found';
diff --git a/apps/desktop/src/shared/types/window.d.ts b/apps/desktop/src/shared/types/window.d.ts
index c69e07c0..da81ffaa 100644
--- a/apps/desktop/src/shared/types/window.d.ts
+++ b/apps/desktop/src/shared/types/window.d.ts
@@ -1,13 +1,13 @@
-import { CommandInput,CommandMap } from '@/shared/commands';
+import { CommandInput, CommandMap } from '@/shared/commands';
import { EventBus } from '@/shared/lib/event-bus';
-import { MutationInput, MutationMap } from '@/shared/mutations';
+import { MutationInput, MutationResult } from '@/shared/mutations';
import { QueryInput, QueryMap } from '@/shared/queries';
export interface ColanodeApi {
init: () => Promise<void>;
executeMutation: <T extends MutationInput>(
input: T
- ) => Promise<MutationMap[T['type']]['output']>;
+ ) => Promise<MutationResult<T>>;
executeQuery: <T extends QueryInput>(
input: T
) => Promise<QueryMap[T['type']]['output']>;