Skip to content

Instantly share code, notes, and snippets.

@ethaizone
Last active June 29, 2023 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethaizone/ceb81ac341868581284e256c73c1ad9d to your computer and use it in GitHub Desktop.
Save ethaizone/ceb81ac341868581284e256c73c1ad9d to your computer and use it in GitHub Desktop.
Sentry middleware for Redux toolkit
// 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