ErrorManager

public class ErrorManager : @unchecked Sendable

错误异常捕获类

JJException AvoidCrash

  • 自定义需要捕获未定义方法异常的类,默认[NSNull, NSNumber, NSString, NSArray, NSDictionary]

    Declaration

    Swift

    public static var captureClasses: [AnyClass] { get set }
  • 自定义需要捕获的Signal字典,默认[SIGABRT, SIGSEGV, SIGBUS, SIGTRAP, SIGILL]

    Declaration

    Swift

    public static var captureSignals: [Int32 : String] { get set }
  • 可选tryCatch句柄,需应用自行桥接ObjC实现,默认nil; 目前仅startCapture方法可选使用、PlayerCache 13.4以下系统可选使用,可不处理

    ObjC桥接代码示例:

    // ObjCBridge.h
    NS_ASSUME_NONNULL_BEGIN
    @interface ObjCBridge : NSObject
    + (void)tryCatch:(void (NS_NOESCAPE ^)(void))block exceptionHandler:(void (NS_NOESCAPE ^)(NSException *exception))exceptionHandler;
    @end
    NS_ASSUME_NONNULL_END
    
    // ObjCBridge.m
    @implementation ObjCBridge
    + (void)tryCatch:(void (NS_NOESCAPE ^)(void))block exceptionHandler:(void (NS_NOESCAPE ^)(NSException * _Nonnull))exceptionHandler {
        @try {
            if (block) block();
        } @catch (NSException *exception) {
            if (exceptionHandler) exceptionHandler(exception);
        }
    }
    @end
    

    swift绑定代码示例:

    ErrorManager.tryCatchHandler = { ObjCBridge.tryCatch($0, exceptionHandler: $1) }
    

    Declaration

    Swift

    public static var tryCatchHandler: (@Sendable (_ block: () -> Void, _ exceptionHandler: (NSException) -> Void) -> Void)? { get set }
  • 开启框架错误捕获功能,默认仅处理captureClasses崩溃保护

    Declaration

    Swift

    public static func startCapture(captureException: Bool = false, captureSignal: Bool = false)

    Parameters

    captureException

    是否捕获全局Exception错误

    captureSignal

    是否捕获全局Signal错误

  • 停止框架错误捕获功能

    Declaration

    Swift

    public static func stopCapture()
  • 捕获自定义错误并在当前线程发送通知,可设置备注

    Declaration

    Swift

    public static func captureError(
        _ error: Error,
        crashed: Bool = false,
        remark: String? = nil,
        function: String = #function,
        file: String = #file,
        line: Int = #line
    )
  • 将NSException异常转换为Error

    Declaration

    Swift

    public static func error(with exception: NSException) -> Error
  • 安全执行ObjC桥接tryCatch代码块,失败时抛Error,详见tryCatchHandler

    Declaration

    Swift

    public static func tryCatch(_ block: () -> Void) throws
  • 安全执行ObjC桥接tryCatch代码块,失败时调用exceptionHandler,详见tryCatchHandler

    Declaration

    Swift

    public static func tryCatch(_ block: () -> Void, exceptionHandler: (NSException) -> Void)