Skip to content

Commit 267878f

Browse files
gwbaik9717hackerwins
authored andcommitted
Unify error throwing methods (#878)
This commit unifies error throwing methods and removes throw error in the logger. Previously, implementation of the logger includes both logging and error throwing, which can lead to confusion and type problems. By separating these concerns, we can ensure that the logger is only responsible for logging messages, while errors are thrown explicitly using `YorkieError`.
1 parent 3e6d0b2 commit 267878f

30 files changed

+443
-183
lines changed

src/api/converter.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ function toValueType(valueType: PrimitiveType): PbValueType {
202202
case PrimitiveType.Date:
203203
return PbValueType.DATE;
204204
default:
205-
throw new YorkieError(Code.Unsupported, `unsupported type: ${valueType}`);
205+
throw new YorkieError(
206+
Code.ErrInvalidType,
207+
`unsupported type: ${valueType}`,
208+
);
206209
}
207210
}
208211

@@ -216,7 +219,10 @@ function toCounterType(valueType: CounterType): PbValueType {
216219
case CounterType.LongCnt:
217220
return PbValueType.LONG_CNT;
218221
default:
219-
throw new YorkieError(Code.Unsupported, `unsupported type: ${valueType}`);
222+
throw new YorkieError(
223+
Code.ErrInvalidType,
224+
`unsupported type: ${valueType}`,
225+
);
220226
}
221227
}
222228

@@ -859,7 +865,7 @@ function fromPresenceChange<P extends Indexable>(
859865
};
860866
}
861867

862-
throw new YorkieError(Code.Unsupported, `unsupported type: ${type}`);
868+
throw new YorkieError(Code.ErrInvalidType, `unsupported type: ${type}`);
863869
}
864870

