From c6943826254fd7320f47475224ea1b86b1143e80 Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 3 Aug 2024 09:28:16 +0900 Subject: [PATCH] Refactor Error Code Names for Clarity --- src/document/crdt/tree.ts | 2 +- src/document/document.ts | 8 ++--- src/document/json/counter.ts | 5 +-- src/document/json/text.ts | 20 +++++------ src/document/json/tree.ts | 42 +++++++++++----------- src/document/operation/operation.ts | 5 +-- src/util/error.ts | 13 ++++--- src/util/index_tree.ts | 55 ++++++----------------------- src/util/observable.ts | 2 +- 9 files changed, 60 insertions(+), 92 deletions(-) diff --git a/src/document/crdt/tree.ts b/src/document/crdt/tree.ts index b3de50240..7c5800205 100644 --- a/src/document/crdt/tree.ts +++ b/src/document/crdt/tree.ts @@ -225,7 +225,7 @@ export class CRDTTreePos { let leftNode = tree.findFloorNode(leftSiblingID); if (!parentNode || !leftNode) { throw new YorkieError( - Code.ErrOperationNotPermitted, + Code.ErrRefused, `cannot find node of CRDTTreePos(${parentID.toTestString()}, ${leftSiblingID.toTestString()})`, ); } diff --git a/src/document/document.ts b/src/document/document.ts index ccecfa850..975c1c954 100644 --- a/src/document/document.ts +++ b/src/document/document.ts @@ -1771,14 +1771,14 @@ export class Document { private undo(): void { if (this.isUpdating) { throw new YorkieError( - Code.ErrOperationNotPermitted, + Code.ErrRefused, 'Undo is not allowed during an update', ); } const undoOps = this.internalHistory.popUndo(); if (undoOps === undefined) { throw new YorkieError( - Code.ErrOperationNotPermitted, + Code.ErrRefused, 'There is no operation to be undone', ); } @@ -1871,7 +1871,7 @@ export class Document { private redo(): void { if (this.isUpdating) { throw new YorkieError( - Code.ErrOperationNotPermitted, + Code.ErrRefused, 'Redo is not allowed during an update', ); } @@ -1879,7 +1879,7 @@ export class Document { const redoOps = this.internalHistory.popRedo(); if (redoOps === undefined) { throw new YorkieError( - Code.ErrOperationNotPermitted, + Code.ErrRefused, 'There is no operation to be redone', ); } diff --git a/src/document/json/counter.ts b/src/document/json/counter.ts index f7333de72..358a49b00 100644 --- a/src/document/json/counter.ts +++ b/src/document/json/counter.ts @@ -81,7 +81,7 @@ export class Counter { if (!this.context || !this.counter) { const ErrorMessage = 'Counter is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } const ticket = this.context.issueTimeTicket(); @@ -106,7 +106,8 @@ export class Counter { */ public toJSForTest(): Devtools.JSONElement { if (!this.context || !this.counter) { - throw new YorkieError(Code.ErrOperationNotReady, 'Counter is not ready'); + const ErrorMessage = 'Counter is not initialized yet'; + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } return this.counter.toJSForTest(); diff --git a/src/document/json/text.ts b/src/document/json/text.ts index 783a64a3b..961e2ca6c 100644 --- a/src/document/json/text.ts +++ b/src/document/json/text.ts @@ -95,7 +95,7 @@ export class Text { if (!this.context || !this.text) { const ErrorMessage = 'Text is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } if (fromIdx > toIdx) { @@ -159,7 +159,7 @@ export class Text { if (!this.context || !this.text) { const ErrorMessage = 'Text is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } if (fromIdx > toIdx) { @@ -210,7 +210,7 @@ export class Text { if (!this.context || !this.text) { const ErrorMessage = 'Text is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } const textRange = this.text.indexRangeToPosRange(range[0], range[1]); @@ -224,7 +224,7 @@ export class Text { if (!this.context || !this.text) { const ErrorMessage = 'Text is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } const textRange = this.text.findIndexesFromRange([ @@ -242,7 +242,7 @@ export class Text { if (!this.context || !this.text) { const ErrorMessage = 'Text is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } return this.text.toTestString(); @@ -255,7 +255,7 @@ export class Text { if (!this.context || !this.text) { const ErrorMessage = 'Text is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } return this.text.values(); @@ -292,7 +292,7 @@ export class Text { if (!this.context || !this.text) { const ErrorMessage = 'Text is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } return this.text.toString(); @@ -304,7 +304,7 @@ export class Text { public toJSON(): string { if (!this.context || !this.text) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Text is not initialized yet', ); } @@ -319,7 +319,7 @@ export class Text { public toJSForTest(): Devtools.JSONElement { if (!this.context || !this.text) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Text is not initialized yet', ); } @@ -335,7 +335,7 @@ export class Text { if (!this.context || !this.text) { const ErrorMessage = 'Text is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } return this.text.indexRangeToPosRange(fromIdx, toIdx); diff --git a/src/document/json/tree.ts b/src/document/json/tree.ts index 983da2987..c6125896e 100644 --- a/src/document/json/tree.ts +++ b/src/document/json/tree.ts @@ -258,7 +258,7 @@ export class Tree { public getSize(): number { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -272,7 +272,7 @@ export class Tree { public getNodeSize(): number { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -286,7 +286,7 @@ export class Tree { public getIndexTree(): IndexTree { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -300,7 +300,7 @@ export class Tree { public styleByPath(path: Array, attributes: { [key: string]: any }) { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -343,7 +343,7 @@ export class Tree { ) { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -392,7 +392,7 @@ export class Tree { ) { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -508,7 +508,7 @@ export class Tree { ): boolean { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -547,7 +547,7 @@ export class Tree { ): boolean { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -581,7 +581,7 @@ export class Tree { ): boolean { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -614,7 +614,7 @@ export class Tree { ): boolean { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -637,7 +637,7 @@ export class Tree { public toXML(): string { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -651,7 +651,7 @@ export class Tree { public toJSON(): string { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -666,7 +666,7 @@ export class Tree { public toJSForTest(): Devtools.JSONElement { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -682,7 +682,7 @@ export class Tree { public toJSInfoForTest(): Devtools.TreeNodeInfo { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -696,7 +696,7 @@ export class Tree { public getRootTreeNode() { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -710,7 +710,7 @@ export class Tree { public indexToPath(index: number): Array { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -724,7 +724,7 @@ export class Tree { public pathToIndex(path: Array): number { if (!this.context || !this.tree) { throw new YorkieError( - Code.ErrOperationNotReady, + Code.ErrNotInitialized, 'Tree is not initialized yet', ); } @@ -741,7 +741,7 @@ export class Tree { if (!this.context || !this.tree) { const ErrorMessage = 'Tree is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } const indexRange: [number, number] = [ @@ -759,7 +759,7 @@ export class Tree { if (!this.context || !this.tree) { const ErrorMessage = 'Tree is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } return this.tree.indexRangeToPosStructRange(range); @@ -772,7 +772,7 @@ export class Tree { if (!this.context || !this.tree) { const ErrorMessage = 'Tree is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } const posRange: [CRDTTreePos, CRDTTreePos] = [ @@ -792,7 +792,7 @@ export class Tree { if (!this.context || !this.tree) { const ErrorMessage = 'Tree is not initialized yet'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotReady, ErrorMessage); + throw new YorkieError(Code.ErrNotInitialized, ErrorMessage); } const posRange: [CRDTTreePos, CRDTTreePos] = [ diff --git a/src/document/operation/operation.ts b/src/document/operation/operation.ts index 03e608247..18057b00a 100644 --- a/src/document/operation/operation.ts +++ b/src/document/operation/operation.ts @@ -211,10 +211,7 @@ export abstract class Operation { // it doesn't have an executedAt yet. The executedAt is set when // the operation is executed through undo or redo. if (!this.executedAt) { - throw new YorkieError( - Code.ErrOperationNotReady, - 'executedAt is not set yet', - ); + throw new YorkieError(Code.ErrNotReady, 'executedAt is not set yet'); } return this.executedAt; } diff --git a/src/util/error.ts b/src/util/error.ts index ab8d3ec3f..3abd06324 100644 --- a/src/util/error.ts +++ b/src/util/error.ts @@ -30,7 +30,7 @@ export enum Code { // ErrInvalidType is returned when the type is invalid. ErrInvalidType = 'ErrInvalidType', - // ErrDummy is returned when the error is intentional. + // ErrDummy is used to verify errors for testing purposes. ErrDummy = 'ErrDummy', // ErrDocumentNotAttached is returned when the document is not attached. @@ -48,11 +48,14 @@ export enum Code { // ErrInvalidArgument is returned when the argument is invalid. ErrInvalidArgument = 'ErrInvalidArgument', - // ErrOperationNotReady is returned when another operation needs to be completed first. - ErrOperationNotReady = 'ErrOperationNotReady', + // ErrNotInitialized is returned when required initialization has not been completed. + ErrNotInitialized = 'ErrNotInitialized', - // ErrOperationNotPermitted is returned when the operation is not permitted. - ErrOperationNotPermitted = 'ErrOperationNotPermitted', + // ErrNotReady is returned when execution of following actions is not ready. + ErrNotReady = 'ErrNotReady', + + // ErrRefused is returned when the execution is rejected. + ErrRefused = 'ErrRefused', } /** diff --git a/src/util/index_tree.ts b/src/util/index_tree.ts index 7c020a954..97c960615 100644 --- a/src/util/index_tree.ts +++ b/src/util/index_tree.ts @@ -130,10 +130,7 @@ export abstract class IndexTreeNode> { this._children = children; if (this.isText && this._children.length > 0) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } } @@ -307,10 +304,7 @@ export abstract class IndexTreeNode> { */ append(...newNode: Array): void { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } this._children.push(...newNode); @@ -326,10 +320,7 @@ export abstract class IndexTreeNode> { */ prepend(...newNode: Array): void { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } this._children.unshift(...newNode); @@ -343,10 +334,7 @@ export abstract class IndexTreeNode> { */ insertBefore(newNode: T, referenceNode: T): void { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } const offset = this._children.indexOf(referenceNode); @@ -363,10 +351,7 @@ export abstract class IndexTreeNode> { */ insertAfter(newNode: T, referenceNode: T): void { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } const offset = this._children.indexOf(referenceNode); @@ -383,10 +368,7 @@ export abstract class IndexTreeNode> { */ insertAt(newNode: T, offset: number): void { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } this.insertAtInternal(newNode, offset); @@ -398,10 +380,7 @@ export abstract class IndexTreeNode> { */ removeChild(child: T) { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } const offset = this._children.indexOf(child); @@ -457,10 +436,7 @@ export abstract class IndexTreeNode> { */ insertAfterInternal(newNode: T, referenceNode: T): void { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } const offset = this._children.indexOf(referenceNode); @@ -477,10 +453,7 @@ export abstract class IndexTreeNode> { */ insertAtInternal(newNode: T, offset: number): void { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } this._children.splice(offset, 0, newNode); @@ -493,10 +466,7 @@ export abstract class IndexTreeNode> { */ findOffset(node: T): number { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } if (node.isRemoved) { @@ -520,10 +490,7 @@ export abstract class IndexTreeNode> { */ findBranchOffset(node: T): number { if (this.isText) { - throw new YorkieError( - Code.ErrOperationNotPermitted, - 'Text node cannot have children', - ); + throw new YorkieError(Code.ErrRefused, 'Text node cannot have children'); } let current: T | undefined = node; diff --git a/src/util/observable.ts b/src/util/observable.ts index efe35883c..f20150dea 100644 --- a/src/util/observable.ts +++ b/src/util/observable.ts @@ -115,7 +115,7 @@ class ObserverProxy implements Observer { if (this.finalized) { const ErrorMessage = 'observable is finalized due to previous error'; logger.fatal(ErrorMessage); - throw new YorkieError(Code.ErrOperationNotPermitted, ErrorMessage); + throw new YorkieError(Code.ErrRefused, ErrorMessage); } if (typeof nextOrObserver === 'object') {