error.js 1.1 KB
Newer Older
VK1688's avatar
VK1688 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/**
 * 全局错误码
 */
const ERROR = {
	50403: 50403,
	// 参数错误
	51001: 51001,
	51002: 51002,
	51003: 51003,
	51004: 51004,
	51005: 51005,
	51006: 51006,
	51007: 51007,
	51008: 51008,
	51009: 51009,
	51010: 51010,
	51011: 51011,
	// 数据不存在
	52001: 52001,
	52002: 52002,
	// 运行错误
	53001: 53001,
	53002: 53002,
	53003: 53003,
	53004: 53004,
	53005: 53005,
}

const errSubject = "uni-pay";

function isUniPayError(errCode) {
	return Object.values(ERROR).includes(errCode);
}

class UniCloudError extends Error {
	constructor(options = {}) {
		super(options.message);
		this.errMsg = options.message || '';
		this.code = this.errCode = options.code;
		this.errSubject = options.subject || errSubject;
		this.forceReturn = options.forceReturn || false;
		this.cause = options.cause;
	}

	toJson(level = 0) {
		if (level >= 10) {
			return
		}
		level++
		return {
			errCode: this.errCode,
			errMsg: this.errMsg,
			code: this.errCode,
			message: this.message,
			errSubject: this.errSubject,
			cause: this.cause && this.cause.toJson ? this.cause.toJson(level) : this.cause
		}
	}
}

module.exports = {
	ERROR,
	isUniPayError,
	UniCloudError
}