unierror.uts 1.8 KB
Newer Older
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
import { RequestErrorCode, RequestFail, UploadFileFail, DownloadFileFail } from "./interface.uts"

/**
 * 错误主题
 */
export const UniErrorSubject = 'uni-request';
/**
 * 错误码
 * @UniError
 */
export const UniErrors : Map<RequestErrorCode, string> = new Map([

	/**
	 * 接口超时
	 */
	[5, 'time out'],
	/**
	 * 服务端系统错误
	 */
	[1000, 'server system error'],
	/**
	 * json数据解析错误
	 */
	[100001, 'invalid json'],
	/**
	 * 错误信息json解析失败
	 */
	[100002, 'error message invalid json'],
	/**
	 * 未知的网络错误
	 */
	[600000, 'unknown network error'],
	/**
	 * 网络中断
	 */
	[600003, 'network interrupted error'],
	/**
	 * URL 格式不合法
	 */
	[600009, 'invalid URL'],
	/**
	 * 请求的 data 序列化失败
	 */
	[600010, 'invalid request data'],

	/**
	 * request系统错误
	 */
	[602001, 'request system error']
]);


export function getErrcode(errCode : number) : RequestErrorCode {
	const res = UniErrors[errCode];
	return res == null ? 602001 : errCode;
}


export class RequestFailImpl extends UniError implements RequestFail {
	constructor(errCode : RequestErrorCode) {
		super();
		this.errSubject = UniErrorSubject;
		this.errCode = errCode;
		this.errMsg = UniErrors[errCode] ?? "";
	}
}


export class UploadFileFailImpl extends UniError implements UploadFileFail {
	constructor(errCode : RequestErrorCode) {
		super();
		this.errSubject = "uni-uploadFile";
		this.errCode = errCode;
		this.errMsg = UniErrors[errCode] ?? "";
	}
}


export class DownloadFileFailImpl extends UniError implements DownloadFileFail {
	constructor(errCode : RequestErrorCode) {
		super();
		this.errSubject = "uni-downloadFile";
		this.errCode = errCode;
		this.errMsg = UniErrors[errCode] ?? "";
	}
}