Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add logger for error-catalog errors
- Loading branch information
Showing
10 changed files
with
2,167 additions
and
2,256 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import chalk from 'chalk'; | ||
import { getReasonPhrase } from 'http-status-codes'; | ||
import {ProblemError} from "@snyk/error-catalog-nodejs-public"; | ||
|
||
export function isErrorOfTypeProblemError( | ||
error: Error | ProblemError, | ||
): error is ProblemError { | ||
return (error as ProblemError)?.isErrorCatalogError === true; | ||
} | ||
function successLogger(message: string) { | ||
console.log('🙌', chalk.bgGreen(' Awesome, No issues were found. ')); | ||
console.log(message); | ||
} | ||
function errorLoggerFactory(logLevel: 'info' | 'warn' | 'error') { | ||
const createLogger = (color: string) => ( | ||
error: ProblemError | Error, | ||
callback?: (err: Error) => void, | ||
) => { | ||
if (isErrorOfTypeProblemError(error)) { | ||
const jsonResponse = error.toJsonApiErrorObject(); | ||
let output = `${chalk[color](`${logLevel.toUpperCase()} `)} ${chalk[ | ||
color | ||
](` ${jsonResponse.title} `)} (${jsonResponse.code}) \nInfo: ${ | ||
jsonResponse.detail | ||
} \nHTTP: ${jsonResponse.status} ${getReasonPhrase( | ||
jsonResponse.status, | ||
)}`; | ||
if (jsonResponse.links) { | ||
output += `\nHelp: ${jsonResponse.links.about}`; | ||
} | ||
return output; | ||
} | ||
if (callback) { | ||
return callback(error); | ||
} | ||
}; | ||
const loglevelColorMap = { | ||
info: 'bgBlue', | ||
warn: 'bgYellow', | ||
error: 'bgRed', | ||
}; | ||
return createLogger(loglevelColorMap[logLevel]); | ||
} | ||
export const cliOutputFormatter = { | ||
error: errorLoggerFactory('error'), | ||
warn: errorLoggerFactory('warn'), | ||
info: errorLoggerFactory('info'), | ||
success: successLogger, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters