Class @ember/debug

Module: @ember/debug

Show:

Module: @ember/debug

Available since v1.0.0

description
String
Describes the expectation. This will become the text of the Error thrown if the assertion fails.
condition
Any
Must be truthy for the assertion to pass. If falsy, an exception will be thrown.

Verify that a certain expectation is met, or throw a exception otherwise.

This is useful for communicating assumptions in the code to other human readers as well as catching bugs that accidentally violates these expectations.

Assertions are removed from production builds, so they can be freely added for documentation and debugging purposes without worries of incuring any performance penalty. However, because of that, they should not be used for checks that could reasonably fail during normal usage. Furthermore, care should be taken to avoid accidentally relying on side-effects produced from evaluating the condition itself, since the code will not run in production.

1
2
3
4
5
6
7
import { assert } from '@ember/debug';

// Test for truthiness
assert('Must pass a string', typeof str === 'string');

// Fail unconditionally
assert('This code path should never be run');
Module: @ember/debug
message
String
A debug message to display.

Display a debug notice.

Calls to this function are removed from production builds, so they can be freely added for documentation and debugging purposes without worries of incuring any performance penalty.

1
2
3
import { debug } from '@ember/debug';

debug('I\'m a debug notice!');
Module: @ember/debug

Available since v1.0.0

message
String
A description of the deprecation.
test
Boolean
A boolean. If falsy, the deprecation will be displayed.
options
Object

Display a deprecation warning with the provided message and a stack trace (Chrome and Firefox only).

  • In a production build, this method is defined as an empty function (NOP). Uses of this method in Ember itself are stripped from the ember.prod.js build.
Module: @ember/debug

Available since v2.1.0

handler
Function
A function to handle deprecation calls.

Allows for runtime registration of handler functions that override the default deprecation behavior. Deprecations are invoked by calls to @ember/debug/deprecate. The following example demonstrates its usage by registering a handler that throws an error if the message contains the word "should", otherwise defers to the default handler.

1
2
3
4
5
6
7
8
9
10
import { registerDeprecationHandler } from '@ember/debug';

registerDeprecationHandler((message, options, next) => {
  if (message.indexOf('should') !== -1) {
    throw new Error(`Deprecation message with should: ${message}`);
  } else {
    // defer to whatever handler was registered before this one
    next(message, options);
  }
});

The handler function takes the following arguments:

  • message - The message received from the deprecation call.
  • options - An object passed in with the deprecation call containing additional information including:
    • id - An id of the deprecation in the form of package-name.specific-deprecation.
    • until - The Ember version number the feature and deprecation will be removed in.
  • next - A function that calls into the previously registered handler.
Module: @ember/debug

Available since v2.1.0

handler
Function
A function to handle warnings.

Allows for runtime registration of handler functions that override the default warning behavior. Warnings are invoked by calls made to @ember/debug/warn. The following example demonstrates its usage by registering a handler that does nothing overriding Ember's default warning behavior.

1
2
3
4
import { registerWarnHandler } from '@ember/debug';

// next is not called, so no warnings get the default behavior
registerWarnHandler(() => {});

The handler function takes the following arguments:

  • message - The message received from the warn call.
  • options - An object passed in with the warn call containing additional information including:
    • id - An id of the warning in the form of package-name.specific-warning.
  • next - A function that calls into the previously registered handler.
Module: @ember/debug

Available since v1.5.0

func
Function
The function to be executed.

Run a function meant for debugging.

Calls to this function are removed from production builds, so they can be freely added for documentation and debugging purposes without worries of incuring any performance penalty.

1
2
3
4
5
6
7
8
9
10
import Component from '@ember/component';
import { runInDebug } from '@ember/debug';

runInDebug(() => {
  Component.reopen({
    didInsertElement() {
      console.log("I'm happy");
    }
  });
});
Module: @ember/debug

Available since v1.0.0

message
String
A warning to display.
test
Boolean
An optional boolean. If falsy, the warning will be displayed.
options
Object
An object that can be used to pass a unique `id` for this warning. The `id` can be used by Ember debugging tools to change the behavior (raise, log, or silence) for that specific warning. The `id` should be namespaced by dots, e.g. "ember-debug.feature-flag-with-features-stripped"

Display a warning with the provided message.

  • In a production build, this method is defined as an empty function (NOP). Uses of this method in Ember itself are stripped from the ember.prod.js build.
1
2
3
4
5
6
7
import { warn } from '@ember/debug';
import tomsterCount from './tomster-counter'; // a module in my project

// Log a warning if we have more than 3 tomsters
warn('Too many tomsters!', tomsterCount <= 3, {
  id: 'ember-debug.too-many-tomsters'
});