Skip to content

Commit

Permalink
fix: use blocksuite error in framework (#7709)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone authored Jul 22, 2024
1 parent 13e6475 commit 1776f48
Show file tree
Hide file tree
Showing 21 changed files with 233 additions and 73 deletions.
7 changes: 6 additions & 1 deletion packages/framework/block-std/src/command/manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';

import type {
Chain,
Command,
Expand Down Expand Up @@ -181,7 +183,10 @@ export class CommandManager {
const cmdFunc = this._commands.get(command);

if (!cmdFunc) {
throw new Error(`The command "${command}" not found`);
throw new BlockSuiteError(
ErrorCode.CommandError,
`The command "${command}" not found`
);
}

const inData = args[0];
Expand Down
6 changes: 5 additions & 1 deletion packages/framework/block-std/src/edgeless/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
SerializedXYWH,
} from '@blocksuite/global/utils';

import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import {
Bound,
PointLocation,
Expand Down Expand Up @@ -202,7 +203,10 @@ export function selectable<
}

if (Object.getPrototypeOf(currentClass.prototype) === null) {
throw new Error('The SuperClass is not a subclass of BlockModel');
throw new BlockSuiteError(
ErrorCode.EdgelessBlockError,
'The SuperClass is not a subclass of BlockModel'
);
}

Object.setPrototypeOf(currentClass.prototype, EdgelessBlockModel.prototype);
Expand Down
6 changes: 5 additions & 1 deletion packages/framework/block-std/src/event/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BlockModel } from '@blocksuite/store';

import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { DisposableGroup, Slot } from '@blocksuite/global/utils';

import type { BlockElement } from '../view/index.js';
Expand Down Expand Up @@ -259,7 +260,10 @@ export class UIEventDispatcher {
break;
}
default: {
throw new Error(`Unknown event scope source: ${state.sourceType}`);
throw new BlockSuiteError(
ErrorCode.EventDispatcherError,
`Unknown event scope source: ${state.sourceType}`
);
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/framework/block-std/src/event/keymap.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { base, keyName } from 'w3c-keyname';

import type { UIEventHandler } from './base.js';
Expand Down Expand Up @@ -40,7 +41,10 @@ function normalizeKeyName(name: string) {
return;
}

throw new Error('Unrecognized modifier name: ' + mod);
throw new BlockSuiteError(
ErrorCode.EventDispatcherError,
'Unrecognized modifier name: ' + mod
);
});
if (alt) result = 'Alt-' + result;
if (ctrl) result = 'Ctrl-' + result;
Expand Down
7 changes: 6 additions & 1 deletion packages/framework/block-std/src/selection/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';

type SelectionConstructor<T = unknown> = {
type: string;
group: string;
Expand All @@ -20,7 +22,10 @@ export abstract class BaseSelection {
}

static fromJSON(_: Record<string, unknown>): BaseSelection {
throw new Error('You must override this method');
throw new BlockSuiteError(
ErrorCode.SelectionError,
'You must override this method'
);
}

is<T extends BlockSuite.SelectionType>(
Expand Down
11 changes: 9 additions & 2 deletions packages/framework/block-std/src/selection/manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { StackItem } from '@blocksuite/store';

import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { DisposableGroup, Slot } from '@blocksuite/global/utils';
import { computed, signal } from '@lit-labs/preact-signals';

Expand Down Expand Up @@ -34,7 +35,10 @@ export class SelectionManager {
private _jsonToSelection = (json: Record<string, unknown>) => {
const ctor = this._selectionConstructors[json.type as string];
if (!ctor) {
throw new Error(`Unknown selection type: ${json.type}`);
throw new BlockSuiteError(
ErrorCode.SelectionError,
`Unknown selection type: ${json.type}`
);
}
return ctor.fromJSON(json);
};
Expand Down Expand Up @@ -130,7 +134,10 @@ export class SelectionManager {
): BlockSuite.SelectionInstance[T] {
const ctor = this._selectionConstructors[type];
if (!ctor) {
throw new Error(`Unknown selection type: ${type}`);
throw new BlockSuiteError(
ErrorCode.SelectionError,
`Unknown selection type: ${type}`
);
}
return new ctor(...args) as BlockSuite.SelectionInstance[T];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SerializedXYWH } from '@blocksuite/global/utils';
import type { BlockModel } from '@blocksuite/store';

import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { nothing } from 'lit';

import type { BlockService } from '../../service/index.js';
Expand Down Expand Up @@ -30,7 +31,10 @@ export abstract class EdgelessBlockElement<
}>;

if (!xywh$) {
throw new Error('Edgeless block should have at least `xywh` property.');
throw new BlockSuiteError(
ErrorCode.EdgelessBlockError,
'Edgeless block should have at least `xywh` property.'
);
}

const [x, y, w, h] = JSON.parse(xywh$.value);
Expand Down Expand Up @@ -104,7 +108,10 @@ export function toEdgelessBlockElement<
}>;

if (!xywh$) {
throw new Error('Edgeless block should have at least `xywh` property.');
throw new BlockSuiteError(
ErrorCode.EdgelessBlockError,
'Edgeless block should have at least `xywh` property.'
);
}

const [x, y, w, h] = JSON.parse(xywh$.value);
Expand All @@ -119,7 +126,8 @@ export function toEdgelessBlockElement<
}>;

