assert.ifError(value)


版本历史
版本变更
v10.0.0

现在,它不再抛出原始错误,而是封装到包含完整堆栈跟踪的 [AssertionError][AssertionError] 中。

v10.0.0

值现在可能仅为 undefinednull。之前所有的假值都与 null 一样处理并且没有抛出错误。

v0.1.97

新增于: v0.1.97

如果 value 不是 undefinednull,则抛出 value。这在回调中测试 error 参数时很有用。堆栈跟踪包含来自传给 ifError() 的错误的所有帧,包括 ifError() 本身的潜在新帧。

\Throws value if value is not undefined or null. This is useful when testing the error argument in callbacks. The stack trace contains all frames from the error passed to ifError() including the potential new frames for ifError() itself.

import assert from 'node:assert/strict';
assert.ifError(null);
// OK
assert.ifError(0);
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
assert.ifError('error');
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
assert.ifError(new Error());
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error
// Create some random error frames.
let err;
(function errorFrame() {
 err = new Error('test error');
})();
(function ifErrorFrame() {
 assert.ifError(err);
})();
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
// at ifErrorFrame
// at errorFrameconst assert = require('node:assert/strict');
assert.ifError(null);
// OK
assert.ifError(0);
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
assert.ifError('error');
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
assert.ifError(new Error());
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error
// Create some random error frames.
let err;
(function errorFrame() {
 err = new Error('test error');
})();
(function ifErrorFrame() {
 assert.ifError(err);
})();
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
// at ifErrorFrame
// at errorFrame

AltStyle によって変換されたページ (->オリジナル) /