How to use the fail function from assert-plus

Find comprehensive JavaScript assert-plus.fail code examples handpicked from public code repositorys.

assert-plus.fail is a function that throws an error if a given condition is not met, based on the Node.js assert module.

588
589
590
591
592
593
594
595
596

        cb(new restify.errors.PreconditionFailedError(
            'Cannot sync when in migration phase: ' + phase));
        return;
    default:
        assert.fail(format('Unvalidated vm migration action: %s',
            action));
        break;
}
fork icon20
star icon11
watch icon0

831
832
833
834
835
836
837
838
839
840

        if (changed.indexOf(vmuuid) === -1) {
            changed.push(vmuuid);
        }
    } else {
        assert.fail('unknown action: ' + vmdiff[idx].action);
    }
}

self.log.trace({
fork icon3
star icon3
watch icon0

How does assert-plus.fail work?

assert-plus.fail is a function that is used to throw an error if a given condition is not met. It is part of the assert-plus module, which is a Node.js module that extends the built-in assert module and provides additional assertions and utilities for writing defensive JavaScript code. The function takes two arguments: a message that will be included in the error if the assertion fails, and an optional error constructor function that will be used to create the error object. If the assertion fails, the function will throw an error object created by calling the provided error constructor function with the message string. For example, suppose you want to ensure that a variable x is a positive integer: javascript Copy code {{{{{{{ const assert = require('assert-plus'); function doSomething(x) { assert.number(x, 'x must be a number'); assert.ok(x > 0, 'x must be positive'); // ... rest of the function } Here, we use assert.number to ensure that x is a number, and assert.ok to ensure that it is positive. If either assertion fails, the corresponding error message will be included in the error that is thrown. assert-plus.fail is a useful function for ensuring that the input to a function meets certain requirements, and can help to catch errors early in the development process.

538
539
540
541
542
543
544
545
546
547
                break;
            case 'gauge':
                obj._handle.set(value);
                break;
            default:
                assert.fail('Unexpected type: ' + type);
                break;
        }
    }, cb);
}
fork icon3
star icon0
watch icon0

649
650
651
652
653
654
655
656
657
658
659
// eslint-disable-next-line
test('handleUncaughtExceptions should not call handler for internal errors', function(t) {
    SERVER.get('/', function(req, res, next) {
        // This route is not used for the test but at least one route needs to
        // be registered to Restify in order for routing logic to be run
        assert.fail('should not run');
    });


    SERVER.on('uncaughtException', function throwError(err) {
        t.ifError(err);
fork icon1
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const assert = require("assert-plus");

function doSomething(x) {
  if (typeof x !== "string") {
    assert.fail(`x must be a string, but was ${typeof x}`);
  }

  // ... rest of the function
}

doSomething(42); // throws an error

In this example, the doSomething function expects its x parameter to be a string. If the x parameter is not a string, the function uses assert.fail to throw an error with a message indicating that x must be a string. When we call doSomething with the argument 42, the assert.fail statement is executed, and an error is thrown with the message "x must be a string, but was number". This can help to catch programming errors early in the development process and make it easier to debug code.

57
58
59
60
61
62
63
64
65
66
67
68
    else if (typeof (arg) === 'number')
        return space(arg);
    else if (typeof (arg) === 'string')
        return arg;
    else
        assert.fail('invalid "' + name + '": not a string or number: ' + arg);
}




/**
fork icon0
star icon0
watch icon1

+ 9 other calls in file