title: "WP_CLI::error()" post_status: publish comment_status: open taxonomy: category: - wp-cli-handbook post_tag: - Internal Api - Repos - Data


WP_CLI::error()

显示以“错误:”为前缀的错误信息并退出脚本。


用法

WP_CLI::error( $message, $exit = true )
$message (string|WP_Error|Exception|Throwable) 写入 STDERR 的消息。
$exit (boolean|integer) 为 true 时默认执行 exit(1)。
@return (null)

说明

错误信息会写入 STDERR。默认情况下会以返回码 1 终止脚本执行。

当需要允许脚本继续执行时,请改用 WP_CLI::warning()

启用 --debug 参数时,此方法还会输出显示错误触发位置的回溯信息,便于定位问题代码。

# `wp cache flush` 将刷新失败视为致命错误。
if ( false === wp_cache_flush() ) {
    WP_CLI::error( 'The object cache could not be flushed.' );
}

内部 API 文档会在每次发布时根据 WP-CLI 代码库生成。如需改进建议,请提交拉取请求。

相关