BBAJSBindingError.h 4.08 KB
//
//  BBAJSBindingError.h
//  SWAN
//
//  Created by baidu on 2018/8/2.
//  Copyright © 2018年 baidu. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <JavaScriptCore/JavaScriptCore.h>

NS_ASSUME_NONNULL_BEGIN

/**
 js 数据类型,有些类型有些重复定义,但是为了方便与OC类型对应
 */
typedef NS_OPTIONS(NSInteger, BBAJSDataType) {
    /// js Undefined, typeof === "undefined", Objective-C nil
    BBAJSDataTypeUndefined = (1 << 0),
    /// js Null, typeof === 'null' or "object", Objective-C NSNull
    BBAJSDataTypeNull = (1 << 1),
    /// js Boolean, typeof === "boolean", Objective-C NSNumber, 或者int/bool/double等数值类型
    BBAJSDataTypeBoolean = (1 << 2),
    /// js Number, typeof === "number", Objective-C NSNumber, 或者int/bool/double等数值类型
    BBAJSDataTypeNumber = (1 << 3),
    /// js String, typeof === "string", Objective-C NSString
    BBAJSDataTypeString = (1 << 4),
    /// js Array object, typeof === "object", Objective-C NSArray
    BBAJSDataTypeArrayObject = (1 << 5),
    /// js function object, typeof === "function", Objective-C block
    BBAJSDataTypeFunctionObject = (1 << 6),
    /// js Date object, typeof === "object", Objective-C NSDate
    BBAJSDataTypeDateObject = (1 << 7),
    /// js Any other object, typeof === "object", Objective-C id或者Class
    BBAJSDataTypeObject = (1 << 8),
    /// typedArray 虽然不是普通的JSArray类型,但是对于native来说,我们希望都转化成为array类型。 对于 imp层实现来说,普通的array和 typedArray 都会变成 NSArray,所以这里 重新 | BBAJSDataTypeArrayObject
    BBAJSDataTypeTypedArray = ((1 << 9) | (BBAJSDataTypeArrayObject)),
    /// JSBinding的对象,也是一种object,需要2步。1.[JSValue toObject].转成JSBinding对象,2. JSBinding对象,通过.impInstance属性,转化成imp层对象,所以需要 | BBAJSDataTypeDateObject
    BBAJSDataTypeJSBindingObject = ((1 << 10) | (BBAJSDataTypeDateObject)),
};

typedef NS_ENUM(NSUInteger, BBAJSErrorTypes) {
    BBAJSErrorTypesTypeError, // 数据类型错误
    BBAJSErrorTypesLogicError // 业务逻辑错误
};


@interface BBAJSBindingError : NSObject


/**
 生成默认的js Error,默认包含line、column、stack信息

 @param errorType 错误类型
 @param context 当前执行的JSContext
 @return js Error
 */
+ (JSValue *)defaultErrorWithErrorType:(BBAJSErrorTypes)errorType
                             jsContext:(JSContext *)context;


/**
 生成js Error,默认包含line、column、stack信息

 @param errorType 错误类型
 @param context 当前执行的JSContext
 @param message 错误信息
 @return js Error
 */
+ (JSValue *)errorType:(BBAJSErrorTypes)errorType
             jsContext:(JSContext *)context
          errorMessage:(NSString *_Nullable)message;


/**
 生成js Error,默认包含line、column、stack信息

 @param errorType 错误类型
 @param context 当前执行的JSContext
 @param name 参数名字
 @param message 错误信息
 @return 实际收到的js数据类型
 */
+ (JSValue *)errorType:(BBAJSErrorTypes)errorType
             jsContext:(JSContext *)context
          argumentName:(NSString *_Nullable)name
          errorMessage:(NSString *_Nullable)message;

/**
 生成js Error,默认包含line、column、stack信息

 @param errorType 错误类型
 @param context 当前执行的JSContext
 @param argumentName 参数名字
 @param message 错误信息
 @param expectedType 期望的js数据类型
 @param actualType 实际收到的js数据类型
 @return js Error
 */
+ (JSValue *)errorType:(BBAJSErrorTypes)errorType
             jsContext:(JSContext *)context
          argumentName:(NSString *_Nullable)argumentName
          errorMessage:(NSString *_Nullable)message
          expectedType:(BBAJSDataType)expectedType
            actualType:(BBAJSDataType)actualType;


/**
 统一error内容格式

 @param error 待统一格式的error;
 @param errorName 错误名称,既error[@"name"]字段,如果为nil或空串,则不会改动原值;
 */
+ (void)customStyleErrorOfError:(JSValue *)error name:(NSString *_Nullable)errorName;

@end

NS_ASSUME_NONNULL_END