Exception

// @namespace Exception
import { Exception } from 'rcfm'

Custom Types

@typedef {Object} ExceptionInstance
@description - 异常实例

@prop {string} [type = ""] - 异常类型
@prop {string} [name = type] - 异常名称
@prop {Object} [data = {}] - 异常数据
@prop {string} [message] - 异常消息

Exception Types

@type {ExceptionInstance}
@description - 不是合规的实例

@prop {string} type = "EXCEPTION_INVALID_INSTANCE"
@prop {string} [name = type]
@prop {Object} [data = {}]
@prop {string} [message]

Class Methods

.create()

创建一个异常实例。

// Exception.create(params)
// @param {Object} params - 异常定义
// @prop {string} [type = ""] - 异常类型
// @prop {string} [data = {}] - 异常数据
// @prop {string} [message] - 异常消息
// @returns {ExceptionInstance}

// @example
const exception = Exception.create({
  type: 'INVALID_OBJECT',
  data: {invalidKey: 'value'},
  message: "The input object is not valid."
})

.isException()

判断入参是否是一个异常实例。

// Exception.isException(arg)
// @param {*} arg - 任意参数
// @returns {boolean} - 当入参是 ExceptionInstance 时返回 true,否则返回 false

// @examples
Exception.isException(Exception.create()) // true
Exception.isException({}) // false

.isntException()

判断入参是否不是一个异常实例。

// Exception.isntException(arg)
// @param {*} arg - 任意参数
// @returns {boolean} - 当入参是 ExceptionInstance 时返回 false,否则返回 true

// @examples
Exception.isntException(Exception.create()) // false
Exception.isntException({}) // true

.mustbeException()

当入参不是一个异常实例时抛出异常。

// Exception.mustbeException(arg, message)
// @param {*} arg - 任意参数
// @param {string} [message] - 自定义异常对象消息
// @returns {undefined}

// @examples
Exception.mustbeException(Exception.create())
Exception.mustbeException({}) // Throws an EXCEPTION_INVALID_INSTANCE exception.

.raise()

创建一个异常实例并抛出。

// Exception.raise(params)
// @param {Object} params - 异常定义
// @prop {string} [type = ""] - 异常类型
// @prop {string} [data = {}] - 异常数据
// @prop {string} [message] - 异常消息
// @throws {ExceptionInstance}

// @example
Exception.raise({
  type: 'INVALID_OBJECT',
  data: {invalidKey: 'value'},
  message: "The input object is not valid."
}) // Throws an INVALID_OBJECT exception.