Skip to content

Commit

Permalink
feat(engin5): Refactor logging statements in collection and document …
Browse files Browse the repository at this point in the history
…references
  • Loading branch information
alimd committed Dec 25, 2023
1 parent efe9a1d commit 636175f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions packages/engine5/src/lib/collection-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class CollectionReference<TItem extends Record<string, unknown> = Record<
protected context_: CollectionContext<TItem>,
protected updatedCallback_: (id: string, context: CollectionContext<TItem>) => void,
) {
this._logger.logMethodArgs?.('new', context_.meta.id);
this._logger.logMethodArgs?.('new', context_.meta);
}

/**
Expand Down Expand Up @@ -139,7 +139,10 @@ export class CollectionReference<TItem extends Record<string, unknown> = Record<
*/
protected item_(id: string | number): CollectionItem<TItem> {
const item = this.context_.data[id];
if (item === undefined) throw new Error(`collection_item_not_found`, {cause: {id}});
if (item === undefined) {
this._logger.accident('item_', `collection_item_not_found`, {id});
throw new Error(`collection_item_not_found`, {cause: {id}});
}
return item;
}

Expand Down Expand Up @@ -183,7 +186,10 @@ export class CollectionReference<TItem extends Record<string, unknown> = Record<
*/
create(id: string | number, data: TItem): void {
this._logger.logMethodArgs?.('create', {id, data});
if (id in this.context_.data) throw new Error(`collection_item_exist`, {cause: {id}});
if (this.exists(id)) {
this._logger.accident('create', `collection_item_exist`, {id});
throw new Error(`collection_item_exist`, {cause: {id}});
}
this.context_.data[id] = {
meta: {
id,
Expand Down
2 changes: 1 addition & 1 deletion packages/engine5/src/lib/document-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class DocumentReference<TDoc extends Record<string, unknown> = Record<str
protected context_: DocumentContext<TDoc>,
protected updatedCallback_: (id: string, context: DocumentContext<TDoc>) => void,
) {
this._logger.logMethodArgs?.('new', context_.meta.id);
this._logger.logMethodArgs?.('new', context_.meta);
}

/**
Expand Down

0 comments on commit 636175f

Please sign in to comment.