# Namespace goog.asserts

code »

## Classes

goog.asserts.AssertionError
  Error object for failed assertions.

Show:Public Protected Private

## Global Functions

code »goog.asserts.DEFAULT_ERROR_HANDLER ( e )

The default error handler.

Parameters

e: `!goog.asserts.AssertionError`
  The exception to be handled.

code »`<T>` goog.asserts.assert ( condition, opt_message, var_args ) ⇒ `T`

Checks if the condition evaluates to true if goog.asserts.ENABLE_ASSERTS is true.

Parameters

condition: `T`
  The condition to check.
opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Returns

The value of the condition.

Throws

`goog.asserts.AssertionError`
  When the condition evaluates to false.

code »goog.asserts.assertArray ( value, opt_message, var_args ) ⇒ `!Array`

Checks if the value is an Array if goog.asserts.ENABLE_ASSERTS is true.

Parameters

value: `*`
  The value to check.
opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Returns

The value, guaranteed to be a non-null array.

Throws

`goog.asserts.AssertionError`
  When the value is not an array.

code »goog.asserts.assertBoolean ( value, opt_message, var_args ) ⇒ `boolean`

Checks if the value is a boolean if goog.asserts.ENABLE_ASSERTS is true.

Parameters

value: `*`
  The value to check.
opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Returns

The value, guaranteed to be a boolean when asserts are enabled.

Throws

`goog.asserts.AssertionError`
  When the value is not a boolean.

code »goog.asserts.assertElement ( value, opt_message, var_args ) ⇒ `!Element`

Checks if the value is a DOM Element if goog.asserts.ENABLE_ASSERTS is true.

Parameters

value: `*`
  The value to check.
opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Returns

The value, likely to be a DOM Element when asserts are enabled.

Throws

`goog.asserts.AssertionError`
  When the value is not a boolean.

code »goog.asserts.assertFunction ( value, opt_message, var_args ) ⇒ `!Function`

Checks if the value is a function if goog.asserts.ENABLE_ASSERTS is true.

Parameters

value: `*`
  The value to check.
opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Returns

The value, guaranteed to be a function when asserts enabled.

Throws

`goog.asserts.AssertionError`
  When the value is not a function.

code »`<T>` goog.asserts.assertInstanceof ( value, type, opt_message, var_args ) ⇒ `!T`

Checks if the value is an instance of the user-defined type if goog.asserts.ENABLE_ASSERTS is true. The compiler may tighten the type returned by this function.

Parameters

value: `*`
  The value to check.
type: `function(new: T, ...)`
  A user-defined constructor.
opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Throws

`goog.asserts.AssertionError`
  When the value is not an instance of type.

code »goog.asserts.assertNumber ( value, opt_message, var_args ) ⇒ `number`

Checks if the value is a number if goog.asserts.ENABLE_ASSERTS is true.

Parameters

value: `*`
  The value to check.
opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Returns

The value, guaranteed to be a number when asserts enabled.

Throws

`goog.asserts.AssertionError`
  When the value is not a number.

code »goog.asserts.assertObject ( value, opt_message, var_args ) ⇒ `!Object`

Checks if the value is an Object if goog.asserts.ENABLE_ASSERTS is true.

Parameters

value: `*`
  The value to check.
opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Returns

The value, guaranteed to be a non-null object.

Throws

`goog.asserts.AssertionError`
  When the value is not an object.

code »goog.asserts.assertObjectPrototypeIsIntact ( )

Checks that no enumerable keys are present in Object.prototype. Such keys would break most code that use `for (var ... in ...)` loops.

code »goog.asserts.assertString ( value, opt_message, var_args ) ⇒ `string`

Checks if the value is a string if goog.asserts.ENABLE_ASSERTS is true.

Parameters

value: `*`
  The value to check.
opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Returns

The value, guaranteed to be a string when asserts enabled.

Throws

`goog.asserts.AssertionError`
  When the value is not a string.

code »goog.asserts.doAssertFailure_ ( defaultMessage, defaultArgs, givenMessage, givenArgs )

Throws an exception with the given message and "Assertion failed" prefixed onto it.

Parameters

defaultMessage: `string`
  The message to use if givenMessage is empty.
defaultArgs: `Array`
  The substitution arguments for defaultMessage.
givenMessage: `(string|undefined)`
  Message supplied by the caller.
givenArgs: `Array`
  The substitution arguments for givenMessage.

Throws

`goog.asserts.AssertionError`
  When the value is not a number.

code »goog.asserts.errorHandler_ ( )

The handler responsible for throwing or logging assertion errors.

code »goog.asserts.fail ( opt_message, var_args )

Fails if goog.asserts.ENABLE_ASSERTS is true. This function is useful in case when we want to add a check in the unreachable area like switch-case statement:

      switch(type) {
        case FOO: doSomething(); break;
        case BAR: doSomethingElse(); break;
        default: goog.assert.fail('Unrecognized type: ' + type);
          // We have only 2 types - "default:" section is unreachable code.
      }

Parameters

opt_message: `string=`
  Error message in case of failure.
var_args: `...*`
  The items to substitute into the failure message.

Throws

`goog.asserts.AssertionError`
  Failure.

code »goog.asserts.setErrorHandler ( errorHandler )

Sets a custom error handler that can be used to customize the behavior of assertion failures, for example by turning all assertion failures into log messages.

Parameters

errorHandler

## Compiler Constants

code »goog.asserts.ENABLE_ASSERTS : `boolean`