Skip to content

Commit

Permalink
Replace 'throw new Error' to YorkieError
Browse files Browse the repository at this point in the history
This commit replaces all instances of throw new Error with YorkieError to provide more specific and consistent error handling throughout the codebase.
  • Loading branch information
gwbaik9717 committed Jul 28, 2024
1 parent 92dc6af commit 35be776
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 93 deletions.
12 changes: 6 additions & 6 deletions src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function toValueType(valueType: PrimitiveType): PbValueType {
return PbValueType.DATE;
default:
throw new YorkieError(
Code.ErrUnsupported,
Code.ErrInvalidType,
`unsupported type: ${valueType}`,
);
}
Expand All @@ -220,7 +220,7 @@ function toCounterType(valueType: CounterType): PbValueType {
return PbValueType.LONG_CNT;
default:
throw new YorkieError(
Code.ErrUnsupported,
Code.ErrInvalidType,
`unsupported type: ${valueType}`,
);
}
Expand Down Expand Up @@ -865,7 +865,7 @@ function fromPresenceChange<P extends Indexable>(
};
}

throw new YorkieError(Code.ErrUnsupported, `unsupported type: ${type}`);
throw new YorkieError(Code.ErrInvalidType, `unsupported type: ${type}`);
}

/**
Expand Down Expand Up @@ -1482,7 +1482,7 @@ function bytesToSnapshot<P extends Indexable>(
*/
function bytesToObject(bytes?: Uint8Array): CRDTObject {
if (!bytes) {
throw new Error('bytes is empty');
throw new YorkieError(Code.ErrInvalidArgument, 'bytes is empty');
}

const pbElement = PbJSONElement.fromBinary(bytes);
Expand All @@ -1501,7 +1501,7 @@ function objectToBytes(obj: CRDTObject): Uint8Array {
*/
function bytesToArray(bytes?: Uint8Array): CRDTArray {
if (!bytes) {
throw new Error('bytes is empty');
throw new YorkieError(Code.ErrInvalidArgument, 'bytes is empty');
}

const pbElement = PbJSONElement.fromBinary(bytes);
Expand All @@ -1520,7 +1520,7 @@ function arrayToBytes(array: CRDTArray): Uint8Array {
*/
function bytesToTree(bytes?: Uint8Array): CRDTTree {
if (!bytes) {
throw new Error('bytes is empty');
throw new YorkieError(Code.ErrInvalidArgument, 'bytes is empty');
}

const pbElement = PbJSONElement.fromBinary(bytes);
Expand Down
19 changes: 15 additions & 4 deletions src/document/crdt/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { Indexable } from '@yorkie-js-sdk/src/document/document';
import type * as Devtools from '@yorkie-js-sdk/src/devtools/types';
import { escapeString } from '@yorkie-js-sdk/src/document/json/strings';
import { GCChild, GCPair, GCParent } from '@yorkie-js-sdk/src/document/crdt/gc';
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';

/**
* `TreeNode` represents a node in the tree.
Expand Down Expand Up @@ -223,7 +224,8 @@ export class CRDTTreePos {
const parentNode = tree.findFloorNode(parentID);
let leftNode = tree.findFloorNode(leftSiblingID);
if (!parentNode || !leftNode) {
throw new Error(
throw new YorkieError(
Code.ErrOperationNotPermitted,
`cannot find node of CRDTTreePos(${parentID.toTestString()}, ${leftSiblingID.toTestString()})`,
);
}
Expand Down Expand Up @@ -514,7 +516,10 @@ export class CRDTTreeNode
*/
get value() {
if (!this.isText) {
throw new Error(`cannot get value of element node: ${this.type}`);
throw new YorkieError(
Code.ErrInvalidType,
`cannot get value of element node: ${this.type}`,
);
}

return this._value;
Expand All @@ -525,7 +530,10 @@ export class CRDTTreeNode
*/
set value(v: string) {
if (!this.isText) {
throw new Error(`cannot set value of element node: ${this.type}`);
throw new YorkieError(
Code.ErrInvalidType,
`cannot set value of element node: ${this.type}`,
);
}

this._value = v;
Expand Down Expand Up @@ -1217,7 +1225,10 @@ export class CRDTTree extends CRDTElement implements GCParent {
ticket: TimeTicket,
): void {
// TODO(hackerwins, easylogic): Implement this with keeping references of the nodes.
throw new Error(`not implemented: ${target}, ${source}, ${ticket}`);
throw new YorkieError(
Code.ErrUnimplemented,
`not implemented: ${target}, ${source}, ${ticket}`,
);
}

/**
Expand Down
35 changes: 28 additions & 7 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,13 @@ export class Document<T, P extends Indexable = Indexable> {
} catch (err) {
// drop clone because it is contaminated.
this.clone = undefined;
logger.error(err);

if (err instanceof YorkieError) {
logger.error(err.toString());
} else {
logger.error(err);
}

throw err;
} finally {
this.isUpdating = false;
Expand Down Expand Up @@ -847,7 +853,10 @@ export class Document<T, P extends Indexable = Indexable> {
): Unsubscribe {
if (typeof arg1 === 'string') {
if (typeof arg2 !== 'function') {
throw new Error('Second argument must be a callback function');
throw new YorkieError(
Code.ErrInvalidArgument,
'Second argument must be a callback function',
);
}
if (arg1 === 'presence') {
const callback = arg2 as DocEventCallbackMap<P>['presence'];
Expand Down Expand Up @@ -1021,7 +1030,7 @@ export class Document<T, P extends Indexable = Indexable> {
complete,
);
}
throw new Error(`"${arg1}" is not a valid`);
throw new YorkieError(Code.ErrInvalidArgument, `"${arg1}" is not a valid`);
}

/**
Expand Down Expand Up @@ -1761,11 +1770,17 @@ export class Document<T, P extends Indexable = Indexable> {
*/
private undo(): void {
if (this.isUpdating) {
throw new Error('Undo is not allowed during an update');
throw new YorkieError(
Code.ErrOperationNotPermitted,
'Undo is not allowed during an update',
);
}
const undoOps = this.internalHistory.popUndo();
if (undoOps === undefined) {
throw new Error('There is no operation to be undone');
throw new YorkieError(
Code.ErrOperationNotPermitted,
'There is no operation to be undone',
);
}

this.ensureClone();
Expand Down Expand Up @@ -1855,12 +1870,18 @@ export class Document<T, P extends Indexable = Indexable> {
*/
private redo(): void {
if (this.isUpdating) {
throw new Error('Redo is not allowed during an update');
throw new YorkieError(
Code.ErrOperationNotPermitted,
'Redo is not allowed during an update',
);
}

const redoOps = this.internalHistory.popRedo();
if (redoOps === undefined) {
throw new Error('There is no operation to be redone');
throw new YorkieError(
Code.ErrOperationNotPermitted,
'There is no operation to be redone',
);
}

this.ensureClone();
Expand Down
2 changes: 1 addition & 1 deletion src/document/json/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class Counter {
*/
public toJSForTest(): Devtools.JSONElement {
if (!this.context || !this.counter) {
throw new Error('it is not initialized yet');
throw new YorkieError(Code.ErrOperationNotReady, 'Counter is not ready');
}

return this.counter.toJSForTest();
Expand Down
10 changes: 8 additions & 2 deletions src/document/json/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ export class Text<A extends Indexable = Indexable> {
*/
public toJSON(): string {
if (!this.context || !this.text) {
throw new Error('it is not initialized yet');
throw new YorkieError(
Code.ErrOperationNotReady,
'Text is not initialized yet',
);
}

return this.text.toJSON();
Expand All @@ -315,7 +318,10 @@ export class Text<A extends Indexable = Indexable> {
*/
public toJSForTest(): Devtools.JSONElement {
if (!this.context || !this.text) {
throw new Error('it is not initialized yet');
throw new YorkieError(
Code.ErrOperationNotReady,
'Text is not initialized yet',
);
}

return this.text.toJSForTest();
Expand Down
Loading

0 comments on commit 35be776

Please sign in to comment.