export class HttpError extends Error {
	constructor(kind, message = '请求失败', payload = null) {
		super(message)
		this.name = 'HttpError'
		this.kind = kind
		this.payload = payload
	}

	static fromParsed(parsed) {
		const map = {
			silent_fail: 'SILENT',
			biz_fail: 'BUSINESS',
			http_error: 'HTTP',
			passthrough: 'UNKNOWN'
		}
		return new HttpError(map[parsed.kind] || 'UNKNOWN', parsed.message, parsed.res)
	}

	static fromNetwork(err) {
		return new HttpError('NETWORK', err?.errMsg || '网络异常', err)
	}

	static fromUpload(res) {
		return new HttpError('UPLOAD', '上传失败', res)
	}
}
