Skip to content

Commit

Permalink
Refactor Error Code Names for Clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
gwbaik9717 committed Aug 3, 2024
1 parent b4c330d commit c694382
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/document/crdt/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()})`,
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1771,14 +1771,14 @@ export class Document<T, P extends Indexable = Indexable> {
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',
);
}
Expand Down Expand Up @@ -1871,15 +1871,15 @@ export class Document<T, P extends Indexable = Indexable> {
private redo(): void {
if (this.isUpdating) {
throw new YorkieError(
Code.ErrOperationNotPermitted,
Code.ErrRefused,
'Redo is not allowed during an update',
);
}

const redoOps = this.internalHistory.popRedo();
if (redoOps === undefined) {
throw new YorkieError(
Code.ErrOperationNotPermitted,
Code.ErrRefused,
'There is no operation to be redone',
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/document/json/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
20 changes: 10 additions & 10 deletions src/document/json/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class Text<A extends Indexable = Indexable> {
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) {
Expand Down Expand Up @@ -159,7 +159,7 @@ export class Text<A extends Indexable = Indexable> {
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) {
Expand Down Expand Up @@ -210,7 +210,7 @@ export class Text<A extends Indexable = Indexable> {
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]);
Expand All @@ -224,7 +224,7 @@ export class Text<A extends Indexable = Indexable> {
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([
Expand All @@ -242,7 +242,7 @@ export class Text<A extends Indexable = Indexable> {
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();
Expand All @@ -255,7 +255,7 @@ export class Text<A extends Indexable = Indexable> {
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();
Expand Down Expand Up @@ -292,7 +292,7 @@ export class Text<A extends Indexable = Indexable> {
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();
Expand All @@ -304,7 +304,7 @@ export class Text<A extends Indexable = Indexable> {
public toJSON(): string {
if (!this.context || !this.text) {
throw new YorkieError(
Code.ErrOperationNotReady,
Code.ErrNotInitialized,
'Text is not initialized yet',
);
}
Expand All @@ -319,7 +319,7 @@ export class Text<A extends Indexable = Indexable> {
public toJSForTest(): Devtools.JSONElement {
if (!this.context || !this.text) {
throw new YorkieError(
Code.ErrOperationNotReady,
Code.ErrNotInitialized,
'Text is not initialized yet',
);
}
Expand All @@ -335,7 +335,7 @@ export class Text<A extends Indexable = Indexable> {
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);
Expand Down
42 changes: 21 additions & 21 deletions src/document/json/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}
Expand All @@ -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',
);
}
Expand All @@ -286,7 +286,7 @@ export class Tree {
public getIndexTree(): IndexTree<CRDTTreeNode> {
if (!this.context || !this.tree) {
throw new YorkieError(
Code.ErrOperationNotReady,
Code.ErrNotInitialized,
'Tree is not initialized yet',
);
}
Expand All @@ -300,7 +300,7 @@ export class Tree {
public styleByPath(path: Array<number>, attributes: { [key: string]: any }) {
if (!this.context || !this.tree) {
throw new YorkieError(
Code.ErrOperationNotReady,
Code.ErrNotInitialized,
'Tree is not initialized yet',
);
}
Expand Down Expand Up @@ -343,7 +343,7 @@ export class Tree {
) {
if (!this.context || !this.tree) {
throw new YorkieError(
Code.ErrOperationNotReady,
Code.ErrNotInitialized,
'Tree is not initialized yet',
);
}
Expand Down Expand Up @@ -392,7 +392,7 @@ export class Tree {
) {
if (!this.context || !this.tree) {
throw new YorkieError(
Code.ErrOperationNotReady,
Code.ErrNotInitialized,
'Tree is not initialized yet',
);
}
Expand Down Expand Up @@ -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',
);
}
Expand Down Expand Up @@ -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',
);
}
Expand Down Expand Up @@ -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',
);
}
Expand Down Expand Up @@ -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',
);
}
Expand All @@ -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',
);
}
Expand All @@ -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',
);
}
Expand All @@ -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',
);
}
Expand All @@ -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',
);
}
Expand All @@ -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',
);
}
Expand All @@ -710,7 +710,7 @@ export class Tree {
public indexToPath(index: number): Array<number> {
if (!this.context || !this.tree) {
throw new YorkieError(
Code.ErrOperationNotReady,
Code.ErrNotInitialized,
'Tree is not initialized yet',
);
}
Expand All @@ -724,7 +724,7 @@ export class Tree {
public pathToIndex(path: Array<number>): number {
if (!this.context || !this.tree) {
throw new YorkieError(
Code.ErrOperationNotReady,
Code.ErrNotInitialized,
'Tree is not initialized yet',
);
}
Expand All @@ -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] = [
Expand All @@ -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);
Expand All @@ -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] = [
Expand All @@ -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] = [
Expand Down
5 changes: 1 addition & 4 deletions src/document/operation/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 8 additions & 5 deletions src/util/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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',
}

/**
Expand Down
Loading

0 comments on commit c694382

Please sign in to comment.