Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat: add custom err to organize command execute (#2945)

ybzky05ab674841dc
diff --git a/packages/core/src/common/error.ts b/packages/core/src/common/error.ts
new file mode 100644
index 0000000000..30b45bad8d
--- /dev/null
+++ b/packages/core/src/common/error.ts
@@ -0,0 +1,22 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export class CustomCommandExecutionError extends Error {
+ constructor(message: string) {
+ super(message);
+ this.name = 'CustomCommandExecutionError';
+ }
+}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 700b235eb9..33d6dfeafd 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -21,6 +21,7 @@ export type { Serializable } from './common/json';
export { DEFAULT_DOCUMENT_SUB_COMPONENT_ID } from './docs/data-model/subdocument';
export { type UnitType, UnitModel, UniverInstanceType } from './common/unit';
export { Registry, RegistryAsMap } from './common/registry';
+export { CustomCommandExecutionError } from './common/error';
export { Univer } from './univer';
export { shallowEqual, isRangesEqual, isUnitRangesEqual } from './common/equal';
export { isNumeric, isSafeNumeric } from './common/number';
diff --git a/packages/core/src/services/command/command.service.ts b/packages/core/src/services/command/command.service.ts
index 72a741cdcf..47d8fba70a 100644
--- a/packages/core/src/services/command/command.service.ts
+++ b/packages/core/src/services/command/command.service.ts
@@ -23,6 +23,7 @@ import { Disposable, DisposableCollection, toDisposable } from '../../shared/lif
import type { IKeyValue } from '../../shared/types';
import { IContextService } from '../context/context.service';
import { ILogService } from '../log/log.service';
+import { CustomCommandExecutionError } from '../../common/error';
export enum CommandType {
/** Command could generate some operations or mutations. */
@@ -309,8 +310,13 @@ export class CommandService extends Disposable implements ICommandService {
}
throw new Error(`[CommandService]: command "${id}" is not registered.`);
} catch (error) {
- this._logService.error(error);
- throw error;
+ if (error instanceof CustomCommandExecutionError) {
+ // If need custom logic, can add it here
+ return false as R;
+ } else {
+ this._logService.error(error);
+ throw error;
+ }
}
}