Logging Custom Error Fields with Rollbar and react-rollbar

Rollbar is a popular error monitoring service with solid React support via the react-rollbar package. Out of the box it captures exceptions and stack traces, but sometimes you need to surface custom fields from your own error classes. Here’s how to do it for unhandled errors caught by ErrorBoundary.

Setup

Install the packages:

npm install rollbar react-rollbar

Wrap your app with RollbarProvider:

import { RollbarProvider } from '@rollbar/react';

const rollbarConfig = {
  accessToken: 'YOUR_CLIENT_ACCESS_TOKEN',
  environment: 'production',
};

function App() {
  return (
    <RollbarProvider config={rollbarConfig}>
      <YourApp />
    </RollbarProvider>
  );
}

Custom Error Class

Define a custom error class that carries the extra fields you want to report:

// errors/UnknownError.js
export default class UnknownError extends Error {
  constructor(errorCode, message) {
    super(message);

    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, UnknownError);
    }

    this.name = 'UnknownError';
    this.errorCode = errorCode;
  }
}

Throw it anywhere in your component tree:

throw new UnknownError('PAYMENT_FAILED', 'Payment could not be processed');

Extracting Custom Fields with extra

The extra prop on ErrorBoundary accepts either a plain object or a function that receives the caught error and returns an object. This is the key to pulling fields off your custom error class:

import { ErrorBoundary } from '@rollbar/react';
import FallbackUI from './FallbackUI';

function AppBoundary({ children }) {
  return (
    <ErrorBoundary
      fallbackUI={FallbackUI}
      extra={err => ({
        email: err.email,
        errorCode: err.errorCode,
      })}
    >
      {children}
    </ErrorBoundary>
  );
}

When UnknownError is thrown, Rollbar will log the error with errorCode visible under the Custom tab in the dashboard. Fields that are undefined on a given error (e.g. a plain Error that has no errorCode) are simply omitted — so this pattern is safe to use as a top-level boundary for all error types.

More details in the official docs: Rollbar Browser JS Reference and rollbar-react.

Using Create React App with ASP.NET Core

Make sure you have already set ASP.NET Core app (Getting Started) and React.js app (Getting Started)

To use ASP.NET Core API from React.js app add option proxy to package.json

...
"proxy":"http://localhost:5000"
...

This option allow to proxy requests starts with /api to your API server (ASP.NET Core app).

More info about proxy option Proxying API Requests in Development

Start your apps

dotnet run
npm start

Full example of this approach AspNetCoreDemoApp

Ensure https for ASP.NET Core apps on Heroku

Update ConfigureServices method with:

Update Configure method with:

More info

Docker deployment using Bitbucket Pipelines and Heroku

Set HEROKU_TOKEN and APP_NAME at Settings -> Pipelines -> Environment variables.

bitbucket-pipelines.yml

deploy.sh

November update of my github projects

Dotnetcore Buildpack

dotnetcore-buildpack

  • Removed dotnet restore step
  • Extremely optimized slag size (DOTNET_SKIP_FIRST_TIME_EXPERIENCE:1)
  • Updated .NET Core SDK 2.0.3
  • Added support PROJECT_FILE and PROJECT_NAME environment variables (#23)

ASP.NET Core Demo App

AspNet5DemoApp

  • Updated npm modules and nuget packages
  • Added react-scripts
  • Added Dockerfile and License

Jincod.CQRS

Jincod.CQRS

  • Updated .NET Core to 2.0.0
  • Updated nuget package to 2.0.1

Build scripts

build-scripts

  • Fixed setup script
  • Added Cake.Bakery to pacakges.config

Strongly Typed Web API Example

Jincod.CQRS

  • Created project for demonstration creating API Controllers using interfaces of services with Refit attributes.