Files
mayfly-go/mayfly_go_web/src/common/assert.ts

68 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 不符合业务断言错误
*/
class AssertError extends Error {
constructor(message: string) {
super(message);
// 错误类名
this.name = 'AssertError';
}
}
/**
* 断言表达式为true
*
* @param condition 条件表达式
* @param msg 错误消息
*/
export function isTrue(condition: boolean, msg: string) {
if (!condition) {
throw new AssertError(msg);
}
}
/**
* 断言不能为空值即null,0,''等
*
* @param obj 对象1
* @param msg 错误消息
*/
export function notBlank(obj: any, msg: string) {
isTrue(obj, msg);
}
/**
* 断言两对象相等
*
* @param obj1 对象1
* @param obj2 对象2
* @param msg 错误消息
*/
export function isEquals(obj1: any, obj2: any, msg: string) {
isTrue(obj1 === obj2, msg);
}
/**
* 断言对象不为null或undefiend
*
* @param obj 对象
* @param msg 错误提示
*/
export function notNull(obj: any, msg: string) {
if (obj == null || obj == undefined) {
throw new AssertError(msg);
}
}
/**
* 断言字符串不能为空
*
* @param str 字符串
* @param msg 错误提示
*/
export function notEmpty(str: string, msg: string) {
if (str == null || str == undefined || str == '') {
throw new AssertError(msg);
}
}