Skip to content

Commit

Permalink
🚧Add try catch around JSON parsing (#39)
Browse files Browse the repository at this point in the history
* Start work for issue #31

* enhance: catch json parsing errors and print friendly error msg

* ci: catch json parse errors

* deps,refactor: replace crayon dependency with built in deno log colors

* refactor: change name of github console log functions

* config: add deno task to reload cache

* deps: update deno lock
  • Loading branch information
CalvinWilkinson authored Jun 27, 2024
1 parent 52cb19c commit 264b759
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 564 deletions.
25 changes: 16 additions & 9 deletions .github/cicd/core/VersionPuller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,31 @@ export class VersionPuller {
const configFiles = [...entries].map((entry) => entry);

if (configFiles.length === 0) {
const errorMsg = `No '${this.denoConfig}' files found.`;
Utils.printNotice(errorMsg);
const errorMsg = `::error::No '${this.denoConfig}' files found.`;
Utils.printGitHubNotice(errorMsg);
Deno.exit(1);
}

const fileName = configFiles[0].name;
const filePath = configFiles[0].path;

const fileData = Deno.readTextFileSync(filePath);
const jsonObj = JSON.parse(fileData);

// If the object contains a property with the name version
if (jsonObj.version === undefined) {
const errorMsg = `The file '${fileName}' does not contain a version property.`;
Utils.printError(errorMsg);
try {
const jsonObj = JSON.parse(fileData);

// If the object contains a property with the name version
if (jsonObj.version === undefined) {
const errorMsg = `::error::The file '${fileName}' does not contain a version property.`;
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

return jsonObj.version;
} catch (error) {
const errorMsg = `There was a problem parsing the file '${fileName}'.\n${error.message}`;
console.log(`::error::${errorMsg}`);
Deno.exit(1);
}

return jsonObj.version;
}
}
12 changes: 6 additions & 6 deletions .github/cicd/scripts/check-release-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ const scriptFileName = new URL(import.meta.url).pathname.split("/").pop();
const versionType = (Deno.env.get("RELEASE_TYPE") ?? "").trim().toLowerCase();

if (versionType === "") {
Utils.printError(`The 'RELEASE_TYPE' environment variable is require.\n\tFileName: ${scriptFileName}`);
Utils.printGitHubError(`The 'RELEASE_TYPE' environment variable is require.\n\tFileName: ${scriptFileName}`);
Deno.exit(1);
}

let version = (Deno.env.get("VERSION") ?? "").trim().toLowerCase();

if (version === "") {
Utils.printError(`The 'VERSION' environment variable is require.\n\tFileName: ${scriptFileName}`);
Utils.printGitHubError(`The 'VERSION' environment variable is require.\n\tFileName: ${scriptFileName}`);
Deno.exit(2);
}

if (versionType != "production" && versionType != "preview") {
Utils.printError(`The version type must be either 'preview' or 'release' but received '${versionType}'.`);
Utils.printGitHubError(`The version type must be either 'preview' or 'release' but received '${versionType}'.`);
Deno.exit(200);
}

Expand All @@ -28,14 +28,14 @@ let releaseNotesDirName = "";

if (versionType === "preview") {
if (Utils.isNotValidPreviewVersion(version)) {
Utils.printError(`The preview version '${version}' is not valid.`);
Utils.printGitHubError(`The preview version '${version}' is not valid.`);
Deno.exit(300);
}

releaseNotesDirName = "preview-releases";
} else if (versionType === "production") {
if (Utils.isNotValidProdVersion(version)) {
Utils.printError(`The production version '${version}' is not valid.`);
Utils.printGitHubError(`The production version '${version}' is not valid.`);
Deno.exit(400);
}

Expand Down Expand Up @@ -63,6 +63,6 @@ const configFiles = [...entries]

if (configFiles.length === 0) {
const errorMsg = `The release notes '${releaseNotesFileName}' file could not be found.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}
16 changes: 11 additions & 5 deletions .github/cicd/scripts/deno-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { existsSync } from "https://deno.land/std@0.224.0/fs/exists.ts";
import { Guards } from "../../../src/core/guards.ts";
import { DenoBuildSettings } from "./deno-build-settings.ts";
import { checkAll } from "./deno-check.ts";
import { crayon } from "https://deno.land/x/crayon@3.3.3/mod.ts";
import { ConsoleLogColor } from "../../../src/core/console-log-color.ts";

let arg = (Deno.args[0] ?? "").trim();

Expand All @@ -23,9 +23,15 @@ const getSettings = (settingsFilePath: string): DenoBuildSettings | undefined=>

const settingJsonData = Deno.readTextFileSync(settingsFilePath);

const settings = JSON.parse(settingJsonData);

return validSettingsObj(settings) ? settings : undefined;
try {
const settings = JSON.parse(settingJsonData);

return validSettingsObj(settings) ? settings : undefined;
} catch (error) {
const errorMsg = `There was a problem parsing the file '${settingsFilePath}'.\n${error.message}`;
console.log(errorMsg);
Deno.exit(1);
}
}

const validSettingsObj = (settingsObj: unknown): settingsObj is DenoBuildSettings => {
Expand All @@ -48,7 +54,7 @@ const settings = getSettings(settingsFilePath);

const ignores = settings?.ignoreExpressions.map((expression) => new RegExp(expression));

console.log(crayon.cyan(`Checking all files in '${Deno.cwd()}' . . .\n`));
ConsoleLogColor.cyan(`Checking all files in '${Deno.cwd()}' . . .\n`);

const results = await checkAll(Deno.cwd(), {
noNpm: false,
Expand Down
2 changes: 1 addition & 1 deletion .github/cicd/scripts/get-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const outputFilePath = Deno.env.get("GITHUB_OUTPUT") ?? "";

if (outputFilePath === "") {
const errorMsg = `The environment variable 'GITHUB_OUTPUT' does not exist.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

Expand Down
22 changes: 11 additions & 11 deletions .github/cicd/scripts/version-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ const ownerName = (Deno.env.get("OWNER_NAME") ?? "").trim();

if (ownerName === "") {
const errorMsg = `The 'OWNER_NAME' environment variable is required.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

const repoName = (Deno.env.get("REPO_NAME") ?? "").trim();

if (repoName === "") {
const errorMsg = `The 'REPO_NAME' environment variable is required.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

const versionType = (Deno.env.get("VERSION_TYPE") ?? "").trim().toLowerCase();

if (versionType === "") {
const errorMsg = `The 'VERSION_TYPE' environment variable is required.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

let version = (Deno.env.get("VERSION") ?? "").trim().toLowerCase();

if (version === "") {
const errorMsg = `The 'VERSION' environment variable is required.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

Expand All @@ -41,44 +41,44 @@ const userCLient: UsersClient = new UsersClient(ownerName, repoName, token);

if (!await userCLient.userExists(ownerName)) {
const errorMsg = `The user '${ownerName}' does not exist.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

const repoClient: RepoClient = new RepoClient(ownerName, repoName, token);

if (!await repoClient.exists()) {
const errorMsg = `The repository '${repoName}' does not exist.`;
Utils.printError(errorMsg);
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

const tagClient: TagClient = new TagClient(ownerName, repoName, token);

if (await tagClient.tagExists(version)) {
const errorMsg = `The tag '${version}' already exists.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

if (versionType != "preview" && versionType != "production") {
const errorMsg = `The version type '${versionType}' is not valid. Valid values are 'preview' or 'production' version type.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}

// Verify that the version is a valid preview or production version
if (versionType === "preview") {
if (Utils.isNotValidPreviewVersion(version)) {
const errorMsg = `The version '${version}' is not valid. Please provide a valid preview version.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}
} else if (versionType === "production") {
if (Utils.isNotValidProdVersion(version)) {
const errorMsg = `The version '${version}' is not valid. Please provide a valid production version.`;
Utils.printError(errorMsg);
Utils.printGitHubError(errorMsg);
Deno.exit(1);
}
}
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"version": "v1.0.0-preview.3",
"tasks": {
"build": "./.github/cicd/scripts/deno-build.ts",
"tests": "deno test ./Tests/*Tests.ts"
"tests": "deno test ./Tests/*Tests.ts",
"reload-cache": "deno cache --reload --lock=deno.lock --lock-write \"./deps.ts\""
},
"lint": {
"include": [
Expand Down
Loading

0 comments on commit 264b759

Please sign in to comment.