From 05e244b3fcec22d2aaa33eca8adad070e218ee0c Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Mon, 13 Mar 2023 21:16:10 -0400 Subject: [PATCH] fix(plugins/traffic): display scope errors --- source/plugins/traffic/index.mjs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/source/plugins/traffic/index.mjs b/source/plugins/traffic/index.mjs index 12563e35..e5b3a775 100644 --- a/source/plugins/traffic/index.mjs +++ b/source/plugins/traffic/index.mjs @@ -16,7 +16,15 @@ export default async function({login, imports, data, rest, q, account}, {enabled //Get views stats from repositories console.debug(`metrics/compute/${login}/plugins > traffic > querying api`) const views = {count: 0, uniques: 0} - const response = [...await Promise.allSettled(repositories.map(({repo, owner}) => imports.filters.repo(`${owner}/${repo}`, skipped) ? rest.repos.getViews({owner, repo}) : {}))].filter(({status}) => status === "fulfilled").map(({value}) => value) + const promised = [...await Promise.allSettled(repositories.map(({repo, owner}) => imports.filters.repo(`${owner}/${repo}`, skipped) ? rest.repos.getViews({owner, repo}) : {}))] + const response = promised.filter(({status}) => status === "fulfilled").map(({value}) => value) + + //Handle error if all promises were rejected + if (promised.filter(({status}) => status === "rejected").length === promised.length) { + if (promised.map(({reason}) => reason.message).every(error => /must have push access to repository/i.test(error))) + throw {error: {message: "Insufficient token scopes"}} + throw new Error(promised[0].reason.message) + } //Compute views console.debug(`metrics/compute/${login}/plugins > traffic > computing stats`)