Skip to content

Commit 60452ef

Browse files
committed
Add setConsole utility
Fixes TanStack#125
1 parent 92bdf70 commit 60452ef

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

.size-snapshot.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"dist/index.js": {
3-
"bundled": 31436,
4-
"minified": 15930,
5-
"gzipped": 4571
3+
"bundled": 31608,
4+
"minified": 15961,
5+
"gzipped": 4574
66
},
77
"dist/index.es.js": {
8-
"bundled": 30916,
9-
"minified": 15466,
10-
"gzipped": 4473,
8+
"bundled": 31088,
9+
"minified": 15497,
10+
"gzipped": 4478,
1111
"treeshaked": {
1212
"rollup": {
1313
"code": 3292,

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 0.4.2
4+
5+
- Added a new `setConsole` exported function that allows you replace the `console` interface used to log errors. By default, the `window.console` object is used.
6+
7+
## 0.4.1
8+
9+
- Fixed an issue where interval fetching errors would throw repeatedly
10+
311
## 0.4.0
412

513
- Added the `useMutation.throwOnError` and corresponding `queryConfig.throwOnError` option to configure whether the `mutate` function rethrows errors encountered in the mutation function

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ This library is being built and maintained by me, @tannerlinsley and I am always
246246
- [`useIsFetching`](#useisfetching)
247247
- [`clearQueryCache`](#clearquerycache)
248248
- [`ReactQueryConfigProvider`](#reactqueryconfigprovider)
249+
- [`setConsole`](#setConsole)
249250

250251
## Installation
251252

@@ -1592,3 +1593,23 @@ function App() {
15921593
- `config: Object`
15931594
- Must be **stable** or **memoized**. Do not create an inline object!
15941595
- For a description of all config options, please see their usage in both the [`useQuery` hook](#usequery) and the [`useMutation` hook](#usemutation).
1596+
1597+
## `setConsole`
1598+
1599+
`setConsole` is an optional utility function that allows you replace the `console` interface used to log errors. By default, the `window.console` object is used. If no global `console` object is found in the environment, nothing will be logged.
1600+
1601+
```js
1602+
import { setConsole } from 'react-query'
1603+
import { printLog, printWarn, printError } from 'custom-logger'
1604+
1605+
setConsole({
1606+
log: printLog,
1607+
warn: printWarn,
1608+
error: printError,
1609+
})
1610+
```
1611+
1612+
### Options
1613+
1614+
- `console: Object`
1615+
- Must implement the `log`, `warn`, and `error` methods.

src/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export let globalStateListeners = []
66
let uid = 0
77
const configContext = React.createContext()
88
const isServer = typeof window === 'undefined'
9+
const noop = () => {}
10+
let Console = console || { error: noop, warn: noop, log: noop }
911

1012
let defaultConfig = {
1113
retry: 3,
@@ -20,6 +22,10 @@ let defaultConfig = {
2022
useErrorBoundary: undefined, // this will default to the suspense value
2123
}
2224

25+
export function setConsole(c) {
26+
Console = c
27+
}
28+
2329
const onWindowFocus = () => {
2430
const { refetchAllOnWindowFocus } = defaultConfig
2531

@@ -33,7 +39,7 @@ const onWindowFocus = () => {
3339
}
3440
},
3541
}).catch(error => {
36-
console.error(error.message)
42+
Console.error(error.message)
3743
})
3844
}
3945
}
@@ -444,7 +450,7 @@ export function useQuery(queryKey, queryFn, config = {}) {
444450
try {
445451
query.fetch()
446452
} catch (err) {
447-
console.error(err)
453+
Console.error(err)
448454
// Swallow this error, since it is handled elsewhere
449455
}
450456
}
@@ -506,7 +512,7 @@ export function useQuery(queryKey, queryFn, config = {}) {
506512
try {
507513
await query.fetch()
508514
} catch (err) {
509-
console.error(err)
515+
Console.error(err)
510516
// Swallow this error. Don't rethrow it into a render function
511517
}
512518
}
@@ -580,7 +586,7 @@ export async function prefetchQuery(queryKey, queryFn, config = {}) {
580586
// Trigger a query subscription with one-time unique id
581587
const unsubscribeFromQuery = query.subscribe({
582588
id: uid++,
583-
onStateUpdate: () => {},
589+
onStateUpdate: noop,
584590
})
585591

586592
// Trigger a fetch and return the promise
@@ -667,7 +673,7 @@ export function useMutation(
667673
try {
668674
await doRefetchQueries()
669675
} catch (err) {
670-
console.error(err)
676+
Console.error(err)
671677
// Swallow this error since it is a side-effect
672678
}
673679
}
@@ -791,7 +797,7 @@ function defaultQueryKeySerializerFn(queryKey) {
791797
const variablesIsObject = isObject(variables)
792798

793799
if (typeof id !== 'string' || (variables && !variablesIsObject)) {
794-
console.warn('Tuple queryKey:', queryKey)
800+
Console.warn('Tuple queryKey:', queryKey)
795801
throw new Error(
796802
`Invalid query key tuple type: [${typeof id}, and ${typeof variables}]`
797803
)

0 commit comments

Comments
 (0)