Skip to content

Commit d9908d7

Browse files
committed
feat: improve error logging
1 parent af585d7 commit d9908d7

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

packages/core/src/helpers/throwFns.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { DoOnStreamFns, StreamResponse } from '@magnetarjs/types'
2-
import { isCollectionModule, isDocModule } from '@magnetarjs/utils'
2+
import { isCollectionModule, isDocModule, logWithFlair } from '@magnetarjs/utils'
33

44
export function logError(errorMessage: string): undefined {
5-
console.error('[@magnetarjs error]\n', errorMessage)
5+
logWithFlair(errorMessage, { error: true })
66
}
77

88
export function logErrorAndThrow(errorMessage: string): undefined {

packages/utils-firestore/src/batchSync.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const newStack = (): Stack => ({
4747
* @param {Record<string, unknown>} payload
4848
* @returns {number}
4949
*/
50-
function countOperations(payload: { [key: string]: unknown }): number {
50+
function countOperations(_payload: { [key: string]: unknown }): number {
5151
const count = 1
5252
// todo: when actions like serverTimestamp, arrayUnion and increment are supported, count them here
5353
return count
@@ -111,6 +111,9 @@ export function batchSyncFactory(
111111
try {
112112
applySyncBatch(writeBatch as any, stack.batch, db as any)
113113
} catch (error) {
114+
if (debug) {
115+
logWithFlair('Error while preparing Firestore write batch', { error, batch: stack.batch })
116+
}
114117
stack.rejects.forEach((rej) => rej(error))
115118
if (state.queue.length) {
116119
triggerSync(0)
@@ -125,7 +128,12 @@ export function batchSyncFactory(
125128
writeBatch
126129
.commit()
127130
.then(() => stack.resolves.forEach((res) => res()))
128-
.catch((error: unknown) => stack.rejects.forEach((rej) => rej(error)))
131+
.catch((error: unknown) => {
132+
if (debug) {
133+
logWithFlair('Firestore batch commit failed', { error, batch: stack.batch })
134+
}
135+
stack.rejects.forEach((rej) => rej(error))
136+
})
129137
.finally(() => {
130138
if (state.queue.length) {
131139
triggerSync(0)

packages/utils/src/logger.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import type { EventFnSuccess, QueryClause, WhereClause } from '@magnetarjs/types'
2-
import { isArray, isFullArray, isNumber } from 'is-what'
2+
import { isAnyObject, isArray, isFullArray, isNumber } from 'is-what'
33

44
/**
55
* `Line-height: 2` is to prevent line overlapping on Safari
66
*/
77
const LOGGER_STYLE =
88
'background: #0e0f15; color: #af98e6; border-radius: 4px; padding: 6px 10px; line-height: 2;'
99

10+
const LOGGER_STYLE_ERROR =
11+
'background: #b91c1c; color: #ffffff; border-radius: 4px; padding: 6px 10px; line-height: 2;'
12+
1013
/**
1114
* Logs to the console with `console.info` and colors.
1215
*/
1316
export function logWithFlair(message: string, ...args: any[]): undefined {
14-
console.info(`%c💫 [magnetar] ${message}`, LOGGER_STYLE, ...args)
17+
const isError = args.some((a) => isAnyObject(a) && !!a?.['error'])
18+
if (isError) {
19+
console.error(`%c💫 [magnetar] ${message}`, LOGGER_STYLE_ERROR, ...args)
20+
} else {
21+
console.info(`%c💫 [magnetar] ${message}`, LOGGER_STYLE, ...args)
22+
}
1523
}
1624

1725
let lastGroupLogTime = 0
@@ -38,7 +46,7 @@ function shouldLog(params: any, preventLogFor: number) {
3846
export function logWithFlairGroup(
3947
title: string,
4048
nestedMessage: string,
41-
options?: { preventLogFor: number }
49+
options?: { preventLogFor: number },
4250
): undefined {
4351
if (options && !shouldLog([title, nestedMessage], options.preventLogFor)) return
4452

@@ -51,12 +59,12 @@ function stringifyQueryClause(q: QueryClause): string {
5159
return 'or' in q
5260
? `or(${q.or
5361
.map((clause) =>
54-
isArray(clause) ? stringifyWhereClause(clause) : stringifyQueryClause(clause)
62+
isArray(clause) ? stringifyWhereClause(clause) : stringifyQueryClause(clause),
5563
)
5664
.join(', ')})`
5765
: `and(${q.and
5866
.map((clause) =>
59-
isArray(clause) ? stringifyWhereClause(clause) : stringifyQueryClause(clause)
67+
isArray(clause) ? stringifyWhereClause(clause) : stringifyQueryClause(clause),
6068
)
6169
.join(', ')})`
6270
}

0 commit comments

Comments
 (0)