Skip to content

Commit

Permalink
added invariant util
Browse files Browse the repository at this point in the history
Added new issue URL logic. Closes #2216
  • Loading branch information
imolorhe committed Jul 1, 2023
1 parent 635f08f commit 1bb570f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
1 change: 0 additions & 1 deletion packages/altair-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"memoizee": "0.4.14",
"mousetrap": "1.6.5",
"mousetrap-global-bind": "1.1.0",
"new-github-issue-url": "0.2.1",
"ng-zorro-antd": "12.1.1",
"ngrx-store-localstorage": "12.0.1",
"ngx-contextmenu": "6.0.0",
Expand Down
14 changes: 14 additions & 0 deletions packages/altair-app/src/app/modules/altair/utils/invariant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const prefix = 'Invariant failed';

class InvariantError extends Error {
framesToPop = 1;
}

export const invariant = (condition: any, message?: string) => {
if (condition) {
return;
}

const value = message ? `${prefix}: ${message}` : prefix;
throw new InvariantError(value);
};
53 changes: 36 additions & 17 deletions packages/altair-app/src/app/modules/altair/utils/issue/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { environment } from 'environments/environment';
import newGithubIssueUrl from 'new-github-issue-url';
import { truncateText } from '..';
import { UnknownError } from '../../interfaces/shared';
import {
issueTemplate,
ISSUE_TEMPLATE_ALTAIR_VERSION_PLACEHOLDER,
ISSUE_TEMPLATE_ERROR_MESSAGE_PLACEHOLDER,
ISSUE_TEMPLATE_ERROR_STACK_PLACEHOLDER,
} from './template';
import { issueErrorTemplate } from './template';

const getErrorMessage = (error: UnknownError) => {
if (!error) {
Expand Down Expand Up @@ -37,19 +31,44 @@ const getErrorStack = (error: UnknownError) => {
export const getIssueUrl = (error: UnknownError) => {
const errorMessage = getErrorMessage(error);
const issueTitle = `Bug: Application error: ${truncateText(errorMessage)}`;
const issueBody = issueTemplate
.replaceAll(ISSUE_TEMPLATE_ERROR_MESSAGE_PLACEHOLDER, errorMessage)
.replaceAll(ISSUE_TEMPLATE_ERROR_STACK_PLACEHOLDER, getErrorStack(error))
.replaceAll(ISSUE_TEMPLATE_ALTAIR_VERSION_PLACEHOLDER, environment.version);

const issueUrl = newGithubIssueUrl({
user: 'imolorhe',
const url = githubIssueUrl({
org: 'altair-graphql',
repo: 'altair',
title: issueTitle,
body: issueBody,
labels: ['bug-report'],
template: 'Bug_report.md',
template: 'bug_report.yaml',
bodyFields: {
'current-behavior': issueErrorTemplate(
errorMessage,
getErrorStack(error)
),
'expected-behavior': 'No errors should occur',
environment: `- OS: \n- Browser: \n- Platform: \n- Version: ${environment.version}`,
},
});

return issueUrl;
return url;
};

interface IssueUrlOptions {
org: string;
repo: string;
title: string;
template: string;
bodyFields: Record<string, string>;
}
const githubIssueUrl = (options: IssueUrlOptions) => {
const { org, repo, title, template, bodyFields } = options;

const url = `https://github.com/${org}/${repo}/issues/new`;
const params = new URLSearchParams({
title,
template,
...bodyFields,
});

const u = new URL(url);
u.search = params.toString();

return u.toString();
};
10 changes: 10 additions & 0 deletions packages/altair-app/src/app/modules/altair/utils/issue/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ ${ISSUE_TEMPLATE_ERROR_STACK_PLACEHOLDER}
Version ${ISSUE_TEMPLATE_ALTAIR_VERSION_PLACEHOLDER}
`;

export const issueErrorTemplate = (message: string, stack: string) => `
Error message: ${message}
Error stack:
\`\`\`
${stack}
\`\`\`
`;

0 comments on commit 1bb570f

Please sign in to comment.