865871
/**
@@ -1476,7 +1482,7 @@ function bytesToSnapshot<P extends Indexable>(
14761482
*/
14771483
function bytesToObject(bytes?: Uint8Array): CRDTObject {
14781484
if (!bytes) {
1479-
throw new Error('bytes is empty');
1485+
throw new YorkieError(Code.ErrInvalidArgument, 'bytes is empty');
14801486
}
14811487

14821488
const pbElement = PbJSONElement.fromBinary(bytes);
@@ -1495,7 +1501,7 @@ function objectToBytes(obj: CRDTObject): Uint8Array {
14951501
*/
14961502
function bytesToArray(bytes?: Uint8Array): CRDTArray {
14971503
if (!bytes) {
1498-
throw new Error('bytes is empty');
1504+
throw new YorkieError(Code.ErrInvalidArgument, 'bytes is empty');
14991505
}
15001506

15011507
const pbElement = PbJSONElement.fromBinary(bytes);
@@ -1514,7 +1520,7 @@ function arrayToBytes(array: CRDTArray): Uint8Array {
15141520
*/
15151521
function bytesToTree(bytes?: Uint8Array): CRDTTree {
15161522
if (!bytes) {
1517-
throw new Error('bytes is empty');
1523+
throw new YorkieError(Code.ErrInvalidArgument, 'bytes is empty');
15181524
}
15191525

15201526
const pbElement = PbJSONElement.fromBinary(bytes);

src/document/crdt/element_rht.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { logger } from '@yorkie-js-sdk/src/util/logger';
1817
import { TimeTicket } from '@yorkie-js-sdk/src/document/time/ticket';
1918
import { CRDTElement } from '@yorkie-js-sdk/src/document/crdt/element';
19+
import { YorkieError, Code } from '@yorkie-js-sdk/src/util/error';
2020

2121
/**
2222
* `ElementRHTNode` is a node of ElementRHT.
@@ -114,7 +114,10 @@ export class ElementRHT {
114114
*/
115115
public delete(createdAt: TimeTicket, executedAt: TimeTicket): CRDTElement {
116116
if (!this.nodeMapByCreatedAt.has(createdAt.toIDString())) {
117-
logger.fatal(`fail to find ${createdAt.toIDString()}`);
117+
throw new YorkieError(
118+
Code.ErrInvalidArgument,
119+
`fail to find ${createdAt.toIDString()}`,
120+
);
118121
}
119122

120123
const node = this.nodeMapByCreatedAt.get(createdAt.toIDString())!;
@@ -142,8 +145,10 @@ export class ElementRHT {
142145
element.getCreatedAt().toIDString(),
143146
);
144147
if (!node) {
145-
logger.fatal(`fail to find ${element.getCreatedAt().toIDString()}`);
146-
return;
148+
throw new YorkieError(
149+
Code.ErrInvalidArgument,
150+
`fail to find ${element.getCreatedAt().toIDString()}`,
151+
);
147152
}
148153

149154
const nodeByKey = this.nodeMapByKey.get(node.getStrKey());

src/document/crdt/rga_tree_list.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { logger } from '@yorkie-js-sdk/src/util/logger';
1817
import { SplayNode, SplayTree } from '@yorkie-js-sdk/src/util/splay_tree';
1918
import {
2019
InitialTimeTicket,
2120
TimeTicket,
2221
} from '@yorkie-js-sdk/src/document/time/ticket';
2322
import { CRDTElement } from '@yorkie-js-sdk/src/document/crdt/element';
2423
import { Primitive } from '@yorkie-js-sdk/src/document/crdt/primitive';
24+
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';
2525

2626
/**
2727
* `RGATreeListNode` is a node of RGATreeList.
@@ -180,7 +180,10 @@ export class RGATreeList {
180180
): RGATreeListNode {
181181
let node = this.nodeMapByCreatedAt.get(createdAt.toIDString());
182182
if (!node) {
183-
logger.fatal(`cant find the given node: ${createdAt.toIDString()}`);
183+
throw new YorkieError(
184+
Code.ErrInvalidArgument,
185+
`cant find the given node: ${createdAt.toIDString()}`,
186+
);
184187
}
185188

186189
while (
@@ -232,12 +235,18 @@ export class RGATreeList {
232235
): void {
233236
const prevNode = this.nodeMapByCreatedAt.get(prevCreatedAt.toIDString());
234237
if (!prevNode) {
235-
logger.fatal(`cant find the given node: ${prevCreatedAt.toIDString()}`);
238+
throw new YorkieError(
239+
Code.ErrInvalidArgument,
240+
`cant find the given node: ${prevCreatedAt.toIDString()}`,
241+
);
236242
}
237243

238244
const node = this.nodeMapByCreatedAt.get(createdAt.toIDString());
239245
if (!node) {
240-
logger.fatal(`cant find the given node: ${createdAt.toIDString()}`);
246+
throw new YorkieError(
247+
Code.ErrInvalidArgument,
248+
`cant find the given node: ${createdAt.toIDString()}`,
249+
);
241250
}
242251

243252
if (
@@ -284,7 +293,8 @@ export class RGATreeList {
284293
element.getCreatedAt().toIDString(),
285294
);
286295
if (!node) {
287-
logger.fatal(
296+
throw new YorkieError(
297+
Code.ErrInvalidArgument,
288298
`fail to find the given createdAt: ${element
289299
.getCreatedAt()
290300
.toIDString()}`,

src/document/crdt/rga_tree_split.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { logger } from '@yorkie-js-sdk/src/util/logger';
1817
import { ActorID } from '@yorkie-js-sdk/src/document/time/actor_id';
1918
import { Comparator } from '@yorkie-js-sdk/src/util/comparator';
2019
import { SplayNode, SplayTree } from '@yorkie-js-sdk/src/util/splay_tree';
@@ -26,6 +25,7 @@ import {
2625
TimeTicketStruct,
2726
} from '@yorkie-js-sdk/src/document/time/ticket';
2827
import { GCChild, GCPair, GCParent } from '@yorkie-js-sdk/src/document/crdt/gc';
28+
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';
2929

3030
export interface ValueChange<T> {
3131
actor: ActorID;
@@ -634,7 +634,8 @@ export class RGATreeSplit<T extends RGATreeSplitValue> implements GCParent {
634634
? this.findFloorNodePreferToLeft(absoluteID)
635635
: this.findFloorNode(absoluteID);
636636
if (!node) {
637-
logger.fatal(
637+
throw new YorkieError(
638+
Code.ErrInvalidArgument,
638639
`the node of the given id should be found: ${absoluteID.toTestString()}`,
639640
);
640641
}
@@ -793,7 +794,8 @@ export class RGATreeSplit<T extends RGATreeSplitValue> implements GCParent {
793794
): RGATreeSplitNode<T> {
794795
let node = this.findFloorNode(id);
795796
if (!node) {
796-
logger.fatal(
797+
throw new YorkieError(
798+
Code.ErrInvalidArgument,
797799
`the node of the given id should be found: ${id.toTestString()}`,
798800
);
799801
}
@@ -847,7 +849,10 @@ export class RGATreeSplit<T extends RGATreeSplitValue> implements GCParent {
847849
offset: number,
848850
): RGATreeSplitNode<T> | undefined {
849851
if (offset > node.getContentLength()) {
850-
logger.fatal('offset should be less than or equal to length');
852+
throw new YorkieError(
853+
Code.ErrInvalidArgument,
854+
`offset should be less than or equal to length`,
855+
);
851856
}
852857

853858
if (offset === 0) {

src/document/crdt/root.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { logger } from '@yorkie-js-sdk/src/util/logger';
1817
import {
1918
InitialTimeTicket,
2019
TimeTicket,
@@ -27,6 +26,7 @@ import { CRDTObject } from '@yorkie-js-sdk/src/document/crdt/object';
2726
import { GCPair } from '@yorkie-js-sdk/src/document/crdt/gc';
2827
import { CRDTText } from '@yorkie-js-sdk/src/document/crdt/text';
2928
import { CRDTTree } from '@yorkie-js-sdk/src/document/crdt/tree';
29+
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';
3030

3131
/**
3232
* `CRDTElementPair` is a structure that represents a pair of element and its
@@ -134,7 +134,10 @@ export class CRDTRoot {
134134
const createdAt = pair.element.getCreatedAt();
135135
const subPath = pair.parent.subPathOf(createdAt);
136136
if (subPath === undefined) {
137-
logger.fatal(`cant find the given element: ${createdAt.toIDString()}`);
137+
throw new YorkieError(
138+
Code.ErrInvalidArgument,
139+
`cant find the given element: ${createdAt.toIDString()}`,
140+
);
138141
}
139142

140143
subPaths.unshift(subPath!);

src/document/crdt/tree.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { Indexable } from '@yorkie-js-sdk/src/document/document';
4343
import type * as Devtools from '@yorkie-js-sdk/src/devtools/types';
4444
import { escapeString } from '@yorkie-js-sdk/src/document/json/strings';
4545
import { GCChild, GCPair, GCParent } from '@yorkie-js-sdk/src/document/crdt/gc';
46+
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';
4647

4748
/**
4849
* `TreeNode` represents a node in the tree.
@@ -223,7 +224,8 @@ export class CRDTTreePos {
223224
const parentNode = tree.findFloorNode(parentID);
224225
let leftNode = tree.findFloorNode(leftSiblingID);
225226
if (!parentNode || !leftNode) {
226-
throw new Error(
227+
throw new YorkieError(
228+
Code.ErrRefused,
227229
`cannot find node of CRDTTreePos(${parentID.toTestString()}, ${leftSiblingID.toTestString()})`,
228230
);
229231
}
@@ -514,7 +516,10 @@ export class CRDTTreeNode
514516
*/
515517
get value() {
516518
if (!this.isText) {
517-
throw new Error(`cannot get value of element node: ${this.type}`);
519+
throw new YorkieError(
520+
Code.ErrInvalidType,
521+
`cannot get value of element node: ${this.type}`,
522+
);
518523
}
519524

520525
return this._value;
@@ -525,7 +530,10 @@ export class CRDTTreeNode
525530
*/
526531
set value(v: string) {
527532
if (!this.isText) {
528-
throw new Error(`cannot set value of element node: ${this.type}`);
533+
throw new YorkieError(
534+
Code.ErrInvalidType,
535+
`cannot set value of element node: ${this.type}`,
536+
);
529537
}
530538

531539
this._value = v;
@@ -1217,7 +1225,10 @@ export class CRDTTree extends CRDTElement implements GCParent {
12171225
ticket: TimeTicket,
12181226
): void {
12191227
// TODO(hackerwins, easylogic): Implement this with keeping references of the nodes.
1220-
throw new Error(`not implemented: ${target}, ${source}, ${ticket}`);
1228+
throw new YorkieError(
1229+
Code.ErrUnimplemented,
1230+
`not implemented: ${target}, ${source}, ${ticket}`,
1231+
);
12211232
}
12221233

12231234
/**

src/document/document.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ export class Document<T, P extends Indexable = Indexable> {
660660
} catch (err) {
661661
// drop clone because it is contaminated.
662662
this.clone = undefined;
663-
logger.error(err);
663+
664664
throw err;
665665
} finally {
666666
this.isUpdating = false;
@@ -847,7 +847,10 @@ export class Document<T, P extends Indexable = Indexable> {
847847
): Unsubscribe {
848848
if (typeof arg1 === 'string') {
849849
if (typeof arg2 !== 'function') {
850-
throw new Error('Second argument must be a callback function');
850+
throw new YorkieError(
851+
Code.ErrInvalidArgument,
852+
'Second argument must be a callback function',
853+
);
851854
}
852855
if (arg1 === 'presence') {
853856
const callback = arg2 as DocEventCallbackMap<P>['presence'];
@@ -1021,7 +1024,7 @@ export class Document<T, P extends Indexable = Indexable> {
10211024
complete,
10221025
);
10231026
}
1024-
throw new Error(`"${arg1}" is not a valid`);
1027+
throw new YorkieError(Code.ErrInvalidArgument, `"${arg1}" is not a valid`);
10251028
}
10261029

10271030
/**
@@ -1761,11 +1764,17 @@ export class Document<T, P extends Indexable = Indexable> {
17611764
*/
17621765
private undo(): void {
17631766
if (this.isUpdating) {
1764-
throw new Error('Undo is not allowed during an update');
1767+
throw new YorkieError(
1768+
Code.ErrRefused,
1769+
'Undo is not allowed during an update',
1770+
);
17651771
}
17661772
const undoOps = this.internalHistory.popUndo();
17671773
if (undoOps === undefined) {
1768-
throw new Error('There is no operation to be undone');
1774+
throw new YorkieError(
1775+
Code.ErrRefused,
1776+
'There is no operation to be undone',
1777+
);
17691778
}
17701779

17711780
this.ensureClone();
@@ -1855,12 +1864,18 @@ export class Document<T, P extends Indexable = Indexable> {
18551864
*/
18561865
private redo(): void {
18571866
if (this.isUpdating) {
1858-
throw new Error('Redo is not allowed during an update');
1867+
throw new YorkieError(
1868+
Code.ErrRefused,
1869+
'Redo is not allowed during an update',
1870+
);
18591871
}
18601872

18611873
const redoOps = this.internalHistory.popRedo();
18621874
if (redoOps === undefined) {
1863-
throw new Error('There is no operation to be redone');
1875+
throw new YorkieError(
1876+
Code.ErrRefused,
1877+
'There is no operation to be redone',
1878+
);
18641879
}
18651880

18661881
this.ensureClone();

src/document/json/counter.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { logger } from '@yorkie-js-sdk/src/util/logger';
1817
import { TimeTicket } from '@yorkie-js-sdk/src/document/time/ticket';
1918
import { ChangeContext } from '@yorkie-js-sdk/src/document/change/context';
2019
import { Primitive } from '@yorkie-js-sdk/src/document/crdt/primitive';
@@ -25,6 +24,7 @@ import {
2524
CRDTCounter,
2625
} from '@yorkie-js-sdk/src/document/crdt/counter';
2726
import type * as Devtools from '@yorkie-js-sdk/src/devtools/types';
27+
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';
2828

2929
/**
3030
* `Counter` is a custom data type that is used to counter.
@@ -78,9 +78,10 @@ export class Counter {
7878
*/
7979
public increase(v: number | Long): Counter {
8080
if (!this.context || !this.counter) {
81-
logger.fatal('it is not initialized yet');
82-
// @ts-ignore
83-
return;
81+
throw new YorkieError(
82+
Code.ErrNotInitialized,
83+
'Counter is not initialized yet',
84+
);
8485
}
8586

8687
const ticket = this.context.issueTimeTicket();
@@ -105,7 +106,10 @@ export class Counter {
105106
*/
106107
public toJSForTest(): Devtools.JSONElement {
107108
if (!this.context || !this.counter) {
108-
throw new Error('it is not initialized yet');
109+
throw new YorkieError(
110+
Code.ErrNotInitialized,
111+
'Counter is not initialized yet',
112+
);
109113
}
110114

111115
return this.counter.toJSForTest();

0 commit comments

Comments
 (0)