Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added invariant util #2217

Merged
merged 1 commit into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
\`\`\`
`;
Loading