Skip to content

Instantly share code, notes, and snippets.

@jgautheron
Created May 10, 2017 14:52
Show Gist options
  • Select an option

  • Save jgautheron/044b88307d934d486f59ae87c5a5a5a0 to your computer and use it in GitHub Desktop.

Select an option

Save jgautheron/044b88307d934d486f59ae87c5a5a5a0 to your computer and use it in GitHub Desktop.

Revisions

  1. jgautheron created this gist May 10, 2017.
    42 changes: 42 additions & 0 deletions errorHandler.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    import ReactUpdates from 'react-dom/lib/ReactUpdates'
    import ReactDefaultBatchingStrategy from 'react-dom/lib/ReactDefaultBatchingStrategy'
    import 'isomorphic-fetch'

    const logError = (err, extra = {}) => {
    fetch('/logger', {
    method: 'POST',
    credentials: 'same-origin',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
    message: err.message,
    callstack: err.stack,
    stamp: new Date(),
    userAgent: typeof navigator !== 'undefined' ? navigator.userAgent : 'server',
    ...extra,
    }),
    })
    }

    let isHandlingError = false
    const ReactTryCatchBatchingStrategy = {
    get isBatchingUpdates() { return ReactDefaultBatchingStrategy.isBatchingUpdates },

    batchedUpdates(...args) {
    try {
    ReactDefaultBatchingStrategy.batchedUpdates(...args)
    } catch (e) {
    if (isHandlingError) {
    throw e
    }

    isHandlingError = true
    try {
    logError(e)
    } finally {
    isHandlingError = false
    }
    }
    },
    }

    ReactUpdates.injection.injectBatchingStrategy(ReactTryCatchBatchingStrategy)
    4 changes: 4 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    server.post('/logger', (req, res) => {
    console.log(req.body)
    res.sendStatus(204)
    })