if (!xywh || !index) {
throw new Error(
throw new BlockSuiteError(
ErrorCode.EdgelessBlockError,
'Edgeless block should have at least `xywh` and `index` properties.'
);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/framework/global/src/exceptions/code.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export enum ErrorCode {
DefaultRuntimeError = 1,
ReactiveProxyError,
DocCollectionError,
ModelCRUDError,
ValueNotExists,
ValueNotInstanceOf,
ValueNotEqual,
Expand All @@ -9,6 +12,10 @@ export enum ErrorCode {
InlineEditorError,
TransformerNotImplementedError,
EdgelessExportError,
CommandError,
EventDispatcherError,
SelectionError,
EdgelessBlockError,

// Fatal error should be greater than 10000
DefaultFatalError = 10000,
Expand Down
8 changes: 6 additions & 2 deletions packages/framework/inline/src/components/v-element.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { LitElement, html, nothing } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
Expand Down Expand Up @@ -26,10 +27,13 @@ export class VElement<
const isEmbed = inlineEditor.isEmbed(this.delta);
if (isEmbed) {
if (this.delta.insert.length !== 1) {
throw new Error(`The length of embed node should only be 1.
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
`The length of embed node should only be 1.
This seems to be an internal issue with inline editor.
Please go to https://github.com/toeverything/blocksuite/issues
to report it.`);
to report it.`
);
}

return html`<span
Expand Down
8 changes: 6 additions & 2 deletions packages/framework/inline/src/components/v-line.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { assertExists } from '@blocksuite/global/utils';
import { LitElement, type TemplateResult, html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
Expand Down Expand Up @@ -52,10 +53,13 @@ export class VLine extends LitElement {
const renderElements = this.elements.flatMap(([template, delta], index) => {
if (inlineEditor.isEmbed(delta)) {
if (delta.insert.length !== 1) {
throw new Error(`The length of embed node should only be 1.
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
`The length of embed node should only be 1.
This seems to be an internal issue with inline editor.
Please go to https://github.com/toeverything/blocksuite/issues
to report it.`);
to report it.`
);
}
// we add `EmbedGap` to make cursor can be placed between embed elements
if (index === 0) {
Expand Down
17 changes: 13 additions & 4 deletions packages/framework/inline/src/inline-editor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { DisposableGroup, Slot, assertExists } from '@blocksuite/global/utils';
import { nothing, render } from 'lit';
import * as Y from 'yjs';
Expand Down Expand Up @@ -60,7 +61,8 @@ export class InlineEditor<

private _onYTextChange = (_: Y.YTextEvent, transaction: Y.Transaction) => {
if (this.yText.toString().includes('\r')) {
throw new Error(
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
'yText must not contain "\\r" because it will break the range synchronization'
);
}
Expand Down Expand Up @@ -218,11 +220,15 @@ export class InlineEditor<
} = {}
) {
if (!yText.doc) {
throw new Error('yText must be attached to a Y.Doc');
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
'yText must be attached to a Y.Doc'
);
}

if (yText.toString().includes('\r')) {
throw new Error(
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
'yText must not contain "\\r" because it will break the range synchronization'
);
}
Expand Down Expand Up @@ -304,7 +310,10 @@ export class InlineEditor<
transact(fn: () => void): void {
const doc = this.yText.doc;
if (!doc) {
throw new Error('yText is not attached to a doc');
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
'yText is not attached to a doc'
);
}

doc.transact(fn, doc.clientID);
Expand Down
7 changes: 6 additions & 1 deletion packages/framework/inline/src/services/text.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';

import type { InlineEditor } from '../inline-editor.js';
import type { DeltaInsert, InlineRange } from '../types.js';
import type { BaseTextAttributes } from '../utils/base-attributes.js';
Expand Down Expand Up @@ -76,7 +78,10 @@ export class InlineTextService<TextAttributes extends BaseTextAttributes> {
this.editor.attributeService.normalizeAttributes(attributes);

if (!text || !text.length) {
throw new Error('text must not be empty');
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
'text must not be empty'
);
}

this.transact(() => {
Expand Down
15 changes: 12 additions & 3 deletions packages/framework/inline/src/utils/point-conversion.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';

import type { VElement, VLine } from '../components/index.js';
import type { DomPoint, TextPoint } from '../types.js';

Expand Down Expand Up @@ -58,7 +60,8 @@ export function textPointToDomPoint(
rootElement: HTMLElement
): DomPoint | null {
if (rootElement.dataset.vRoot !== 'true') {
throw new Error(
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
'textRangeToDomPoint should be called with editor root element'
);
}
Expand All @@ -80,13 +83,19 @@ export function textPointToDomPoint(

const textParentElement = text.parentElement;
if (!textParentElement) {
throw new Error('text element parent not found');
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
'text element parent not found'
);
}

const lineElement = textParentElement.closest('v-line');

if (!lineElement) {
throw new Error('line element not found');
throw new BlockSuiteError(
ErrorCode.InlineEditorError,
'line element not found'
);
}

const lineIndex = Array.from(rootElement.querySelectorAll('v-line')).indexOf(
Expand Down
6 changes: 5 additions & 1 deletion packages/framework/store/src/adapter/assets.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { assertExists, sha } from '@blocksuite/global/utils';

/**
Expand Down Expand Up @@ -29,7 +30,10 @@ export class MemoryBlobCRUD {
const value = typeof valueOrKey === 'string' ? _value : valueOrKey;

if (!value) {
throw new Error('value is required');
throw new BlockSuiteError(
ErrorCode.TransformerError,
'value is required'
);
}

this._map.set(key, value);
Expand Down
Loading

0 comments on commit 1776f48

Please sign in to comment.