Skip to content

Commit

Permalink
feat: add custom err to organize command execute (#2945)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybzky authored Aug 7, 2024
1 parent ca866c1 commit 05ab674
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
22 changes: 22 additions & 0 deletions packages/core/src/common/error.ts
Original file line number Diff line number Diff line change
@@ -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';
}
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/services/command/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit 05ab674

Please sign in to comment.