Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify error throwing methods #878

Merged
merged 11 commits into from
Aug 7, 2024
18 changes: 12 additions & 6 deletions src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ function toValueType(valueType: PrimitiveType): PbValueType {
case PrimitiveType.Date:
return PbValueType.DATE;
default:
throw new YorkieError(Code.Unsupported, `unsupported type: ${valueType}`);
throw new YorkieError(
Code.ErrInvalidType,
`unsupported type: ${valueType}`,
);
}
}

Expand All @@ -216,7 +219,10 @@ function toCounterType(valueType: CounterType): PbValueType {
case CounterType.LongCnt:
return PbValueType.LONG_CNT;
default:
throw new YorkieError(Code.Unsupported, `unsupported type: ${valueType}`);
throw new YorkieError(
Code.ErrInvalidType,
`unsupported type: ${valueType}`,
);
}
}

Expand Down Expand Up @@ -859,7 +865,7 @@ function fromPresenceChange<P extends Indexable>(
};
}

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

/**
Expand Down Expand Up @@ -1476,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 @@ -1495,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 @@ -1514,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
12 changes: 9 additions & 3 deletions src/document/crdt/element_rht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { logger } from '@yorkie-js-sdk/src/util/logger';
import { TimeTicket } from '@yorkie-js-sdk/src/document/time/ticket';
import { CRDTElement } from '@yorkie-js-sdk/src/document/crdt/element';
import { YorkieError, Code } from '@yorkie-js-sdk/src/util/error';

/**
* `ElementRHTNode` is a node of ElementRHT.
Expand Down Expand Up @@ -114,7 +115,9 @@ export class ElementRHT {
*/
public delete(createdAt: TimeTicket, executedAt: TimeTicket): CRDTElement {
if (!this.nodeMapByCreatedAt.has(createdAt.toIDString())) {
logger.fatal(`fail to find ${createdAt.toIDString()}`);
const ErrorMessage = `fail to find ${createdAt.toIDString()}`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}

const node = this.nodeMapByCreatedAt.get(createdAt.toIDString())!;
Expand Down Expand Up @@ -142,8 +145,11 @@ export class ElementRHT {
element.getCreatedAt().toIDString(),
);
if (!node) {
logger.fatal(`fail to find ${element.getCreatedAt().toIDString()}`);
return;
const ErrorMessage = `fail to find ${element
.getCreatedAt()
.toIDString()}`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}

const nodeByKey = this.nodeMapByKey.get(node.getStrKey());
Expand Down
23 changes: 15 additions & 8 deletions src/document/crdt/rga_tree_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '@yorkie-js-sdk/src/document/time/ticket';
import { CRDTElement } from '@yorkie-js-sdk/src/document/crdt/element';
import { Primitive } from '@yorkie-js-sdk/src/document/crdt/primitive';
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';

/**
* `RGATreeListNode` is a node of RGATreeList.
Expand Down Expand Up @@ -180,7 +181,9 @@ export class RGATreeList {
): RGATreeListNode {
let node = this.nodeMapByCreatedAt.get(createdAt.toIDString());
if (!node) {
logger.fatal(`cant find the given node: ${createdAt.toIDString()}`);
const ErrorMessage = `cant find the given node: ${createdAt.toIDString()}`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}

while (
Expand Down Expand Up @@ -232,12 +235,16 @@ export class RGATreeList {
): void {
const prevNode = this.nodeMapByCreatedAt.get(prevCreatedAt.toIDString());
if (!prevNode) {
logger.fatal(`cant find the given node: ${prevCreatedAt.toIDString()}`);
const ErrorMessage = `cant find the given node: ${prevCreatedAt.toIDString()}`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}

const node = this.nodeMapByCreatedAt.get(createdAt.toIDString());
if (!node) {
logger.fatal(`cant find the given node: ${createdAt.toIDString()}`);
const ErrorMessage = `cant find the given node: ${createdAt.toIDString()}`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}

if (
Expand Down Expand Up @@ -284,11 +291,11 @@ export class RGATreeList {
element.getCreatedAt().toIDString(),
);
if (!node) {
logger.fatal(
`fail to find the given createdAt: ${element
.getCreatedAt()
.toIDString()}`,
);
const ErrorMessage = `fail to find the given createdAt: ${element
.getCreatedAt()
.toIDString()}`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}
this.release(node!);
}
Expand Down
17 changes: 10 additions & 7 deletions src/document/crdt/rga_tree_split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
TimeTicketStruct,
} from '@yorkie-js-sdk/src/document/time/ticket';
import { GCChild, GCPair, GCParent } from '@yorkie-js-sdk/src/document/crdt/gc';
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';

export interface ValueChange<T> {
actor: ActorID;
Expand Down Expand Up @@ -634,9 +635,9 @@ export class RGATreeSplit<T extends RGATreeSplitValue> implements GCParent {
? this.findFloorNodePreferToLeft(absoluteID)
: this.findFloorNode(absoluteID);
if (!node) {
logger.fatal(
`the node of the given id should be found: ${absoluteID.toTestString()}`,
);
const ErrorMessage = `the node of the given id should be found: ${absoluteID.toTestString()}`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}
const index = this.treeByIndex.indexOf(node!);
const offset = node!.isRemoved()
Expand Down Expand Up @@ -793,9 +794,9 @@ export class RGATreeSplit<T extends RGATreeSplitValue> implements GCParent {
): RGATreeSplitNode<T> {
let node = this.findFloorNode(id);
if (!node) {
logger.fatal(
`the node of the given id should be found: ${id.toTestString()}`,
);
const ErrorMessage = `the node of the given id should be found: ${id.toTestString()}`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}

if (id.getOffset() > 0 && node!.getID().getOffset() == id.getOffset()) {
Expand Down Expand Up @@ -847,7 +848,9 @@ export class RGATreeSplit<T extends RGATreeSplitValue> implements GCParent {
offset: number,
): RGATreeSplitNode<T> | undefined {
if (offset > node.getContentLength()) {
logger.fatal('offset should be less than or equal to length');
const ErrorMessage = `offset should be less than or equal to length`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}

if (offset === 0) {
Expand Down
5 changes: 4 additions & 1 deletion src/document/crdt/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { CRDTObject } from '@yorkie-js-sdk/src/document/crdt/object';
import { GCPair } from '@yorkie-js-sdk/src/document/crdt/gc';
import { CRDTText } from '@yorkie-js-sdk/src/document/crdt/text';
import { CRDTTree } from '@yorkie-js-sdk/src/document/crdt/tree';
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';

/**
* `CRDTElementPair` is a structure that represents a pair of element and its
Expand Down Expand Up @@ -134,7 +135,9 @@ export class CRDTRoot {
const createdAt = pair.element.getCreatedAt();
const subPath = pair.parent.subPathOf(createdAt);
if (subPath === undefined) {
logger.fatal(`cant find the given element: ${createdAt.toIDString()}`);
const ErrorMessage = `cant find the given element: ${createdAt.toIDString()}`;
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrInvalidArgument, ErrorMessage);
}

subPaths.unshift(subPath!);
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.ErrRefused,
`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;
chacha912 marked this conversation as resolved.
Show resolved Hide resolved
} 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.ErrRefused,
'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.ErrRefused,
'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.ErrRefused,
'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.ErrRefused,
'There is no operation to be redone',
);
}

this.ensureClone();
Expand Down
10 changes: 6 additions & 4 deletions src/document/json/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
CRDTCounter,
} from '@yorkie-js-sdk/src/document/crdt/counter';
import type * as Devtools from '@yorkie-js-sdk/src/devtools/types';
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';

/**
* `Counter` is a custom data type that is used to counter.
Expand Down Expand Up @@ -78,9 +79,9 @@ export class Counter {
*/
public increase(v: number | Long): Counter {
if (!this.context || !this.counter) {
logger.fatal('it is not initialized yet');
// @ts-ignore
return;
const ErrorMessage = 'Counter is not initialized yet';
logger.fatal(ErrorMessage);
throw new YorkieError(Code.ErrNotInitialized, ErrorMessage);
}

const ticket = this.context.issueTimeTicket();
Expand All @@ -105,7 +106,8 @@ export class Counter {
*/
public toJSForTest(): Devtools.JSONElement {
if (!this.context || !this.counter) {
throw new Error('it is not initialized yet');
const ErrorMessage = 'Counter is not initialized yet';
throw new YorkieError(Code.ErrNotInitialized, ErrorMessage);
}

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