Add more error handling
This commit is contained in:
parent
0c836dab9a
commit
d4025dd32d
116
src/index.js
116
src/index.js
@ -55,60 +55,72 @@ async function main() {
|
||||
let uberJSON = []
|
||||
|
||||
let paginationOption = {}
|
||||
while (true) {
|
||||
let res = await fetch(
|
||||
"https://drivers.uber.com/earnings/api/getWebActivityFeed?localeCode=en",
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
startDateIso: options.startDateIso ?? "2023-04-01",
|
||||
endDateIso: options.endDateIso ?? "2023-04-31",
|
||||
paginationOption,
|
||||
}),
|
||||
headers: usefulRequestHeaders,
|
||||
},
|
||||
)
|
||||
let body = await res.json()
|
||||
let trips = body?.data?.activities
|
||||
?.map(async (activity) => {
|
||||
if (activity.formattedTotal == "$0.00") {
|
||||
return null
|
||||
}
|
||||
if (activity.type == "QUEST") {
|
||||
// These are all duplicates of MISC items.
|
||||
return null
|
||||
}
|
||||
if (activity.type == "MISC" || activity.activityTitle == "Delivery") {
|
||||
return {
|
||||
uuid: activity.uuid,
|
||||
recognizedAt: new Date(
|
||||
(activity.recognizedAt ?? 1) * 1000,
|
||||
).toISOString(),
|
||||
pickupAddress: activity.tripMetaData?.pickupAddress,
|
||||
dropOffAddress: activity.tripMetaData?.dropOffAddress,
|
||||
total: activity.formattedTotal,
|
||||
type: activity.activityTitle,
|
||||
try {
|
||||
while (true) {
|
||||
let res = await fetch(
|
||||
"https://drivers.uber.com/earnings/api/getWebActivityFeed?localeCode=en",
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
startDateIso: options.startDateIso ?? "2023-04-01",
|
||||
endDateIso: options.endDateIso ?? "2023-04-31",
|
||||
paginationOption,
|
||||
}),
|
||||
headers: usefulRequestHeaders,
|
||||
},
|
||||
)
|
||||
let body = await res.json()
|
||||
let trips = body?.data?.activities
|
||||
?.map(async (activity) => {
|
||||
if (activity.formattedTotal == "$0.00") {
|
||||
return null
|
||||
}
|
||||
}
|
||||
if (activity.type == "TRIP" || activity.type == "CT") {
|
||||
// Trip or Share
|
||||
return await processTrip(activity, usefulRequestHeaders)
|
||||
}
|
||||
return { unparsedActivity: activity }
|
||||
})
|
||||
.filter((item) => item)
|
||||
if (trips) {
|
||||
let tripResults = await utils.settlePromises(trips)
|
||||
uberJSON = [...uberJSON, ...tripResults]
|
||||
} else {
|
||||
let failedRequest = { failedRequest: res, body }
|
||||
console.error(failedRequest)
|
||||
uberJSON = [...uberJSON, failedRequest]
|
||||
if (activity.type == "QUEST") {
|
||||
// These are all duplicates of MISC items.
|
||||
return null
|
||||
}
|
||||
if (
|
||||
activity.type == "MISC" ||
|
||||
activity.activityTitle == "Delivery"
|
||||
) {
|
||||
return {
|
||||
uuid: activity.uuid,
|
||||
recognizedAt: new Date(
|
||||
(activity.recognizedAt ?? 1) * 1000,
|
||||
).toISOString(),
|
||||
pickupAddress: activity.tripMetaData?.pickupAddress,
|
||||
dropOffAddress: activity.tripMetaData?.dropOffAddress,
|
||||
total: activity.formattedTotal,
|
||||
type: activity.activityTitle,
|
||||
}
|
||||
}
|
||||
if (activity.type == "TRIP" || activity.type == "CT") {
|
||||
// Trip or Share
|
||||
try {
|
||||
return await processTrip(activity, usefulRequestHeaders)
|
||||
} catch (error) {
|
||||
console.error({ error, activity })
|
||||
return { unparsedActivity: activity, error }
|
||||
}
|
||||
}
|
||||
return { unparsedActivity: activity }
|
||||
})
|
||||
.filter((item) => item)
|
||||
if (trips) {
|
||||
let tripResults = await utils.settlePromises(trips)
|
||||
uberJSON = [...uberJSON, ...tripResults]
|
||||
} else {
|
||||
let failedRequest = { failedRequest: res, body }
|
||||
console.error(failedRequest)
|
||||
uberJSON = [...uberJSON, failedRequest]
|
||||
}
|
||||
if (!body.data?.pagination?.hasMoreData) {
|
||||
break
|
||||
}
|
||||
paginationOption.cursor = body.data.pagination.nextCursor
|
||||
}
|
||||
if (!body.data?.pagination?.hasMoreData) {
|
||||
break
|
||||
}
|
||||
paginationOption.cursor = body.data.pagination.nextCursor
|
||||
} catch (err) {
|
||||
console.error("Mostly critical failure", err)
|
||||
}
|
||||
fs.writeFileSync("./uberResults.json", JSON.stringify(uberJSON))
|
||||
} catch (err) {
|
||||
|
Loading…
Reference in New Issue
Block a user