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

How to catch runSync() exception #47

Open
mickaelmartinsdev opened this issue Feb 26, 2018 · 5 comments
Open

How to catch runSync() exception #47

mickaelmartinsdev opened this issue Feb 26, 2018 · 5 comments
Labels

Comments

@mickaelmartinsdev
Copy link

Hi,
I want to catch 'exception' when TypeHelper throws exception. How I can do that ?

try{ testSync.runSync(); }catch(error) { console.log('error : '+ error); }

Thank-you

@vegarringdal
Copy link
Member

the throwOnError options just exits node process with error 1, not 0 (success), to make something like travis to react.
To know if error happend during sync the easier, to stop transpiling/nw build, just skip the throwOn options and check how many errors.

var TypeHelper = require('./dist/commonjs/index.js').TypeHelper

// sync run
let checkerSync = TypeHelper({
    tsConfig: './tsconfig.json',
    basePath: './',
    tsLint: './tslint.json',
    name: 'checkerSync'
});
let noErrors = checkerSync.runSync()
if (noErrors) {
    console.log('on no, I have: ' + noErrors + ' errors');
} else {
    console.log('all OK :-)');
}

@mickaelmartinsdev
Does this help you issue ?

@jpike88
Copy link
Contributor

jpike88 commented Apr 21, 2018

Documentation needs to be clearer for standard test cases

@jpike88
Copy link
Contributor

jpike88 commented Jan 19, 2021

@vegarringdal actually this is a big issue now I'm trying to pipe error output to somewhere else.

Returning a number isn't enough, it should be an array of strings. If length 0, then 0 errors.

@vegarringdal
Copy link
Member

I didnt see this before now.
We need to create a another function so we dont make a breaking change

@vegarringdal
Copy link
Member

image

added a getresult function

https://github.com/fuse-box/fuse-box-typechecker/blob/master/src/index.ts#L80-L83

please make a PR with edits to this function with the array data you would like to see

export function getResult(options: ITypeCheckerOptions, errors: IResults) {
// get the lint errors messages
const tsErrorMessages: TypeCheckError[] = processTsDiagnostics(options, errors);
// group by filename
let groupedErrors: { [k: string]: TypeCheckError[] } = {};
tsErrorMessages.forEach((error: TypeCheckError) => {
if (!groupedErrors[error.fileName]) {
groupedErrors[error.fileName] = [] as TypeCheckError[];
}
groupedErrors[error.fileName].push(error);
});
let allErrors = Object.entries(groupedErrors).map(([fileName, errors]) => {
const short = options.shortenFilenames !== false ? true : false;
const fullFileName = path.resolve(fileName);
let shortFileName = fullFileName.split(options.basePath as string).join('.');
if (path.isAbsolute(shortFileName)) {
// most likely a tsconfig path
shortFileName = path.relative(process.cwd(), fullFileName);
} else {
// if somepne passes in basepath we need to use that in print
if (options.basePathSetup) {
shortFileName = path.join(options.basePathSetup, shortFileName);
}
}
return (
` <cyan><bold><underline>${shortFileName}</underline></bold></cyan> <grey> - ${errors.length} errors.</grey>\n` +
errors
.map((err: TypeCheckError) => {
const fName = short ? shortFileName : fullFileName;
let text = `<yellow> - ${fName}:${err.line}:${err.char}</yellow><dim> (${
(<ITSError>err).category
}</dim><dim> ${(<ITSError>err).code})</dim><dim> ${
(<ITSError>err).message
}</dim>`;
return text;
})
.join(END_LINE)
);
});
return allErrors;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants