Skip to content

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

Refactor mutation errors

Hakan Shehu670a25d780a7
diff --git a/apps/desktop/src/main/mutations/accounts/account-logout.ts b/apps/desktop/src/main/mutations/accounts/account-logout.ts
index 8ce444ca..3479505c 100644
--- a/apps/desktop/src/main/mutations/accounts/account-logout.ts
+++ b/apps/desktop/src/main/mutations/accounts/account-logout.ts
@@ -1,7 +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 { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
AccountLogoutMutationInput,
AccountLogoutMutationOutput,
@@ -21,7 +21,7 @@ export class AccountLogoutMutationHandler
if (!account) {
throw new MutationError(
- 'account_not_found',
+ MutationErrorCode.AccountNotFound,
'Account was not found or has been logged out already. Try closing the app and opening it again.'
);
}
diff --git a/apps/desktop/src/main/mutations/accounts/account-update.ts b/apps/desktop/src/main/mutations/accounts/account-update.ts
index 1a4a8cd6..c9740d1f 100644
--- a/apps/desktop/src/main/mutations/accounts/account-update.ts
+++ b/apps/desktop/src/main/mutations/accounts/account-update.ts
@@ -8,7 +8,7 @@ import {
AccountUpdateMutationInput,
AccountUpdateMutationOutput,
} from '@/shared/mutations/accounts/account-update';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import { parseApiError } from '@/shared/lib/axios';
export class AccountUpdateMutationHandler
@@ -25,7 +25,7 @@ export class AccountUpdateMutationHandler
if (!account) {
throw new MutationError(
- 'account_not_found',
+ MutationErrorCode.AccountNotFound,
'Account not found or has been logged out already. Try closing the app and opening it again.'
);
}
@@ -38,7 +38,7 @@ export class AccountUpdateMutationHandler
if (!server) {
throw new MutationError(
- 'server_not_found',
+ MutationErrorCode.ServerNotFound,
`The server ${account.server} associated with this account was not found. Try closing the app and opening it again.`
);
}
@@ -68,7 +68,7 @@ export class AccountUpdateMutationHandler
if (!updatedAccount) {
throw new MutationError(
- 'account_not_found',
+ MutationErrorCode.AccountNotFound,
'Account not found or has been logged out already. Try closing the app and opening it again.'
);
}
@@ -92,7 +92,7 @@ export class AccountUpdateMutationHandler
};
} catch (error) {
const apiError = parseApiError(error);
- throw new MutationError('api_error', apiError.message);
+ throw new MutationError(MutationErrorCode.ApiError, apiError.message);
}
}
}
diff --git a/apps/desktop/src/main/mutations/accounts/email-login.ts b/apps/desktop/src/main/mutations/accounts/email-login.ts
index 5d72c2cf..214b616d 100644
--- a/apps/desktop/src/main/mutations/accounts/email-login.ts
+++ b/apps/desktop/src/main/mutations/accounts/email-login.ts
@@ -8,7 +8,7 @@ import {
EmailLoginMutationInput,
EmailLoginMutationOutput,
} from '@/shared/mutations/accounts/email-login';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import { parseApiError } from '@/shared/lib/axios';
import { mapAccount, mapWorkspace } from '@/main/utils';
@@ -26,7 +26,7 @@ export class EmailLoginMutationHandler
if (!server) {
throw new MutationError(
- 'server_not_found',
+ MutationErrorCode.ServerNotFound,
`Server ${input.server} was not found! Try using a different server.`
);
}
@@ -62,7 +62,7 @@ export class EmailLoginMutationHandler
if (!createdAccount) {
throw new MutationError(
- 'account_login_failed',
+ MutationErrorCode.AccountLoginFailed,
'Failed to login with email and password! Please try again.'
);
}
@@ -93,7 +93,7 @@ export class EmailLoginMutationHandler
if (!createdAccount) {
throw new MutationError(
- 'account_login_failed',
+ MutationErrorCode.AccountLoginFailed,
'Failed to login with email and password! Please try again.'
);
}
@@ -119,7 +119,7 @@ export class EmailLoginMutationHandler
};
} catch (error) {
const apiError = parseApiError(error);
- throw new MutationError('api_error', apiError.message);
+ throw new MutationError(MutationErrorCode.ApiError, apiError.message);
}
}
}
diff --git a/apps/desktop/src/main/mutations/accounts/email-register.ts b/apps/desktop/src/main/mutations/accounts/email-register.ts
index 41f9e782..e71d73f7 100644
--- a/apps/desktop/src/main/mutations/accounts/email-register.ts
+++ b/apps/desktop/src/main/mutations/accounts/email-register.ts
@@ -8,7 +8,7 @@ import {
EmailRegisterMutationInput,
EmailRegisterMutationOutput,
} from '@/shared/mutations/accounts/email-register';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import { parseApiError } from '@/shared/lib/axios';
import { mapAccount, mapWorkspace } from '@/main/utils';
@@ -26,7 +26,7 @@ export class EmailRegisterMutationHandler
if (!server) {
throw new MutationError(
- 'server_not_found',
+ MutationErrorCode.ServerNotFound,
`Server ${input.server} was not found! Try using a different server.`
);
}
@@ -63,8 +63,8 @@ export class EmailRegisterMutationHandler
if (!createdAccount) {
throw new MutationError(
- 'account_login_failed',
- 'Failed to login with email and password! Please try again.'
+ MutationErrorCode.AccountRegisterFailed,
+ 'Failed to register with email and password! Please try again.'
);
}
@@ -94,8 +94,8 @@ export class EmailRegisterMutationHandler
if (!createdAccount) {
throw new MutationError(
- 'account_login_failed',
- 'Failed to login with email and password! Please try again.'
+ MutationErrorCode.AccountRegisterFailed,
+ 'Failed to register with email and password! Please try again.'
);
}
@@ -120,7 +120,7 @@ export class EmailRegisterMutationHandler
};
} catch (error) {
const apiError = parseApiError(error);
- throw new MutationError('api_error', apiError.message);
+ throw new MutationError(MutationErrorCode.ApiError, apiError.message);
}
}
}
diff --git a/apps/desktop/src/main/mutations/avatars/avatar-upload.ts b/apps/desktop/src/main/mutations/avatars/avatar-upload.ts
index 52a64d92..356f3eef 100644
--- a/apps/desktop/src/main/mutations/avatars/avatar-upload.ts
+++ b/apps/desktop/src/main/mutations/avatars/avatar-upload.ts
@@ -9,7 +9,7 @@ import {
AvatarUploadMutationInput,
AvatarUploadMutationOutput,
} from '@/shared/mutations/avatars/avatar-upload';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import { parseApiError } from '@/shared/lib/axios';
interface AvatarUploadResponse {
@@ -31,7 +31,7 @@ export class AvatarUploadMutationHandler
if (!credentials) {
throw new MutationError(
- 'account_not_found',
+ MutationErrorCode.AccountNotFound,
'Account not found or has been logged out already. Try closing the app and opening it again.'
);
}
@@ -58,7 +58,7 @@ export class AvatarUploadMutationHandler
};
} catch (error) {
const apiError = parseApiError(error);
- throw new MutationError('api_error', apiError.message);
+ throw new MutationError(MutationErrorCode.ApiError, apiError.message);
}
}
}
diff --git a/apps/desktop/src/main/mutations/channels/channel-create.ts b/apps/desktop/src/main/mutations/channels/channel-create.ts
index 8431b319..d460e379 100644
--- a/apps/desktop/src/main/mutations/channels/channel-create.ts
+++ b/apps/desktop/src/main/mutations/channels/channel-create.ts
@@ -7,7 +7,7 @@ import {
ChannelCreateMutationInput,
ChannelCreateMutationOutput,
} from '@/shared/mutations/channels/channel-create';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class ChannelCreateMutationHandler
implements MutationHandler<ChannelCreateMutationInput>
@@ -27,7 +27,7 @@ export class ChannelCreateMutationHandler
if (!space) {
throw new MutationError(
- 'space_not_found',
+ MutationErrorCode.SpaceNotFound,
'Space not found or has been deleted.'
);
}
diff --git a/apps/desktop/src/main/mutations/channels/channel-update.ts b/apps/desktop/src/main/mutations/channels/channel-update.ts
index 80500e74..11b51fbe 100644
--- a/apps/desktop/src/main/mutations/channels/channel-update.ts
+++ b/apps/desktop/src/main/mutations/channels/channel-update.ts
@@ -1,6 +1,8 @@
+import { ChannelAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
ChannelUpdateMutationInput,
ChannelUpdateMutationOutput,
@@ -12,17 +14,10 @@ export class ChannelUpdateMutationHandler
async handleMutation(
input: ChannelUpdateMutationInput
): Promise<ChannelUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<ChannelAttributes>(
input.channelId,
input.userId,
(attributes) => {
- if (attributes.type !== 'channel') {
- throw new MutationError(
- 'invalid_attributes',
- 'Something went wrong while updating the channel.'
- );
- }
-
attributes.name = input.name;
attributes.avatar = input.avatar;
@@ -32,21 +27,21 @@ export class ChannelUpdateMutationHandler
if (result === 'not_found') {
throw new MutationError(
- 'channel_not_found',
+ MutationErrorCode.ChannelNotFound,
'Channel not found or has been deleted.'
);
}
if (result === 'invalid_attributes') {
throw new MutationError(
- 'invalid_attributes',
+ MutationErrorCode.ChannelUpdateFailed,
'Something went wrong while updating the channel.'
);
}
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.ChannelUpdateForbidden,
"You don't have permission to update this channel."
);
}
@@ -58,7 +53,7 @@ export class ChannelUpdateMutationHandler
}
throw new MutationError(
- 'unknown',
+ MutationErrorCode.Unknown,
'Something went wrong while updating the channel.'
);
}
diff --git a/apps/desktop/src/main/mutations/databases/database-update.ts b/apps/desktop/src/main/mutations/databases/database-update.ts
index 9fd99f67..33521485 100644
--- a/apps/desktop/src/main/mutations/databases/database-update.ts
+++ b/apps/desktop/src/main/mutations/databases/database-update.ts
@@ -1,6 +1,8 @@
+import { DatabaseAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
DatabaseUpdateMutationInput,
DatabaseUpdateMutationOutput,
@@ -12,17 +14,10 @@ export class DatabaseUpdateMutationHandler
async handleMutation(
input: DatabaseUpdateMutationInput
): Promise<DatabaseUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError(
- 'invalid_attributes',
- 'Node is not a database'
- );
- }
-
attributes.name = input.name;
attributes.avatar = input.avatar;
@@ -32,14 +27,14 @@ export class DatabaseUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.DatabaseUpdateForbidden,
"You don't have permission to update this database."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.DatabaseUpdateFailed,
'Something went wrong while updating the database.'
);
}
diff --git a/apps/desktop/src/main/mutations/databases/field-create.ts b/apps/desktop/src/main/mutations/databases/field-create.ts
index fca9371e..e0caf8c3 100644
--- a/apps/desktop/src/main/mutations/databases/field-create.ts
+++ b/apps/desktop/src/main/mutations/databases/field-create.ts
@@ -13,7 +13,7 @@ import {
FieldCreateMutationInput,
FieldCreateMutationOutput,
} from '@/shared/mutations/databases/field-create';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import { databaseService } from '@/main/data/database-service';
import { fetchEntry } from '@/main/utils';
@@ -26,7 +26,7 @@ export class FieldCreateMutationHandler
if (input.fieldType === 'relation') {
if (!input.relationDatabaseId) {
throw new MutationError(
- 'relation_database_not_found',
+ MutationErrorCode.RelationDatabaseNotFound,
'Relation database not found.'
);
}
@@ -42,7 +42,7 @@ export class FieldCreateMutationHandler
if (!relationDatabase || relationDatabase.type !== 'database') {
throw new MutationError(
- 'relation_database_not_found',
+ MutationErrorCode.RelationDatabaseNotFound,
'Relation database not found.'
);
}
@@ -82,14 +82,14 @@ export class FieldCreateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.FieldCreateForbidden,
"You don't have permission to create a field in this database."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.FieldCreateFailed,
'Something went wrong while creating the field.'
);
}
diff --git a/apps/desktop/src/main/mutations/databases/field-delete.ts b/apps/desktop/src/main/mutations/databases/field-delete.ts
index a397ec29..d198855a 100644
--- a/apps/desktop/src/main/mutations/databases/field-delete.ts
+++ b/apps/desktop/src/main/mutations/databases/field-delete.ts
@@ -1,6 +1,8 @@
+import { DatabaseAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
FieldDeleteMutationInput,
FieldDeleteMutationOutput,
@@ -12,17 +14,13 @@ export class FieldDeleteMutationHandler
async handleMutation(
input: FieldDeleteMutationInput
): Promise<FieldDeleteMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError('invalid_attributes', 'Invalid node type');
- }
-
if (!attributes.fields[input.fieldId]) {
throw new MutationError(
- 'field_not_found',
+ MutationErrorCode.FieldNotFound,
'The field you are trying to delete does not exist.'
);
}
@@ -35,14 +33,14 @@ export class FieldDeleteMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.FieldDeleteForbidden,
"You don't have permission to delete this field."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.FieldDeleteFailed,
'Something went wrong while deleting the field.'
);
}
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 e20f544c..434cf67f 100644
--- a/apps/desktop/src/main/mutations/databases/field-name-update.ts
+++ b/apps/desktop/src/main/mutations/databases/field-name-update.ts
@@ -1,6 +1,8 @@
+import { DatabaseAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
FieldNameUpdateMutationInput,
FieldNameUpdateMutationOutput,
@@ -12,18 +14,14 @@ export class FieldNameUpdateMutationHandler
async handleMutation(
input: FieldNameUpdateMutationInput
): Promise<FieldNameUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError('invalid_attributes', 'Invalid node type');
- }
-
const field = attributes.fields[input.fieldId];
if (!field) {
throw new MutationError(
- 'field_not_found',
+ MutationErrorCode.FieldNotFound,
'The field you are trying to update does not exist.'
);
}
@@ -35,14 +33,14 @@ export class FieldNameUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.FieldUpdateForbidden,
"You don't have permission to update this field."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.FieldUpdateFailed,
'Something went wrong while updating the field.'
);
}
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 e955983b..f829eb62 100644
--- a/apps/desktop/src/main/mutations/databases/select-option-create.ts
+++ b/apps/desktop/src/main/mutations/databases/select-option-create.ts
@@ -1,5 +1,6 @@
import {
compareString,
+ DatabaseAttributes,
generateId,
generateNodeIndex,
IdType,
@@ -11,7 +12,7 @@ import {
SelectOptionCreateMutationInput,
SelectOptionCreateMutationOutput,
} from '@/shared/mutations/databases/select-option-create';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class SelectOptionCreateMutationHandler
implements MutationHandler<SelectOptionCreateMutationInput>
@@ -20,28 +21,21 @@ export class SelectOptionCreateMutationHandler
input: SelectOptionCreateMutationInput
): Promise<SelectOptionCreateMutationOutput> {
const id = generateId(IdType.SelectOption);
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError(
- 'invalid_attributes',
- 'Node is not a database'
- );
- }
-
const field = attributes.fields[input.fieldId];
if (!field) {
throw new MutationError(
- 'field_not_found',
+ MutationErrorCode.FieldNotFound,
'The field you are trying to create a select option in does not exist.'
);
}
if (field.type !== 'select' && field.type !== 'multiSelect') {
throw new MutationError(
- 'invalid_field_type',
+ MutationErrorCode.FieldTypeInvalid,
'The field you are trying to create a select option in is not a "Select" or "Multi-Select" field.'
);
}
@@ -69,14 +63,14 @@ export class SelectOptionCreateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.SelectOptionCreateForbidden,
"You don't have permission to create a select option in this field."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.SelectOptionCreateFailed,
'Something went wrong while creating the select option.'
);
}
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 ee799fdb..898523ee 100644
--- a/apps/desktop/src/main/mutations/databases/select-option-delete.ts
+++ b/apps/desktop/src/main/mutations/databases/select-option-delete.ts
@@ -1,6 +1,8 @@
+import { DatabaseAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
SelectOptionDeleteMutationInput,
SelectOptionDeleteMutationOutput,
@@ -12,42 +14,35 @@ export class SelectOptionDeleteMutationHandler
async handleMutation(
input: SelectOptionDeleteMutationInput
): Promise<SelectOptionDeleteMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError(
- 'invalid_attributes',
- 'Node is not a database'
- );
- }
-
const field = attributes.fields[input.fieldId];
if (!field) {
throw new MutationError(
- 'field_not_found',
+ MutationErrorCode.FieldNotFound,
'The field you are trying to delete a select option from does not exist.'
);
}
if (field.type !== 'select' && field.type !== 'multiSelect') {
throw new MutationError(
- 'invalid_field_type',
+ MutationErrorCode.FieldTypeInvalid,
'The field you are trying to delete a select option from is not a "Select" or "Multi-Select" field.'
);
}
if (!field.options) {
throw new MutationError(
- 'select_option_not_found',
+ MutationErrorCode.SelectOptionNotFound,
'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',
+ MutationErrorCode.SelectOptionNotFound,
'The select option you are trying to delete does not exist.'
);
}
@@ -60,14 +55,14 @@ export class SelectOptionDeleteMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.SelectOptionDeleteForbidden,
"You don't have permission to delete this select option."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.SelectOptionDeleteFailed,
'Something went wrong while deleting the select option.'
);
}
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 4c42fe1b..9de23725 100644
--- a/apps/desktop/src/main/mutations/databases/select-option-update.ts
+++ b/apps/desktop/src/main/mutations/databases/select-option-update.ts
@@ -1,6 +1,8 @@
+import { DatabaseAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
SelectOptionUpdateMutationInput,
SelectOptionUpdateMutationOutput,
@@ -12,28 +14,21 @@ export class SelectOptionUpdateMutationHandler
async handleMutation(
input: SelectOptionUpdateMutationInput
): Promise<SelectOptionUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError(
- 'invalid_attributes',
- 'Node is not a database'
- );
- }
-
const field = attributes.fields[input.fieldId];
if (!field) {
throw new MutationError(
- 'field_not_found',
+ MutationErrorCode.FieldNotFound,
'The field you are trying to update a select option in does not exist.'
);
}
if (field.type !== 'select' && field.type !== 'multiSelect') {
throw new MutationError(
- 'invalid_field_type',
+ MutationErrorCode.FieldTypeInvalid,
'The field you are trying to update a select option in is not a "Select" or "Multi-Select" field.'
);
}
@@ -45,7 +40,7 @@ export class SelectOptionUpdateMutationHandler
const option = field.options[input.optionId];
if (!option) {
throw new MutationError(
- 'select_option_not_found',
+ MutationErrorCode.SelectOptionNotFound,
'The select option you are trying to update does not exist.'
);
}
@@ -58,14 +53,14 @@ export class SelectOptionUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.SelectOptionUpdateForbidden,
"You don't have permission to update this select option."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.SelectOptionUpdateFailed,
'Something went wrong while updating the select option.'
);
}
diff --git a/apps/desktop/src/main/mutations/databases/view-create.ts b/apps/desktop/src/main/mutations/databases/view-create.ts
index 42758dc2..388a90e5 100644
--- a/apps/desktop/src/main/mutations/databases/view-create.ts
+++ b/apps/desktop/src/main/mutations/databases/view-create.ts
@@ -1,5 +1,6 @@
import {
compareString,
+ DatabaseAttributes,
generateId,
generateNodeIndex,
IdType,
@@ -11,7 +12,7 @@ import {
ViewCreateMutationInput,
ViewCreateMutationOutput,
} from '@/shared/mutations/databases/view-create';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class ViewCreateMutationHandler
implements MutationHandler<ViewCreateMutationInput>
@@ -20,17 +21,10 @@ export class ViewCreateMutationHandler
input: ViewCreateMutationInput
): Promise<ViewCreateMutationOutput> {
const id = generateId(IdType.View);
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError(
- 'invalid_attributes',
- 'Node is not a database'
- );
- }
-
const maxIndex = Object.values(attributes.views)
.map((view) => view.index)
.sort((a, b) => -compareString(a, b))[0];
@@ -53,14 +47,14 @@ export class ViewCreateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.ViewCreateForbidden,
"You don't have permission to create a view in this database."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.ViewCreateFailed,
'Something went wrong while creating the view.'
);
}
diff --git a/apps/desktop/src/main/mutations/databases/view-delete.ts b/apps/desktop/src/main/mutations/databases/view-delete.ts
index 50699907..8de4595d 100644
--- a/apps/desktop/src/main/mutations/databases/view-delete.ts
+++ b/apps/desktop/src/main/mutations/databases/view-delete.ts
@@ -1,6 +1,8 @@
+import { DatabaseAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
ViewDeleteMutationInput,
ViewDeleteMutationOutput,
@@ -12,20 +14,13 @@ export class ViewDeleteMutationHandler
async handleMutation(
input: ViewDeleteMutationInput
): Promise<ViewDeleteMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError(
- 'invalid_attributes',
- 'Node is not a database'
- );
- }
-
if (!attributes.views[input.viewId]) {
throw new MutationError(
- 'view_not_found',
+ MutationErrorCode.ViewNotFound,
'The view you are trying to delete does not exist.'
);
}
@@ -37,14 +32,14 @@ export class ViewDeleteMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.ViewDeleteForbidden,
"You don't have permission to delete this view."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.ViewDeleteFailed,
'Something went wrong while deleting the view.'
);
}
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 f33f9381..e7c41bbb 100644
--- a/apps/desktop/src/main/mutations/databases/view-name-update.ts
+++ b/apps/desktop/src/main/mutations/databases/view-name-update.ts
@@ -1,6 +1,8 @@
+import { DatabaseAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
ViewNameUpdateMutationInput,
ViewNameUpdateMutationOutput,
@@ -12,21 +14,14 @@ export class ViewNameUpdateMutationHandler
async handleMutation(
input: ViewNameUpdateMutationInput
): Promise<ViewNameUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError(
- 'invalid_attributes',
- 'Node is not a database'
- );
- }
-
const view = attributes.views[input.viewId];
if (!view) {
throw new MutationError(
- 'view_not_found',
+ MutationErrorCode.ViewNotFound,
'The view you are trying to update the name of does not exist.'
);
}
@@ -38,14 +33,14 @@ export class ViewNameUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.ViewUpdateForbidden,
"You don't have permission to update this view."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.ViewUpdateFailed,
'Something went wrong while updating the view.'
);
}
diff --git a/apps/desktop/src/main/mutations/databases/view-update.ts b/apps/desktop/src/main/mutations/databases/view-update.ts
index 06457034..c56840df 100644
--- a/apps/desktop/src/main/mutations/databases/view-update.ts
+++ b/apps/desktop/src/main/mutations/databases/view-update.ts
@@ -1,6 +1,8 @@
+import { DatabaseAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
ViewUpdateMutationInput,
ViewUpdateMutationOutput,
@@ -12,20 +14,13 @@ export class ViewUpdateMutationHandler
async handleMutation(
input: ViewUpdateMutationInput
): Promise<ViewUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<DatabaseAttributes>(
input.databaseId,
input.userId,
(attributes) => {
- if (attributes.type !== 'database') {
- throw new MutationError(
- 'invalid_attributes',
- 'Node is not a database'
- );
- }
-
if (!attributes.views[input.view.id]) {
throw new MutationError(
- 'view_not_found',
+ MutationErrorCode.ViewNotFound,
'The view you are trying to update does not exist.'
);
}
@@ -37,14 +32,14 @@ export class ViewUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.ViewUpdateForbidden,
"You don't have permission to update this view."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.ViewUpdateFailed,
'Something went wrong while updating the view.'
);
}
diff --git a/apps/desktop/src/main/mutations/entries/entry-collaborator-create.ts b/apps/desktop/src/main/mutations/entries/entry-collaborator-create.ts
index 7af0ed38..d712d941 100644
--- a/apps/desktop/src/main/mutations/entries/entry-collaborator-create.ts
+++ b/apps/desktop/src/main/mutations/entries/entry-collaborator-create.ts
@@ -6,7 +6,7 @@ import {
EntryCollaboratorCreateMutationInput,
EntryCollaboratorCreateMutationOutput,
} from '@/shared/mutations/entries/entry-collaborator-create';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class EntryCollaboratorCreateMutationHandler
implements MutationHandler<EntryCollaboratorCreateMutationInput>
@@ -27,14 +27,14 @@ export class EntryCollaboratorCreateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.EntryCollaboratorCreateForbidden,
"You don't have permission to add collaborators to this entry."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.EntryCollaboratorCreateFailed,
'Something went wrong while adding collaborators to the entry.'
);
}
diff --git a/apps/desktop/src/main/mutations/entries/entry-collaborator-delete.ts b/apps/desktop/src/main/mutations/entries/entry-collaborator-delete.ts
index 1042d2df..1f4ffa35 100644
--- a/apps/desktop/src/main/mutations/entries/entry-collaborator-delete.ts
+++ b/apps/desktop/src/main/mutations/entries/entry-collaborator-delete.ts
@@ -6,7 +6,7 @@ import {
EntryCollaboratorDeleteMutationInput,
EntryCollaboratorDeleteMutationOutput,
} from '@/shared/mutations/entries/entry-collaborator-delete';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class EntryCollaboratorDeleteMutationHandler
implements MutationHandler<EntryCollaboratorDeleteMutationInput>
@@ -25,14 +25,14 @@ export class EntryCollaboratorDeleteMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.EntryCollaboratorDeleteForbidden,
"You don't have permission to remove collaborators from this node."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.EntryCollaboratorDeleteFailed,
'Something went wrong while removing collaborators from the node.'
);
}
diff --git a/apps/desktop/src/main/mutations/entries/entry-collaborator-update.ts b/apps/desktop/src/main/mutations/entries/entry-collaborator-update.ts
index a0e7d5dd..1e3e29b5 100644
--- a/apps/desktop/src/main/mutations/entries/entry-collaborator-update.ts
+++ b/apps/desktop/src/main/mutations/entries/entry-collaborator-update.ts
@@ -6,7 +6,7 @@ import {
EntryCollaboratorUpdateMutationInput,
EntryCollaboratorUpdateMutationOutput,
} from '@/shared/mutations/entries/entry-collaborator-update';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class EntryCollaboratorUpdateMutationHandler
implements MutationHandler<EntryCollaboratorUpdateMutationInput>
@@ -25,14 +25,14 @@ export class EntryCollaboratorUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.EntryCollaboratorUpdateForbidden,
"You don't have permission to update collaborators for this entry."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.EntryCollaboratorUpdateFailed,
'Something went wrong while updating collaborators for the entry.'
);
}
diff --git a/apps/desktop/src/main/mutations/files/file-create.ts b/apps/desktop/src/main/mutations/files/file-create.ts
index 7b762b6d..2d076849 100644
--- a/apps/desktop/src/main/mutations/files/file-create.ts
+++ b/apps/desktop/src/main/mutations/files/file-create.ts
@@ -12,7 +12,7 @@ import {
FileCreateMutationInput,
FileCreateMutationOutput,
} from '@/shared/mutations/files/file-create';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import { databaseService } from '@/main/data/database-service';
import { eventBus } from '@/shared/lib/event-bus';
import { fetchEntry, fetchUser, mapEntry, mapFile } from '@/main/utils';
@@ -26,7 +26,7 @@ export class FileCreateMutationHandler
const metadata = fileService.getFileMetadata(input.filePath);
if (!metadata) {
throw new MutationError(
- 'invalid_file',
+ MutationErrorCode.FileInvalid,
'File is invalid or could not be read.'
);
}
@@ -37,17 +37,26 @@ export class FileCreateMutationHandler
const user = await fetchUser(workspaceDatabase, input.userId);
if (!user) {
- throw new MutationError('user_not_found', 'User not found.');
+ throw new MutationError(
+ MutationErrorCode.UserNotFound,
+ 'There was an error while fetching the user. Please make sure you are logged in.'
+ );
}
const entry = await fetchEntry(workspaceDatabase, input.entryId);
if (!entry) {
- throw new MutationError('entry_not_found', 'Entry not found.');
+ throw new MutationError(
+ MutationErrorCode.EntryNotFound,
+ 'There was an error while fetching the entry. Please make sure you have access to this entry.'
+ );
}
const root = await fetchEntry(workspaceDatabase, input.rootId);
if (!root) {
- throw new MutationError('entry_not_found', 'Entry not found.');
+ throw new MutationError(
+ MutationErrorCode.RootNotFound,
+ 'There was an error while fetching the root. Please make sure you have access to this root.'
+ );
}
const fileId = generateId(IdType.File);
@@ -66,7 +75,7 @@ export class FileCreateMutationHandler
})
) {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.FileCreateForbidden,
'You are not allowed to upload a file in this entry.'
);
}
diff --git a/apps/desktop/src/main/mutations/files/file-delete.ts b/apps/desktop/src/main/mutations/files/file-delete.ts
index f2ba8e73..5fef77e5 100644
--- a/apps/desktop/src/main/mutations/files/file-delete.ts
+++ b/apps/desktop/src/main/mutations/files/file-delete.ts
@@ -13,7 +13,7 @@ import {
} from '@/shared/mutations/files/file-delete';
import { eventBus } from '@/shared/lib/event-bus';
import { fetchEntry, fetchUser, mapEntry, mapFile } from '@/main/utils';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class FileDeleteMutationHandler
implements MutationHandler<FileDeleteMutationInput>
@@ -33,24 +33,33 @@ export class FileDeleteMutationHandler
if (!file) {
throw new MutationError(
- 'file_not_found',
+ MutationErrorCode.FileNotFound,
'File could not be found or has been already deleted.'
);
}
const user = await fetchUser(workspaceDatabase, input.userId);
if (!user) {
- throw new MutationError('user_not_found', 'User not found.');
+ throw new MutationError(
+ MutationErrorCode.UserNotFound,
+ 'There was an error while fetching the user. Please make sure you are logged in.'
+ );
}
const entry = await fetchEntry(workspaceDatabase, file.root_id);
if (!entry) {
- throw new MutationError('entry_not_found', 'Entry not found.');
+ throw new MutationError(
+ MutationErrorCode.EntryNotFound,
+ 'There was an error while fetching the entry. Please make sure you have access to this entry.'
+ );
}
const root = await fetchEntry(workspaceDatabase, entry.root_id);
if (!root) {
- throw new MutationError('entry_not_found', 'Entry not found.');
+ throw new MutationError(
+ MutationErrorCode.RootNotFound,
+ 'There was an error while fetching the root. Please make sure you have access to this root.'
+ );
}
if (
@@ -69,7 +78,7 @@ export class FileDeleteMutationHandler
})
) {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.FileDeleteForbidden,
'You are not allowed to delete this file.'
);
}
diff --git a/apps/desktop/src/main/mutations/files/file-download.ts b/apps/desktop/src/main/mutations/files/file-download.ts
index 542012a8..dba41abf 100644
--- a/apps/desktop/src/main/mutations/files/file-download.ts
+++ b/apps/desktop/src/main/mutations/files/file-download.ts
@@ -2,7 +2,7 @@ import { databaseService } from '@/main/data/database-service';
import { MutationHandler } from '@/main/types';
import { mapFileState } from '@/main/utils';
import { eventBus } from '@/shared/lib/event-bus';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
FileDownloadMutationInput,
FileDownloadMutationOutput,
@@ -26,7 +26,7 @@ export class FileDownloadMutationHandler
if (!file) {
throw new MutationError(
- 'node_not_found',
+ MutationErrorCode.FileNotFound,
'The file you are trying to download does not exist.'
);
}
diff --git a/apps/desktop/src/main/mutations/folders/folder-update.ts b/apps/desktop/src/main/mutations/folders/folder-update.ts
index 3d871ea3..a9f0555e 100644
--- a/apps/desktop/src/main/mutations/folders/folder-update.ts
+++ b/apps/desktop/src/main/mutations/folders/folder-update.ts
@@ -1,6 +1,8 @@
+import { FolderAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
FolderUpdateMutationInput,
FolderUpdateMutationOutput,
@@ -12,14 +14,10 @@ export class FolderUpdateMutationHandler
async handleMutation(
input: FolderUpdateMutationInput
): Promise<FolderUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<FolderAttributes>(
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;
@@ -29,15 +27,15 @@ export class FolderUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.FolderUpdateForbidden,
"You don't have permission to update this folder."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
- 'Something went wrong while updating the folder.'
+ MutationErrorCode.FolderUpdateFailed,
+ 'There was an error while updating the folder. Please try again.'
);
}
diff --git a/apps/desktop/src/main/mutations/messages/message-create.ts b/apps/desktop/src/main/mutations/messages/message-create.ts
index 4a98316c..d0d2b235 100644
--- a/apps/desktop/src/main/mutations/messages/message-create.ts
+++ b/apps/desktop/src/main/mutations/messages/message-create.ts
@@ -17,7 +17,7 @@ import {
MessageCreateMutationInput,
MessageCreateMutationOutput,
} from '@/shared/mutations/messages/message-create';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
CreateFile,
CreateFileState,
@@ -46,17 +46,26 @@ export class MessageCreateMutationHandler
const user = await fetchUser(workspaceDatabase, input.userId);
if (!user) {
- throw new MutationError('user_not_found', 'User not found.');
+ throw new MutationError(
+ MutationErrorCode.UserNotFound,
+ 'There was an error while fetching the user. Please make sure you are logged in.'
+ );
}
const entry = await fetchEntry(workspaceDatabase, input.conversationId);
if (!entry) {
- throw new MutationError('entry_not_found', 'Conversation not found.');
+ throw new MutationError(
+ MutationErrorCode.EntryNotFound,
+ 'There was an error while fetching the conversation. Please make sure you have access to this conversation.'
+ );
}
const root = await fetchEntry(workspaceDatabase, input.rootId);
if (!root) {
- throw new MutationError('entry_not_found', 'Conversation not found.');
+ throw new MutationError(
+ MutationErrorCode.RootNotFound,
+ 'There was an error while fetching the root. Please make sure you have access to this root.'
+ );
}
if (
@@ -70,7 +79,7 @@ export class MessageCreateMutationHandler
})
) {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.MessageCreateForbidden,
'You are not allowed to create a message in this conversation.'
);
}
@@ -91,8 +100,8 @@ export class MessageCreateMutationHandler
const metadata = fileService.getFileMetadata(path);
if (!metadata) {
throw new MutationError(
- 'invalid_file',
- 'File attachment is invalid or could not be read.'
+ MutationErrorCode.FileInvalid,
+ 'The file attachment is invalid or could not be read.'
);
}
diff --git a/apps/desktop/src/main/mutations/messages/message-delete.ts b/apps/desktop/src/main/mutations/messages/message-delete.ts
index b55a6b90..e01f330f 100644
--- a/apps/desktop/src/main/mutations/messages/message-delete.ts
+++ b/apps/desktop/src/main/mutations/messages/message-delete.ts
@@ -13,7 +13,7 @@ import {
} from '@/shared/mutations/messages/message-delete';
import { eventBus } from '@/shared/lib/event-bus';
import { fetchEntry, fetchUser, mapEntry, mapMessage } from '@/main/utils';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class MessageDeleteMutationHandler
implements MutationHandler<MessageDeleteMutationInput>
@@ -39,17 +39,26 @@ export class MessageDeleteMutationHandler
const user = await fetchUser(workspaceDatabase, input.userId);
if (!user) {
- throw new MutationError('user_not_found', 'User not found.');
+ throw new MutationError(
+ MutationErrorCode.UserNotFound,
+ 'There was an error while fetching the user. Please make sure you are logged in.'
+ );
}
const entry = await fetchEntry(workspaceDatabase, message.entry_id);
if (!entry) {
- throw new MutationError('entry_not_found', 'Conversation not found.');
+ throw new MutationError(
+ MutationErrorCode.EntryNotFound,
+ 'There was an error while fetching the conversation. Please make sure you have access to this conversation.'
+ );
}
const root = await fetchEntry(workspaceDatabase, message.root_id);
if (!root) {
- throw new MutationError('entry_not_found', 'Conversation not found.');
+ throw new MutationError(
+ MutationErrorCode.RootNotFound,
+ 'There was an error while fetching the root. Please make sure you have access to this root.'
+ );
}
if (
@@ -67,7 +76,7 @@ export class MessageDeleteMutationHandler
})
) {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.MessageDeleteForbidden,
'You are not allowed to delete this message.'
);
}
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 90b8f131..41f9bfa1 100644
--- a/apps/desktop/src/main/mutations/messages/message-reaction-create.ts
+++ b/apps/desktop/src/main/mutations/messages/message-reaction-create.ts
@@ -18,7 +18,7 @@ import {
mapEntry,
mapMessageReaction,
} from '@/main/utils';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class MessageReactionCreateMutationHandler
implements MutationHandler<MessageReactionCreateMutationInput>
@@ -37,7 +37,10 @@ export class MessageReactionCreateMutationHandler
.executeTakeFirst();
if (!message) {
- throw new MutationError('message_not_found', 'Message not found.');
+ throw new MutationError(
+ MutationErrorCode.MessageNotFound,
+ 'Message not found or has been deleted.'
+ );
}
const existingMessageReaction = await workspaceDatabase
@@ -56,12 +59,18 @@ export class MessageReactionCreateMutationHandler
const user = await fetchUser(workspaceDatabase, input.userId);
if (!user) {
- throw new MutationError('user_not_found', 'User not found.');
+ throw new MutationError(
+ MutationErrorCode.UserNotFound,
+ 'There was an error while fetching the user. Please make sure you are logged in.'
+ );
}
const root = await fetchEntry(workspaceDatabase, input.rootId);
if (!root) {
- throw new MutationError('entry_not_found', 'Conversation not found.');
+ throw new MutationError(
+ MutationErrorCode.RootNotFound,
+ 'There was an error while fetching the root. Please make sure you have access to this root.'
+ );
}
if (
@@ -78,8 +87,8 @@ export class MessageReactionCreateMutationHandler
})
) {
throw new MutationError(
- 'unauthorized',
- 'You are not allowed to react to this message.'
+ MutationErrorCode.MessageReactionCreateForbidden,
+ "You don't have permission to react to this message."
);
}
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 29b8da35..bf86608a 100644
--- a/apps/desktop/src/main/mutations/pages/page-content-update.ts
+++ b/apps/desktop/src/main/mutations/pages/page-content-update.ts
@@ -1,4 +1,4 @@
-import { Block } from '@colanode/core';
+import { Block, PageAttributes } from '@colanode/core';
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
@@ -7,7 +7,7 @@ import {
PageContentUpdateMutationInput,
PageContentUpdateMutationOutput,
} from '@/shared/mutations/pages/page-content-update';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class PageContentUpdateMutationHandler
implements MutationHandler<PageContentUpdateMutationInput>
@@ -15,14 +15,10 @@ export class PageContentUpdateMutationHandler
async handleMutation(
input: PageContentUpdateMutationInput
): Promise<PageContentUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<PageAttributes>(
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)) {
@@ -50,14 +46,14 @@ export class PageContentUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.PageUpdateForbidden,
"You don't have permission to update this page."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.PageUpdateFailed,
'Something went wrong while updating the page content.'
);
}
diff --git a/apps/desktop/src/main/mutations/pages/page-update.ts b/apps/desktop/src/main/mutations/pages/page-update.ts
index e2d9ff46..0bfae973 100644
--- a/apps/desktop/src/main/mutations/pages/page-update.ts
+++ b/apps/desktop/src/main/mutations/pages/page-update.ts
@@ -1,6 +1,8 @@
+import { PageAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
PageUpdateMutationInput,
PageUpdateMutationOutput,
@@ -12,14 +14,10 @@ export class PageUpdateMutationHandler
async handleMutation(
input: PageUpdateMutationInput
): Promise<PageUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<PageAttributes>(
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;
@@ -29,15 +27,15 @@ export class PageUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.PageUpdateForbidden,
"You don't have permission to update this page."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
- 'Something went wrong while updating the page.'
+ MutationErrorCode.PageUpdateFailed,
+ 'Something went wrong while updating the page. Please try again later.'
);
}
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 f5fcd1cd..98ed4daa 100644
--- a/apps/desktop/src/main/mutations/records/record-avatar-update.ts
+++ b/apps/desktop/src/main/mutations/records/record-avatar-update.ts
@@ -1,6 +1,8 @@
+import { RecordAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
RecordAvatarUpdateMutationInput,
RecordAvatarUpdateMutationOutput,
@@ -12,14 +14,10 @@ export class RecordAvatarUpdateMutationHandler
async handleMutation(
input: RecordAvatarUpdateMutationInput
): Promise<RecordAvatarUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<RecordAttributes>(
input.recordId,
input.userId,
(attributes) => {
- if (attributes.type !== 'record') {
- throw new MutationError('invalid_attributes', 'Invalid node type');
- }
-
attributes.avatar = input.avatar;
return attributes;
}
@@ -27,7 +25,7 @@ export class RecordAvatarUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.RecordUpdateForbidden,
"You don't have permission to update this record."
);
}
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 a1fe123e..da2f61a3 100644
--- a/apps/desktop/src/main/mutations/records/record-content-update.ts
+++ b/apps/desktop/src/main/mutations/records/record-content-update.ts
@@ -1,4 +1,4 @@
-import { Block } from '@colanode/core';
+import { Block, RecordAttributes } from '@colanode/core';
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
@@ -7,7 +7,7 @@ import {
RecordContentUpdateMutationInput,
RecordContentUpdateMutationOutput,
} from '@/shared/mutations/records/record-content-update';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class RecordContentUpdateMutationHandler
implements MutationHandler<RecordContentUpdateMutationInput>
@@ -15,14 +15,10 @@ export class RecordContentUpdateMutationHandler
async handleMutation(
input: RecordContentUpdateMutationInput
): Promise<RecordContentUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<RecordAttributes>(
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)) {
@@ -50,15 +46,15 @@ export class RecordContentUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.RecordUpdateForbidden,
"You don't have permission to update this record."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
- 'Something went wrong while updating the record content.'
+ MutationErrorCode.RecordUpdateFailed,
+ 'Something went wrong while updating the record content. Please try again later.'
);
}
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 e3f1a734..2f6eaf51 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,6 +1,8 @@
+import { RecordAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
RecordFieldValueDeleteMutationInput,
RecordFieldValueDeleteMutationOutput,
@@ -12,14 +14,10 @@ export class RecordFieldValueDeleteMutationHandler
async handleMutation(
input: RecordFieldValueDeleteMutationInput
): Promise<RecordFieldValueDeleteMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<RecordAttributes>(
input.recordId,
input.userId,
(attributes) => {
- if (attributes.type !== 'record') {
- throw new MutationError('invalid_attributes', 'Invalid node type');
- }
-
delete attributes.fields[input.fieldId];
return attributes;
}
@@ -27,15 +25,15 @@ export class RecordFieldValueDeleteMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.RecordUpdateForbidden,
"You don't have permission to delete this field value."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
- 'Something went wrong while deleting the field value.'
+ MutationErrorCode.RecordUpdateFailed,
+ 'Something went wrong while deleting the field value. Please try again later.'
);
}
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 21764ad4..563a3cc0 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,6 +1,8 @@
+import { RecordAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
RecordFieldValueSetMutationInput,
RecordFieldValueSetMutationOutput,
@@ -12,14 +14,10 @@ export class RecordFieldValueSetMutationHandler
async handleMutation(
input: RecordFieldValueSetMutationInput
): Promise<RecordFieldValueSetMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<RecordAttributes>(
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;
}
@@ -27,14 +25,14 @@ export class RecordFieldValueSetMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.RecordUpdateForbidden,
"You don't have permission to set this field value."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
+ MutationErrorCode.RecordUpdateFailed,
'Something went wrong while setting the field value.'
);
}
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 8f33499a..1cc0c428 100644
--- a/apps/desktop/src/main/mutations/records/record-name-update.ts
+++ b/apps/desktop/src/main/mutations/records/record-name-update.ts
@@ -1,6 +1,8 @@
+import { RecordAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
RecordNameUpdateMutationInput,
RecordNameUpdateMutationOutput,
@@ -12,14 +14,10 @@ export class RecordNameUpdateMutationHandler
async handleMutation(
input: RecordNameUpdateMutationInput
): Promise<RecordNameUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<RecordAttributes>(
input.recordId,
input.userId,
(attributes) => {
- if (attributes.type !== 'record') {
- throw new MutationError('invalid_attributes', 'Invalid node type');
- }
-
attributes.name = input.name;
return attributes;
}
@@ -27,15 +25,15 @@ export class RecordNameUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.RecordUpdateForbidden,
"You don't have permission to update this record."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
- 'Something went wrong while updating the record name.'
+ MutationErrorCode.RecordUpdateFailed,
+ 'Something went wrong while updating the record name. Please try again later.'
);
}
diff --git a/apps/desktop/src/main/mutations/servers/server-create.ts b/apps/desktop/src/main/mutations/servers/server-create.ts
index 350a31e7..1018c6a1 100644
--- a/apps/desktop/src/main/mutations/servers/server-create.ts
+++ b/apps/desktop/src/main/mutations/servers/server-create.ts
@@ -1,7 +1,7 @@
import { databaseService } from '@/main/data/database-service';
import { serverService } from '@/main/services/server-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
ServerCreateMutationInput,
ServerCreateMutationOutput,
@@ -13,23 +13,24 @@ export class ServerCreateMutationHandler
async handleMutation(
input: ServerCreateMutationInput
): Promise<ServerCreateMutationOutput> {
+ const domain = parseDomain(input.domain);
const existingServer = await databaseService.appDatabase
.selectFrom('servers')
.selectAll()
- .where('domain', '=', input.domain)
+ .where('domain', '=', domain)
.executeTakeFirst();
if (existingServer) {
throw new MutationError(
- 'server_already_exists',
+ MutationErrorCode.ServerAlreadyExists,
'A server with this domain already exists.'
);
}
- const server = await serverService.createServer(input.domain);
+ const server = await serverService.createServer(domain);
if (server === null) {
throw new MutationError(
- 'invalid_server_domain',
+ MutationErrorCode.ServerInitFailed,
'Could not fetch server configuration. Please make sure the domain is correct.'
);
}
@@ -39,3 +40,17 @@ export class ServerCreateMutationHandler
};
}
}
+
+const parseDomain = (domain: string): string => {
+ try {
+ // Try to parse as URL first
+ const url = new URL(domain.toLowerCase());
+ return url.host; // host includes domain + port if present
+ } catch {
+ // If not a valid URL, treat as domain directly
+ throw new MutationError(
+ MutationErrorCode.ServerDomainInvalid,
+ 'The provided domain is not valid. Please make sure it is a valid server domain.'
+ );
+ }
+};
diff --git a/apps/desktop/src/main/mutations/spaces/space-create.ts b/apps/desktop/src/main/mutations/spaces/space-create.ts
index dd70ef7a..51868ddf 100644
--- a/apps/desktop/src/main/mutations/spaces/space-create.ts
+++ b/apps/desktop/src/main/mutations/spaces/space-create.ts
@@ -13,7 +13,7 @@ import {
SpaceCreateMutationInput,
SpaceCreateMutationOutput,
} from '@/shared/mutations/spaces/space-create';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
export class SpaceCreateMutationHandler
implements MutationHandler<SpaceCreateMutationInput>
@@ -28,12 +28,15 @@ export class SpaceCreateMutationHandler
.executeTakeFirst();
if (!workspace) {
- throw new MutationError('workspace_not_found', 'Workspace not found');
+ throw new MutationError(
+ MutationErrorCode.WorkspaceNotFound,
+ 'Workspace was not found or has been deleted.'
+ );
}
if (workspace.role === 'guest' || workspace.role === 'none') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.SpaceCreateForbidden,
"You don't have permission to create spaces in this workspace."
);
}
diff --git a/apps/desktop/src/main/mutations/spaces/space-update.ts b/apps/desktop/src/main/mutations/spaces/space-update.ts
index 2d622f22..ed62bf07 100644
--- a/apps/desktop/src/main/mutations/spaces/space-update.ts
+++ b/apps/desktop/src/main/mutations/spaces/space-update.ts
@@ -1,6 +1,8 @@
+import { SpaceAttributes } from '@colanode/core';
+
import { entryService } from '@/main/services/entry-service';
import { MutationHandler } from '@/main/types';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
SpaceUpdateMutationInput,
SpaceUpdateMutationOutput,
@@ -12,14 +14,10 @@ export class SpaceUpdateMutationHandler
async handleMutation(
input: SpaceUpdateMutationInput
): Promise<SpaceUpdateMutationOutput> {
- const result = await entryService.updateEntry(
+ const result = await entryService.updateEntry<SpaceAttributes>(
input.id,
input.userId,
(attributes) => {
- if (attributes.type !== 'space') {
- throw new MutationError('invalid_attributes', 'Entry is not a space');
- }
-
attributes.name = input.name;
attributes.description = input.description;
attributes.avatar = input.avatar;
@@ -30,15 +28,15 @@ export class SpaceUpdateMutationHandler
if (result === 'unauthorized') {
throw new MutationError(
- 'unauthorized',
+ MutationErrorCode.SpaceUpdateForbidden,
"You don't have permission to update this space."
);
}
if (result !== 'success') {
throw new MutationError(
- 'unknown',
- 'Something went wrong while updating the space.'
+ MutationErrorCode.SpaceUpdateFailed,
+ 'Something went wrong while updating the space. Please try again later.'
);
}
diff --git a/apps/desktop/src/main/mutations/users/user-role-update.ts b/apps/desktop/src/main/mutations/users/user-role-update.ts
index d39b8e1b..5d2febd0 100644
--- a/apps/desktop/src/main/mutations/users/user-role-update.ts
+++ b/apps/desktop/src/main/mutations/users/user-role-update.ts
@@ -7,7 +7,7 @@ import {
UserRoleUpdateMutationInput,
UserRoleUpdateMutationOutput,
} from '@/shared/mutations/workspaces/workspace-user-role-update';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import { parseApiError } from '@/shared/lib/axios';
export class UserRoleUpdateMutationHandler
@@ -23,7 +23,10 @@ export class UserRoleUpdateMutationHandler
.executeTakeFirst();
if (!workspace) {
- throw new MutationError('workspace_not_found', 'Workspace not found');
+ throw new MutationError(
+ MutationErrorCode.WorkspaceNotFound,
+ 'Workspace was not found or has been deleted.'
+ );
}
const account = await databaseService.appDatabase
@@ -34,8 +37,8 @@ export class UserRoleUpdateMutationHandler
if (!account) {
throw new MutationError(
- 'account_not_found',
- 'The account associated with this workspace was not found.'
+ MutationErrorCode.AccountNotFound,
+ 'The account associated with this workspace was not found or has been logged out.'
);
}
@@ -47,7 +50,7 @@ export class UserRoleUpdateMutationHandler
if (!server) {
throw new MutationError(
- 'server_not_found',
+ MutationErrorCode.ServerNotFound,
'The server associated with this account was not found.'
);
}
@@ -69,7 +72,7 @@ export class UserRoleUpdateMutationHandler
};
} catch (error) {
const apiError = parseApiError(error);
- throw new MutationError('api_error', apiError.message);
+ throw new MutationError(MutationErrorCode.ApiError, apiError.message);
}
}
}
diff --git a/apps/desktop/src/main/mutations/users/users-invite.ts b/apps/desktop/src/main/mutations/users/users-invite.ts
index e8184971..11e2c004 100644
--- a/apps/desktop/src/main/mutations/users/users-invite.ts
+++ b/apps/desktop/src/main/mutations/users/users-invite.ts
@@ -7,7 +7,7 @@ import {
UsersInviteMutationInput,
UsersInviteMutationOutput,
} from '@/shared/mutations/workspaces/workspace-users-invite';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import { parseApiError } from '@/shared/lib/axios';
export class UsersInviteMutationHandler
@@ -23,7 +23,10 @@ export class UsersInviteMutationHandler
.executeTakeFirst();
if (!workspace) {
- throw new MutationError('workspace_not_found', 'Workspace not found');
+ throw new MutationError(
+ MutationErrorCode.WorkspaceNotFound,
+ 'Workspace was not found or has been deleted.'
+ );
}
const account = await databaseService.appDatabase
@@ -34,8 +37,8 @@ export class UsersInviteMutationHandler
if (!account) {
throw new MutationError(
- 'account_not_found',
- 'The account associated with this workspace was not found.'
+ MutationErrorCode.AccountNotFound,
+ 'The account associated with this workspace was not found or has been logged out.'
);
}
@@ -47,7 +50,7 @@ export class UsersInviteMutationHandler
if (!server) {
throw new MutationError(
- 'server_not_found',
+ MutationErrorCode.ServerNotFound,
'The server associated with this account was not found.'
);
}
@@ -70,7 +73,7 @@ export class UsersInviteMutationHandler
};
} catch (error) {
const apiError = parseApiError(error);
- throw new MutationError('api_error', apiError.message);
+ throw new MutationError(MutationErrorCode.ApiError, apiError.message);
}
}
}
diff --git a/apps/desktop/src/main/mutations/workspaces/workspace-create.ts b/apps/desktop/src/main/mutations/workspaces/workspace-create.ts
index bdc4d674..56bf4960 100644
--- a/apps/desktop/src/main/mutations/workspaces/workspace-create.ts
+++ b/apps/desktop/src/main/mutations/workspaces/workspace-create.ts
@@ -8,7 +8,7 @@ import {
WorkspaceCreateMutationInput,
WorkspaceCreateMutationOutput,
} from '@/shared/mutations/workspaces/workspace-create';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import { parseApiError } from '@/shared/lib/axios';
export class WorkspaceCreateMutationHandler
@@ -24,7 +24,10 @@ export class WorkspaceCreateMutationHandler
.executeTakeFirst();
if (!account) {
- throw new MutationError('account_not_found', 'Account not found!');
+ throw new MutationError(
+ MutationErrorCode.AccountNotFound,
+ 'Account not found or has been logged out.'
+ );
}
const server = await databaseService.appDatabase
@@ -35,7 +38,7 @@ export class WorkspaceCreateMutationHandler
if (!server) {
throw new MutationError(
- 'server_not_found',
+ MutationErrorCode.ServerNotFound,
'The server associated with this account was not found.'
);
}
@@ -71,7 +74,10 @@ export class WorkspaceCreateMutationHandler
.executeTakeFirst();
if (!createdWorkspace) {
- throw new MutationError('unknown', 'Failed to create workspace!');
+ throw new MutationError(
+ MutationErrorCode.WorkspaceNotCreated,
+ 'Something went wrong updating the workspace. Please try again later.'
+ );
}
eventBus.publish({
@@ -94,7 +100,7 @@ export class WorkspaceCreateMutationHandler
};
} catch (error) {
const apiError = parseApiError(error);
- throw new MutationError('api_error', apiError.message);
+ throw new MutationError(MutationErrorCode.ApiError, apiError.message);
}
}
}
diff --git a/apps/desktop/src/main/mutations/workspaces/workspace-update.ts b/apps/desktop/src/main/mutations/workspaces/workspace-update.ts
index 317545e6..961cef2e 100644
--- a/apps/desktop/src/main/mutations/workspaces/workspace-update.ts
+++ b/apps/desktop/src/main/mutations/workspaces/workspace-update.ts
@@ -3,7 +3,7 @@ import { MutationHandler } from '@/main/types';
import { parseApiError } from '@/shared/lib/axios';
import { eventBus } from '@/shared/lib/event-bus';
import { httpClient } from '@/shared/lib/http-client';
-import { MutationError } from '@/shared/mutations';
+import { MutationError, MutationErrorCode } from '@/shared/mutations';
import {
WorkspaceUpdateMutationInput,
WorkspaceUpdateMutationOutput,
@@ -23,7 +23,10 @@ export class WorkspaceUpdateMutationHandler
.executeTakeFirst();
if (!account) {
- throw new MutationError('account_not_found', 'Account not found!');
+ throw new MutationError(
+ MutationErrorCode.AccountNotFound,
+ 'Account not found or has been logged out.'
+ );
}
const server = await databaseService.appDatabase
@@ -34,7 +37,7 @@ export class WorkspaceUpdateMutationHandler
if (!server) {
throw new MutationError(
- 'server_not_found',
+ MutationErrorCode.ServerNotFound,
'The server associated with this account was not found.'
);
}
@@ -72,7 +75,10 @@ export class WorkspaceUpdateMutationHandler
.executeTakeFirst();
if (!updatedWorkspace) {
- throw new MutationError('unknown', 'Failed to update workspace!');
+ throw new MutationError(
+ MutationErrorCode.WorkspaceNotUpdated,
+ 'Something went wrong updating the workspace. Please try again later.'
+ );
}
eventBus.publish({
@@ -92,7 +98,7 @@ export class WorkspaceUpdateMutationHandler
};
} catch (error) {
const apiError = parseApiError(error);
- throw new MutationError('api_error', apiError.message);
+ throw new MutationError(MutationErrorCode.ApiError, apiError.message);
}
}
}
diff --git a/apps/desktop/src/main/services/entry-service.ts b/apps/desktop/src/main/services/entry-service.ts
index a05845e2..cba2dca9 100644
--- a/apps/desktop/src/main/services/entry-service.ts
+++ b/apps/desktop/src/main/services/entry-service.ts
@@ -196,10 +196,10 @@ class EntryService {
});
}
- public async updateEntry(
+ public async updateEntry<T extends EntryAttributes>(
entryId: string,
userId: string,
- updater: (attributes: EntryAttributes) => EntryAttributes
+ updater: (attributes: T) => T
): Promise<UpdateEntryResult> {
for (let count = 0; count < UPDATE_RETRIES_LIMIT; count++) {
const result = await this.tryUpdateEntry(entryId, userId, updater);
@@ -211,10 +211,10 @@ class EntryService {
return 'failed';
}
- private async tryUpdateEntry(
+ private async tryUpdateEntry<T extends EntryAttributes>(
entryId: string,
userId: string,
- updater: (attributes: EntryAttributes) => EntryAttributes
+ updater: (attributes: T) => T
): Promise<UpdateEntryResult | null> {
this.debug(`Updating entry ${entryId}`);
@@ -239,7 +239,7 @@ class EntryService {
const entry = mapEntry(entryRow);
const transactionId = generateId(IdType.Transaction);
const updatedAt = new Date().toISOString();
- const updatedAttributes = updater(entry.attributes);
+ const updatedAttributes = updater(entry.attributes as T);
const ydoc = new YDoc();
const previousTransactions = await workspaceDatabase
diff --git a/apps/desktop/src/main/services/mutation-service.ts b/apps/desktop/src/main/services/mutation-service.ts
index dc98af1c..86bac2b7 100644
--- a/apps/desktop/src/main/services/mutation-service.ts
+++ b/apps/desktop/src/main/services/mutation-service.ts
@@ -3,6 +3,7 @@ import { mutationHandlerMap } from '@/main/mutations';
import { MutationHandler } from '@/main/types';
import {
MutationError,
+ MutationErrorCode,
MutationInput,
MutationResult,
} from '@/shared/mutations';
@@ -41,7 +42,7 @@ class MutationService {
return {
success: false,
error: {
- code: 'unknown',
+ code: MutationErrorCode.Unknown,
message: 'Something went wrong trying to execute the mutation.',
},
};
diff --git a/apps/desktop/src/renderer/hooks/use-mutation.tsx b/apps/desktop/src/renderer/hooks/use-mutation.tsx
index ed59faf5..c2c38f57 100644
--- a/apps/desktop/src/renderer/hooks/use-mutation.tsx
+++ b/apps/desktop/src/renderer/hooks/use-mutation.tsx
@@ -2,6 +2,7 @@ import React from 'react';
import {
MutationError,
+ MutationErrorCode,
MutationErrorData,
MutationInput,
MutationMap,
@@ -32,7 +33,7 @@ export const useMutation = () => {
options.onError?.(error);
} else {
options.onError?.({
- code: 'unknown',
+ code: MutationErrorCode.Unknown,
message: 'Something went wrong trying to execute the mutation.',
});
}
diff --git a/apps/desktop/src/shared/mutations/index.ts b/apps/desktop/src/shared/mutations/index.ts
index ffa89e56..bcf61e04 100644
--- a/apps/desktop/src/shared/mutations/index.ts
+++ b/apps/desktop/src/shared/mutations/index.ts
@@ -31,28 +31,78 @@ export class MutationError extends Error {
}
}
-export type MutationErrorCode =
- | 'unknown'
- | 'account_not_found'
- | 'account_login_failed'
- | 'account_register_failed'
- | 'user_not_found'
- | 'entry_not_found'
- | 'server_not_found'
- | 'invalid_attributes'
- | 'space_not_found'
- | 'channel_not_found'
- | 'unauthorized'
- | 'invalid_file'
- | 'file_not_found'
- | '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'
- | 'relation_database_not_found'
- | 'api_error';
+export enum MutationErrorCode {
+ Unknown = 'unknown',
+ ApiError = 'api_error',
+ AccountNotFound = 'account_not_found',
+ AccountLoginFailed = 'account_login_failed',
+ AccountRegisterFailed = 'account_register_failed',
+ ServerNotFound = 'server_not_found',
+ WorkspaceNotFound = 'workspace_not_found',
+ WorkspaceNotCreated = 'workspace_not_created',
+ WorkspaceNotUpdated = 'workspace_not_updated',
+ SpaceNotFound = 'space_not_found',
+ SpaceUpdateForbidden = 'space_update_forbidden',
+ SpaceUpdateFailed = 'space_update_failed',
+ SpaceCreateForbidden = 'space_create_forbidden',
+ SpaceCreateFailed = 'space_create_failed',
+ ServerAlreadyExists = 'server_already_exists',
+ ServerDomainInvalid = 'server_domain_invalid',
+ ServerInitFailed = 'server_init_failed',
+ ChannelNotFound = 'channel_not_found',
+ ChannelUpdateForbidden = 'channel_update_forbidden',
+ ChannelUpdateFailed = 'channel_update_failed',
+ DatabaseNotFound = 'database_not_found',
+ DatabaseUpdateForbidden = 'database_update_forbidden',
+ DatabaseUpdateFailed = 'database_update_failed',
+ RelationDatabaseNotFound = 'relation_database_not_found',
+ FieldNotFound = 'field_not_found',
+ FileInvalid = 'file_invalid',
+ FieldCreateForbidden = 'field_create_forbidden',
+ FieldCreateFailed = 'field_create_failed',
+ FieldUpdateForbidden = 'field_update_forbidden',
+ FieldUpdateFailed = 'field_update_failed',
+ FieldDeleteForbidden = 'field_delete_forbidden',
+ FieldDeleteFailed = 'field_delete_failed',
+ FieldTypeInvalid = 'field_type_invalid',
+ SelectOptionCreateForbidden = 'select_option_create_forbidden',
+ SelectOptionCreateFailed = 'select_option_create_failed',
+ SelectOptionNotFound = 'select_option_not_found',
+ SelectOptionUpdateForbidden = 'select_option_update_forbidden',
+ SelectOptionUpdateFailed = 'select_option_update_failed',
+ SelectOptionDeleteForbidden = 'select_option_delete_forbidden',
+ SelectOptionDeleteFailed = 'select_option_delete_failed',
+ ViewNotFound = 'view_not_found',
+ ViewCreateForbidden = 'view_create_forbidden',
+ ViewCreateFailed = 'view_create_failed',
+ ViewUpdateForbidden = 'view_update_forbidden',
+ ViewUpdateFailed = 'view_update_failed',
+ ViewDeleteForbidden = 'view_delete_forbidden',
+ ViewDeleteFailed = 'view_delete_failed',
+ RecordUpdateForbidden = 'record_update_forbidden',
+ RecordUpdateFailed = 'record_update_failed',
+ PageUpdateForbidden = 'page_update_forbidden',
+ PageUpdateFailed = 'page_update_failed',
+ EntryCollaboratorCreateForbidden = 'entry_collaborator_create_forbidden',
+ EntryCollaboratorCreateFailed = 'entry_collaborator_create_failed',
+ EntryCollaboratorDeleteForbidden = 'entry_collaborator_delete_forbidden',
+ EntryCollaboratorDeleteFailed = 'entry_collaborator_delete_failed',
+ EntryCollaboratorUpdateForbidden = 'entry_collaborator_update_forbidden',
+ EntryCollaboratorUpdateFailed = 'entry_collaborator_update_failed',
+ UserNotFound = 'user_not_found',
+ EntryNotFound = 'entry_not_found',
+ RootNotFound = 'root_not_found',
+ FileCreateForbidden = 'file_create_forbidden',
+ FileCreateFailed = 'file_create_failed',
+ FileNotFound = 'file_not_found',
+ FileDeleteForbidden = 'file_delete_forbidden',
+ FileDeleteFailed = 'file_delete_failed',
+ FolderUpdateForbidden = 'folder_update_forbidden',
+ FolderUpdateFailed = 'folder_update_failed',
+ MessageCreateForbidden = 'message_create_forbidden',
+ MessageCreateFailed = 'message_create_failed',
+ MessageDeleteForbidden = 'message_delete_forbidden',
+ MessageDeleteFailed = 'message_delete_failed',
+ MessageNotFound = 'message_not_found',
+ MessageReactionCreateForbidden = 'message_reaction_create_forbidden',
+}