Build testable error handlers that don't throw in production...

Downloads in past


2.1.16 years ago6 years agoMinified + gzip package size for ember-test-friendly-error-handler in KB


In production, you often want to catch certain types of errors such as network errors (e.g. myModel.save().catch(() => this.showNetworkFailureMessage())) however these kinds of generic catch handlers can wreak havoc on your tests. In tests, most of the time you want these uncaught errors to actually fail your tests unless explicitly testing the generic catch handler behaviors (e.g. this.showNetworkFailureMessage).


ember install ember-test-friendly-error-handler


In your application code you would import the error handler generator, and invoke it with a descriptive label and your callback.


Ember.onerror is a hook that is invoked when an error is thrown by any code within the Ember run loop (e.g. {{action}}'s, component event methods, model hooks, etc). In practice, this is nearly all of your application code. Ember.onerror has the ability to "swallow" errors by handling them without rethrowing, and ultimately making the failure scenario impossible to detect while testing.
It is common for applications to leverage Ember.onerror to do error reporting and attempt to gracefully handle errors thrown within the application, and when possible prevent those errors from bubbling out and causing issues with the running application (or providing more detailed information when they do impact the app).
Without something like ember-test-friendly-error-handler, applications that implement Ember.onerror either have to replicate this addon's behavior, or are unable to properly test both the "production" mode (eg error swallowing) and development/testing mode (eg re-throw errors to make them possible to track down and fix).
Here is how an application might set this up:
// app/app.js
import Ember from 'ember';
import buildErrorHandler from 'ember-test-friendly-error-handler';

Ember.onerror = buildErrorHandler('Ember.onerror', (reason) => {
  // whatever else you might want here...
// ...existing `app/app.js` content goes here...


To generate a promise rejection handler (aka .catch handler) you might do something like:
import buildErrorHandler from 'ember-test-friendly-error-handler';

// ... snip ...
  .catch(buildErrorHandler('save-my-model', () => this.showNetworkFailureMessage()));


When you need to test the generic handler behavior (this.showNetworkFailureMessage() above), you need to disable the automatic error re-throwing behavior that ember-test-friendly-error-handler provides you so that your test more closely resembles your production environment.
A test that does this might look like:
import { module, test } from 'qunit';
import { 
} from 'ember-test-friendly-error-handler';

module('some good description', {
  afterEach() {

test('network failure message is displayed', function(assert) {

  triggerNetworkFailure();         // ⚡️
  return triggerModelSave()
    .then(() => {
      assertNetworkFailureShown(); // 😼


The following interface describes the ember-test-friendly-error-handler module's API:
export default function(label: string, callback: Function): Function;

// the following are only present when testing
export function squelchErrorHandlerFor(label: string): void;
export function unsquelchAllErrorHandlers(): void;



  • git clone <repository-url> this repository
  • cd ember-test-friendly-error-handler
  • npm install


Running Tests

  • npm test (Runs ember try:each to test your addon against multiple Ember versions)
  • ember test
  • ember test --server


  • ember build

For more information on using ember-cli, visit https://ember-cli.com/.