Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import { Middleware, Store, Dispatch } from 'redux';
  2. import Raven from 'raven-js';
  3.  
  4. const loggerKey = '[redux-raven-middleware]';
  5.  
  6. export default (dsn, config = {}) => {
  7. if (!Raven.isSetup()) {
  8. if (!dsn) {
  9. // skip this middleware if there is no DSN (Data Source Name).
  10. console.error(`${loggerKey} Sentry DSN required.`);
  11. return store => next => action => next(action);
  12. }
  13. Raven.config(dsn, config).install();
  14. }
  15.  
  16. return ({ getState }: Store) => (next: Dispatch) => (action: any): any => {
  17. const state: IAppState = getState();
  18. try {
  19. return next(action);
  20. } catch (err) {
  21. try {
  22. const { authentication, settings: { endpoint } } = state;
  23.  
  24. if (authentication.isAuthenticated && authentication.user) {
  25. Raven.setUserContext({ email: authentication.user.email });
  26. } else {
  27. Raven.setUserContext();
  28. }
  29.  
  30. (Raven as any).setExtraContext({ disco_api_endpoint: endpoint });
  31. } catch (fatalErr) {
  32. console.error(`Unexpected fatal error in Raven middleware: ${fatalErr}`);
  33. }
  34.  
  35. console.error(`${loggerKey} Reporting error to Sentry:`, err);
  36.  
  37. // Send the report.
  38. Raven.captureException(err, { extra: { action, state } });
  39. }
  40. }
  41. }
  42.  
  43.  
  44.  
  45. /** WEBPACK FOOTER **
  46. ** ./~/tslint-loader!./src/app/store/middleware/raven.ts
  47. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement