可选options: ErrorOptions可选cause稳定错误码;业务分支应比较该字段,不要比较 message。
可选data可选调试数据;结构由具体错误码决定,可能随 Host 版本变化。
可选stack静态stackThe Error.stackTraceLimit property specifies the number of stack frames
collected by a stack trace (whether generated by new Error().stack or
Error.captureStackTrace(obj)).
The default value is 10 but may be set to any valid JavaScript number. Changes
will affect any stack trace captured after the value has been changed.
If set to a non-number value, or set to a negative number, stack traces will not capture any frames.
静态captureCreates a .stack property on targetObject, which when accessed returns
a string representing the location in the code at which
Error.captureStackTrace() was called.
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`
The first line of the trace will be prefixed with
${myObject.name}: ${myObject.message}.
The optional constructorOpt argument accepts a function. If given, all frames
above constructorOpt, including constructorOpt, will be omitted from the
generated stack trace.
The constructorOpt argument is useful for hiding implementation
details of error generation from the user. For instance:
function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();
可选constructorOpt: Function静态isIndicates whether the argument provided is a built-in Error instance or not.
静态prepare
SDK 对外抛出的标准 bridge / runtime 错误类型。
备注
该错误用于表示握手失败、bridge 调用失败、父容器运行时错误等 非 HTTP 结果错误。其
code字段可用于业务分支处理,data仅作为可选调试信息,不保证结构稳定。错误码是跨 Host 的分支契约;message只用于日志和开发者提示,不应作为机器判断条件。常见处理类别包括:
INVALID_PARAMS(修正调用参数)、PERMISSION_NOT_IN_SNAPSHOT(当前运行快照缺少权限,需发布含声明的新版本或重新打开调试)、PERMISSION_DENIED(检查权限启用状态与平台批准)、USER_GESTURE_REQUIRED(改为用户点击后重试)、AUTHORIZATION_CANCELLED(结束当前流程),以及METHOD_FORBIDDEN(当前 Host 不支持该能力)。具体能力还可能返回模块专属错误码。 旧 Runtime 仍可能返回PERMISSION_NOT_DECLARED;SDK 会保留 Host 返回的错误码。 不要把所有错误都当作可重试错误,也不要把data当成稳定的序列化协议。示例
} catch (error) { if (error instanceof HbMiniProgramSDKError && error.code === 'USER_GESTURE_REQUIRED') { showRetryButton() return } throw error }