How to use the ifError function from assert

Find comprehensive JavaScript assert.ifError code examples handpicked from public code repositorys.

The assert.ifError method throws an error if the input argument is truthy.

80
81
82
83
84
85
86
87
88
89
  return err instanceof Error && err.message === 'test' && err.caught;
});

const p = require('./napi_child').spawnSync(
  process.execPath, [__filename, 'fatal', bindingPath]);
assert.ifError(p.error);
assert.ok(p.stderr.toString().includes(
  'FATAL ERROR: Error::ThrowFatalError This is a fatal error'));

assert.throws(() => binding.error.throwDefaultError(false),
fork icon457
star icon0
watch icon52

55
56
57
58
59
60
61
62
63
64
it('should load /config with csrf_token', (done) => {
    request({
        url: `${nconf.get('url')}/api/config`,
        json: true,
    }, (err, response, body) => {
        assert.ifError(err);
        assert.equal(response.statusCode, 200);
        assert(body.csrf_token);
        done();
    });
fork icon1
star icon0
watch icon5

+ 213 other calls in file

How does assert.ifError work?

The assert.ifError method is a utility function in the built-in assert module of Node.js. The method takes a single argument and checks whether it is "truthy". If the argument is truthy, meaning it evaluates to true in a boolean context, the method throws an AssertionError with the argument as the error message. If the argument is falsy, meaning it evaluates to false, null, undefined, 0, NaN, or an empty string in a boolean context, the method does nothing and returns undefined. The purpose of this method is to simplify error handling in situations where a value is expected to be falsy but may not be. Instead of writing an if statement to check the value and throw an error if necessary, the assert.ifError method provides a shorthand that can be used in a single line of code. For example, if a function returns a value that is expected to be falsy, we can use assert.ifError to throw an error if the value is truthy: javascript Copy code {{{{{{{ const myFunc = () => { const result = calculateValue(); // Throw an error if the result is truthy assert.ifError(result); // Continue with other logic // ... }; In this example, we call a function called calculateValue that returns a value. We use assert.ifError to check whether the value is truthy, and throw an error if it is. If the value is falsy, the function continues with its other logic. Overall, the assert.ifError method provides a simple way to handle error conditions where a value is expected to be falsy but may not be, and helps to reduce the amount of boilerplate code needed for error handling.

119
120
121
122
123
124
125
126
127
128
  }
  if('workers' in options) {
    genOptions.workers = options.workers;
  }
  RSA.generateKeyPair(genOptions, function(err, pair) {
    ASSERT.ifError(err);
    _pairCheck(pair);
    callback(pair);
  });
}
fork icon0
star icon0
watch icon1

2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
describe('update', function() {
  describe('updateMany', function() {
    it('works', function(done) {
      mquery(col).updateMany({ name: 'exec' }, { name: 'test' }).
        exec(function(error) {
          assert.ifError(error);
          mquery(col).count({ name: 'test' }).exec(function(error, res) {
            assert.ifError(error);
            assert.equal(res, 2);
            done();
fork icon0
star icon0
watch icon1

+ 74 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const assert = require("assert");

const myFunc = (value) => {
  // Throw an error if the value is truthy
  assert.ifError(value);

  console.log("Value is falsy");
};

myFunc(null); // Output: Value is falsy
myFunc(undefined); // Output: Value is falsy
myFunc(0); // Output: Value is falsy
myFunc(""); // Output: Value is falsy

myFunc("error"); // Throws an AssertionError with message 'error'

In this example, we have defined a function called myFunc that takes a single argument value. We use assert.ifError to check whether value is truthy, and throw an error with the value as the error message if it is. We call myFunc multiple times with different input values, including null, undefined, 0, and an empty string. In each case, the function prints a message to the console indicating that the value is falsy. In the last call, we pass the string 'error' as the input value. Because this value is truthy, the assert.ifError method throws an AssertionError with the message 'error'. The error message is printed to the console, but the function does not continue with its other logic.

127
128
129
130
131
132
133
134
135
136
    });
});

it('should add a new field to an object', (done) => {
    db.setObjectField('testObject2', 'type', 'cat', function (err) {
        assert.ifError(err, null);
        assert(arguments.length < 2);
        done();
    });
});
fork icon0
star icon0
watch icon0

+ 32 other calls in file