️ HTTPResponseError

This commit is contained in:
yeyang
2023-02-07 12:45:55 +08:00
parent 3afaacbad0
commit e98518747d
2 changed files with 29 additions and 15 deletions

View File

@@ -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 Errorreason${reason[1]}`)
})
return await fetch(url, options)
.then(res => checkStatus(res))
.catch(err => {
logger.error(err)
throw Error(`Request Get Errorreason${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 Errorreason${err.message.match(/reason:(.*)/)[1]}`)
})
return await fetch(url, options)
.then(res => checkStatus(res))
.catch(err => {
logger.error(err)
throw Error(`Request Post Errorreason${err.message.match(/reason:(.*)/)[1]}`)
})
}
/**

View File

@@ -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')