import SentryScopeProxy from "./sentry-scope-proxy" ; import { GitHubAPI , Octokit } from "probot" ; export default class OctokitError extends Error { sentryScope : SentryScopeProxy ; requestOptions : Octokit . HookOptions ; httpError : Octokit . HookError ; constructor ( httpError , requestOptions ) { super ( `${ requestOptions . method } ${ requestOptions . url } responded with ${ httpError . status }` ) ; this . name = this . constructor . name ; this . httpError = httpError ; this . requestOptions = requestOptions ; this . sentryScope = new SentryScopeProxy ( ) ; this . sentryScope . extra . request = this . requestMetadata ( ) ; this . sentryScope . extra . response = this . responseMetadata ( ) ; this . sentryScope . fingerprint = this . generateFingerprint ( ) ; } get responseCode ( ) { return this . httpError . status ; } static wrapRequestErrors ( githubApi ) { githubApi . hook . wrap ( "request" , async ( request , options ) => { try { return await request ( options ) ; } catch ( error ) { throw new OctokitError ( error , options ) ; } } ) ; } requestMetadata = ( ) => ( { method : this . requestOptions . method , path : this . requestOptions . url , headers : this . requestOptions . headers } ) ; responseMetadata = ( ) => ( { code : this . responseCode , body : this . deserializeMessage ( this . httpError . message ) , headers : this . httpError . headers } ) ; generateFingerprint = ( ) => [ "{{ default }}" , this . requestOptions . method , this . requestOptions . url , this . responseCode ] ; deserializeMessage ( message ) { try { return JSON . parse ( message ) ; } catch ( error ) { if ( error . name !== "SyntaxError" ) { throw error ; } } return message ; } }