docs: exception handler
Closed this issue · 4 comments
hildjj commented
I'm not sure this works as intended. I get a TypeScript error from the exception handler. The goal in that handler is being able to call format()
with type safety.
import { PeggySyntaxError, parse } from './arithmetics';
try {
const sampleOutput = parse('my sample...');
} catch (ex: PeggySyntaxError) {
// Handle parsing error
// [...]
}
pjmolina commented
siefkenj commented
Ah. This is very complicated, it seems, but I think I found a solution.
The issue is PeggySyntaxError
is not just a type, it's also a class that can be constructed.
@pjmolina Your fix is correct, but it needs to be exported both ways. That is, the generated code should be
export const PeggySyntaxError = peggyParser.SyntaxError as typeof _PeggySyntaxError;
export type PeggySyntaxError = _PeggySyntaxError;
so that the constructor and the type can both be exported.
I think DefaultTracer
will also have the same issue since it is exported the same way as PeggySyntaxError
.