Skip to content

Commit

Permalink
improve github error logging (#384)
Browse files Browse the repository at this point in the history
* improve logging

* update package-lock

* refactored getErrorMessage

* bump version

* added default
  • Loading branch information
shubhbapna authored Feb 10, 2023
1 parent 8306396 commit a5bc40d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 35 deletions.
111 changes: 80 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kie/build-chain-action",
"version": "3.0.13",
"version": "3.0.14",
"description": "Library to execute commands based on github projects dependencies.",
"main": "dist/index.js",
"author": "",
Expand Down Expand Up @@ -57,6 +57,7 @@
"@actions/core": "^1.8.2",
"@actions/exec": "^1.1.1",
"@actions/glob": "^0.3.0",
"@octokit/request-error": "^3.0.3",
"@kie/build-chain-configuration-reader": "^3.0.10",
"@octokit/rest": "^18.12.0",
"@octokit/types": "^6.39.0",
Expand Down
37 changes: 34 additions & 3 deletions src/service/git/github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LoggerService } from "@bc/service/logger/logger-service";
import { logAndThrow } from "@bc/utils/log";
import { Octokit } from "@octokit/rest";
import { Endpoints } from "@octokit/types";
import { RequestError } from "@octokit/request-error";
import Container, { Service } from "typedi";

@Service()
Expand All @@ -29,7 +30,12 @@ export class GithubAPIService {
await this.octokit.repos.getBranch({ owner, repo, branch });
return true;
} catch (err) {
this.logger.warn(`project github.com/${owner}/${repo}:${branch} does not exist. It's not necessarily an error.`);
this.logger.warn(
this.getErrorMessage(
err,
`project github.com/${owner}/${repo}:${branch} does not exist. It's not necessarily an error.`
)
);
return false;
}
}
Expand Down Expand Up @@ -64,7 +70,8 @@ export class GithubAPIService {
if (head) {
msg += `&head=${head}`;
}
this.logger.error(msg);

this.logger.error(this.getErrorMessage(err, msg));
throw err;
}
}
Expand Down Expand Up @@ -106,8 +113,32 @@ export class GithubAPIService {
throw new NotFoundError();
}
} catch (err) {
this.logger.error(`Error getting fork name for ${targetOwner}/${repo} where owner is ${sourceOwner}`);
this.logger.error(
this.getErrorMessage(
err,
`Error getting fork name for ${targetOwner}/${repo} where owner is ${sourceOwner}`
)
);
throw err;
}
}

private getErrorMessage(err: unknown, msg: string): string {
let reason;
if (err instanceof RequestError) {
switch (err.status) {
case 401:
reason = "Failed to authenticate with provided token, please use -token argument to provide a new one. You can also check your GITHUB_TOKEN environment variable and check whether the provided token is still valid.";
break;
case 404:
reason = "Failed to fetch GitHub URL, please check if the URL used in -url argument is valid and if the token you are using have permissions to access it.";
break;
case 403:
reason = "Failed to fetch resource. Either your github token does not have access to the requested resource or you have reached your github api rate limit.";
break;
default: // let reason be undefined for all other codes
}
}
return reason ? `${msg} Reason: ${reason}` : msg;
}
}

0 comments on commit a5bc40d

Please sign in to comment.