Files
RSSHub/lib/utils/got.js
Chih-Hsuan Yen 1c9c4ccfc8 fix(core): make sure timeout error messages include URLs (#7981)
Before this fix, timeout messages are not quite useful

> error: Request undefined fail, retry attempt #1: TimeoutError: Timeout awaiting 'request' for 5000ms
2021-08-12 01:27:07 -07:00

41 lines
1.1 KiB
JavaScript

const logger = require('./logger');
const config = require('@/config').value;
const got = require('got');
const custom = got.extend({
retry: config.requestRetry,
hooks: {
beforeRetry: [
(options, err, count) => {
logger.error(`Request ${options.url} fail, retry attempt #${count}: ${err}`);
},
],
afterResponse: [
(response) => {
try {
response.data = JSON.parse(response.body);
} catch (e) {
response.data = response.body;
}
response.status = response.statusCode;
return response;
},
],
init: [
(options) => {
// compatible with axios api
if (options && options.data) {
options.body = options.body || options.data;
}
},
],
},
headers: {
'user-agent': config.ua,
},
timeout: config.requestTimeout,
});
custom.all = (list) => Promise.all(list);
module.exports = custom;