Last active
June 29, 2023 06:17
-
-
Save ethaizone/ceb81ac341868581284e256c73c1ad9d to your computer and use it in GitHub Desktop.
Sentry middleware for Redux toolkit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FYI. I added this as history data but I don't use it on actual project because `createAsyncThunk` did serialize on error object. | |
import { isRejected, isRejectedWithValue } from '@reduxjs/toolkit' | |
import type { AnyAction, Middleware, MiddlewareAPI } from '@reduxjs/toolkit' | |
import * as Sentry from '@sentry/react' | |
/** | |
* Log a error and send to sentry | |
*/ | |
export const sentryMiddleware: Middleware = | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
(store: MiddlewareAPI) => (next) => (action: AnyAction) => { | |
if (isRejected(action) || isRejectedWithValue(action)) { | |
const { error, ...reduxAction } = action | |
Sentry.captureException(error.message, { | |
tags: { | |
action: 'redux', | |
}, | |
extra: { | |
error: error, | |
reduxAction, | |
}, | |
}) | |
} | |
return next(action) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment