diff --git a/lib/request/request.js b/lib/request/request.js index 7ae79ab..f41592e 100644 --- a/lib/request/request.js +++ b/lib/request/request.js @@ -8,6 +8,22 @@ import _ from 'lodash' const CHROME_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36' const POSTMAN_UA = 'PostmanRuntime/7.29.0' +class HTTPResponseError extends Error { + constructor (response) { + super(`HTTP Error Response: ${response.status} ${response.statusText}`) + this.response = response + } +} + +const checkStatus = response => { + if (response.ok) { + // response.status >= 200 && response.status < 300 + return response + } else { + throw new HTTPResponseError(response) + } +} + export default new class { /** * @description: Get请求 @@ -33,11 +49,12 @@ export default new class { ...options.headers } if (!options.agent)options.agent = await this.getAgent() - return await fetch(url, options).catch(err => { - logger.error(err) - const reason = err.message.match(/reason:(.*)/) - throw Error(`Request Get Error,reason:${reason[1]}`) - }) + return await fetch(url, options) + .then(res => checkStatus(res)) + .catch(err => { + logger.error(err) + throw Error(`Request Get Error,reason:${err.message.match(/reason:(.*)/)[1]}`) + }) } /** @@ -57,10 +74,12 @@ export default new class { options.headers['Content-Type'] = 'application/json' } if (!options.agent)options.agent = await this.getAgent() - return await fetch(url, options).catch(err => { - logger.error(err) - throw Error(`Request Post Error,reason:${err.message.match(/reason:(.*)/)[1]}`) - }) + return await fetch(url, options) + .then(res => checkStatus(res)) + .catch(err => { + logger.error(err) + throw Error(`Request Post Error,reason:${err.message.match(/reason:(.*)/)[1]}`) + }) } /** diff --git a/model/Pixiv.js b/model/Pixiv.js index 3fc3c9b..78068eb 100644 --- a/model/Pixiv.js +++ b/model/Pixiv.js @@ -106,12 +106,7 @@ export default new class Pixiv { } // 请求api let api = `${this.domain}/rank` - let res = await request.get(api, { params }).then(res => res.json()).catch(err => console.log(err)) - - if (!res || res.error || lodash.isEmpty(res.illusts)) { - logger.mark('[椰奶Pixiv][排行榜]使用备用接口') - res = await request.get('https://api.obfs.dev/api/pixiv/rank', { params }).then(res => res.json()) - } + let res = await request.get(api, { params }).then(res => res.json()) if (res.error) throw Error(res.error.message) if (lodash.isEmpty(res.illusts)) throw Error('暂无数据,请等待榜单更新哦(。-ω-)zzz')