import { Octokit } from "@octokit/rest" ; import util from "util" ; import * as objectTools from "../helpers/object-tools" ; import { Context } from "./context" ; /** DevError incidates a Prettifier bug */ export class DevError extends Error { context : Context ; readonly cause : Error ; constructor ( activity , cause , context = { } ) { super ( activity ) ; this . cause = cause ; this . context = context ; } /** adds missing fields from the given context to this error */ enrich ( additionalContext ) { this . context = { ... additionalContext , ... this . context } ; } } /** logs the given developer error as a GitHub issue */ export async function logDevError ( err ) { console . log ( `${ err . context . org }|${ err . context . repo }: DevError ${ err . message }` ) ; objectTools . removeKeys ( err , "octokit" ) ; const octokit = new Octokit ( { auth : process . env . GITHUBOPS_TOKEN } ) ; await octokit . issues . create ( { owner : "kevgo" , repo : "prettifier-ops" , title : `Bug report: ${ err . message }` , body : bodyTemplate ( err ) , } ) ; } export function bodyTemplate ( err ) { return `
<details>
  <summary>error</summary>

  \`\`\`
  ${ util . inspect ( err , true , Infinity ) }
  \`\`\`
</details>

<details>
  <summary>cause</summary>

  \`\`\`
  ${ util . inspect ( err . cause , true , Infinity ) }
  \`\`\`
</details>

<details>
  <summary>context</summary>

  \`\`\`
  ${ util . inspect ( err . context , true , Infinity ) }
  \`\`\`
</details>

<details>
  <summary>stack</summary>

  \`\`\`
  ${ err . stack }

  ${ err . cause . stack }
  \`\`\`
</details>
` . substr ( 0 , 60000 ) ; }