Skip to content

Commit cde2c06

Browse files
zzj3720pull[bot]
authored andcommitted
refactor(database): replace Error with BlockSuiteError (#7732)
1 parent 960a0c8 commit cde2c06

File tree

7 files changed

+54
-15
lines changed

7 files changed

+54
-15
lines changed

packages/blocks/src/database-block/data-view/common/ast.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
2+
13
import type { TType } from '../logical/typesystem.js';
24
import type { UniComponent } from '../utils/uni-component/uni-component.js';
35

@@ -63,7 +65,10 @@ export const getRefType = (vars: Variable[], ref: VariableOrProperty) => {
6365
export const firstFilterName = (vars: Variable[], ref: VariableOrProperty) => {
6466
const type = getRefType(vars, ref);
6567
if (!type) {
66-
throw new Error(`can't resolve ref type`);
68+
throw new BlockSuiteError(
69+
ErrorCode.DatabaseBlockError,
70+
`can't resolve ref type`
71+
);
6772
}
6873
return filterMatcher.match(type)?.name;
6974
};
@@ -87,7 +92,10 @@ export const firstFilter = (vars: Variable[]): SingleFilter => {
8792
};
8893
const filter = firstFilterName(vars, ref);
8994
if (!filter) {
90-
throw new Error(`can't match any filter`);
95+
throw new BlockSuiteError(
96+
ErrorCode.DatabaseBlockError,
97+
`can't match any filter`
98+
);
9199
}
92100
return {
93101
type: 'filter',

packages/blocks/src/database-block/data-view/logical/typesystem.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
2+
13
export interface TUnion {
24
type: 'union';
35
title: 'union';
@@ -131,7 +133,10 @@ export class DataDefine<Data extends DataTypeShape = Record<string, unknown>> {
131133
isSuperOf(subType: TDataType): boolean {
132134
const dataDefine = this.dataMap.get(subType.name);
133135
if (!dataDefine) {
134-
throw new Error('bug');
136+
throw new BlockSuiteError(
137+
ErrorCode.DatabaseBlockError,
138+
'data config not found'
139+
);
135140
}
136141
return dataDefine.isSubOfByName(this.config.name);
137142
}
@@ -235,7 +240,10 @@ export class Typesystem {
235240
if (this.isDataType(sub)) {
236241
const dataDefine = this.dataMap.get(sub.name);
237242
if (!dataDefine) {
238-
throw new Error('bug');
243+
throw new BlockSuiteError(
244+
ErrorCode.DatabaseBlockError,
245+
'data config not found'
246+
);
239247
}
240248
if (!this.isDataType(superType)) {
241249
return false;
@@ -265,7 +273,10 @@ export class Typesystem {
265273
case 'array':
266274
return tArray(subst(type.ele));
267275
case 'function':
268-
throw new Error('TODO');
276+
throw new BlockSuiteError(
277+
ErrorCode.DatabaseBlockError,
278+
'not implement yet'
279+
);
269280
}
270281
};
271282
const result = tFunction({

packages/blocks/src/database-block/data-view/view/data-view.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import type {
66
import type { Disposable, Slot } from '@blocksuite/global/utils';
77
import type { Doc } from '@blocksuite/store';
88

9+
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
10+
911
import type { DataSource } from '../common/data-source/base.js';
1012
import type { ViewSource } from '../common/index.js';
1113
import type { DataViewRenderer } from '../data-view.js';
1214
import type { DataViewSelection, InsertToPosition } from '../types.js';
1315
import type { UniComponent } from '../utils/uni-component/index.js';
1416
import type { DataViewWidget } from '../widget/types.js';
15-
import type { DataViewManagerBase } from './data-view-manager.js';
16-
import type { DataViewManager } from './data-view-manager.js';
17+
import type {
18+
DataViewManager,
19+
DataViewManagerBase,
20+
} from './data-view-manager.js';
1721

1822
export interface DataViewProps<
1923
T extends DataViewManager = DataViewManager,
@@ -123,7 +127,10 @@ export class ViewRendererManager {
123127
getView(type: string): DataViewRendererConfig {
124128
const view = this.map.get(type);
125129
if (!view) {
126-
throw new Error(`${type} is not exist`);
130+
throw new BlockSuiteError(
131+
ErrorCode.DatabaseBlockError,
132+
`${type} is not exist`
133+
);
127134
}
128135
return view;
129136
}

packages/blocks/src/database-block/data-view/view/presets/kanban/controller/selection.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ReactiveController } from 'lit';
22

3+
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
34
import { assertExists } from '@blocksuite/global/utils';
45

56
import type { KanbanGroup } from '../group.js';
@@ -307,8 +308,8 @@ export class KanbanSelectionController implements ReactiveController {
307308
groupIndex => (groupIndex === 0 ? groups.length - 1 : groupIndex - 1)
308309
);
309310
}
310-
311-
throw new Error(
311+
throw new BlockSuiteError(
312+
ErrorCode.DatabaseBlockError,
312313
'Unknown arrow keys, only support: up, down, left, and right keys.'
313314
);
314315
}
@@ -393,8 +394,8 @@ export class KanbanSelectionController implements ReactiveController {
393394
groupIndex => (groupIndex === 0 ? groups.length - 1 : groupIndex - 1)
394395
);
395396
}
396-
397-
throw new Error(
397+
throw new BlockSuiteError(
398+
ErrorCode.DatabaseBlockError,
398399
'Unknown arrow keys, only support: up, down, left, and right keys.'
399400
);
400401
}

packages/blocks/src/database-block/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
12
import { nanoid } from '@blocksuite/store';
23

34
import type {
@@ -72,7 +73,10 @@ const initMap: Record<
7273
};
7374
const column = allowList.sort((a, b) => getWeight(b) - getWeight(a))[0];
7475
if (!column) {
75-
throw new Error('not implement yet');
76+
throw new BlockSuiteError(
77+
ErrorCode.DatabaseBlockError,
78+
'not implement yet'
79+
);
7680
}
7781
return {
7882
id,

packages/blocks/src/database-block/view-source.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
12
import { Slot } from '@blocksuite/global/utils';
23

34
import type { SingleViewSource, ViewSource } from './data-view/common/index.js';
@@ -61,7 +62,10 @@ export class DatabaseBlockViewSource implements ViewSource {
6162
};
6263
const view = getView();
6364
if (!view) {
64-
throw new Error('view not found');
65+
throw new BlockSuiteError(
66+
ErrorCode.DatabaseBlockError,
67+
'view not found'
68+
);
6569
}
6670
// eslint-disable-next-line @typescript-eslint/no-this-alias
6771
const self = this;
@@ -81,7 +85,10 @@ export class DatabaseBlockViewSource implements ViewSource {
8185
get view() {
8286
const view = getView();
8387
if (!view) {
84-
throw new Error('view not found');
88+
throw new BlockSuiteError(
89+
ErrorCode.DatabaseBlockError,
90+
'view not found'
91+
);
8592
}
8693
return view;
8794
},

packages/framework/global/src/exceptions/code.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export enum ErrorCode {
1717
SelectionError,
1818
GfxBlockElementError,
1919
MissingViewModelError,
20+
DatabaseBlockError,
2021

2122
// Fatal error should be greater than 10000
2223
DefaultFatalError = 10000,

0 commit comments

Comments
 (0)