Skip to content

Commit

Permalink
feat(app): do error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhck committed Jul 13, 2019
1 parent f77e3f1 commit b451084
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,34 @@ export class App {
}

private static async handleEvent(context: Context<TData>): Promise<void> {
const body = App.getBody(context.payload);
const templateVars = App.getTemplateVars(context.payload);
const newBody = App.getCompiledBody(body, templateVars);
if (App.isPr(context.payload)) {
await context.github.pulls.update(context.issue({body: newBody}));
context.log.debug("updated PR body");
} else {
await context.github.issues.update(context.issue({body: newBody}));
context.log.debug("updated ISSUE body");
try {
const body = App.getBody(context.payload);
const templateVars = App.getTemplateVars(context.payload);
const newBody = App.getCompiledBody(body, templateVars);
if (App.isPr(context.payload)) {
await context.github.pulls.update(context.issue({body: newBody}));
context.log.debug("updated PR body");
} else {
await context.github.issues.update(context.issue({body: newBody}));
context.log.debug("updated ISSUE body");
}
} catch (e) {
await context.github.issues.createComment(context.issue({body: this.getErrorComment(e)}));
}
}

private static getErrorComment(error: Error): string {
const message = (error.stack) ? error.stack!.split("\n").join("\n>") : error.toString();
return `## There was error processing your body
The exact error message is the following
${message}
This body won't be processed any further, please fix your template.
`;
}

private static getTemplateVars(data: TData): ITemplateVars {
return App.isPr(data)
? App.getPrVars(data)
Expand Down

0 comments on commit b451084

Please sign in to comment.