Skip to content

Commit 6e765c7

Browse files
committed
Add formatError helper.
1 parent 7ac7406 commit 6e765c7

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * from "./src/Config";
99

1010
export * from "./src/Utils/Map";
1111
export * from "./src/Utils/uniqueArray";
12+
export * from "./src/Utils/formatError";
1213

1314
export * from "./src/Schema/Definition";
1415
export * from "./src/Schema/Schema";

src/Utils/formatError.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as ts from "typescript";
2+
import * as path from "path";
3+
4+
import { BaseError } from "../Error/BaseError";
5+
import { DiagnosticError } from "../Error/DiagnosticError";
6+
import { UnknownNodeError } from "../Error/UnknownNodeError";
7+
8+
function getSourceFile(node: ts.Node): ts.SourceFile {
9+
let sourceFile: ts.Node = node.parent;
10+
while (sourceFile) {
11+
if (sourceFile.kind === ts.SyntaxKind.SourceFile) {
12+
return sourceFile as ts.SourceFile;
13+
}
14+
sourceFile = sourceFile.parent;
15+
}
16+
17+
return undefined;
18+
}
19+
20+
function getNodeLocation(node: ts.Node): [string, number, number] {
21+
const sourceFile: ts.SourceFile = getSourceFile(node);
22+
if (!sourceFile) {
23+
return ["<unknown file>", 0, 0];
24+
}
25+
26+
const lineAndChar: ts.LineAndCharacter = ts.getLineAndCharacterOfPosition(sourceFile, node.getStart(sourceFile));
27+
return [sourceFile.fileName, lineAndChar.line + 1, lineAndChar.character];
28+
}
29+
30+
export function formatError(error: BaseError): string {
31+
if (error instanceof DiagnosticError) {
32+
const rootDir: string = process.cwd().split(path.sep)[0] || "/";
33+
return ts.formatDiagnostics(error.getDiagnostics(), {
34+
getCanonicalFileName: (fileName: string) => fileName,
35+
getCurrentDirectory: () => rootDir,
36+
getNewLine: () => "\n",
37+
});
38+
} else if (error instanceof UnknownNodeError) {
39+
const firstLine: string = error.getNode().getFullText().trim().split("\n")[0].trim();
40+
const [sourceFile, lineNumber, charPos]: [string, number, number] = getNodeLocation(error.getNode());
41+
return `${error.name}: Unknown node type "${firstLine}" at ${sourceFile}(${lineNumber},${charPos})\n`;
42+
}
43+
44+
return `${error.name}: ${error.message}\n`;
45+
}

typescript-to-json-schema.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import * as ts from "typescript";
2-
import * as path from "path";
31
import * as commander from "commander";
42

53
import { createGenerator } from "./factory/generator";
4+
import { formatError } from "./src/Utils/formatError";
65

76
import { Config } from "./src/Config";
87
import { Schema } from "./src/Schema/Schema";
9-
import { DiagnosticError } from "./src/Error/DiagnosticError";
8+
import { BaseError } from "./src/Error/BaseError";
109

1110
const args: any = commander
1211
.option("-p, --path <path>", "Typescript path")
@@ -45,15 +44,10 @@ try {
4544
const schema: Schema = createGenerator(config).createSchema(args.type);
4645
process.stdout.write(JSON.stringify(schema, null, 2));
4746
} catch (error) {
48-
if (error instanceof DiagnosticError) {
49-
const errorMessage: string = ts.formatDiagnostics(error.getDiagnostics(), {
50-
getCanonicalFileName: (fileName: string) => fileName,
51-
getCurrentDirectory: () => path.resolve(path.dirname(args.path)),
52-
getNewLine: () => "\n",
53-
});
54-
process.stderr.write(errorMessage);
47+
if (error instanceof BaseError) {
48+
process.stderr.write(formatError(error));
5549
process.exit(1);
50+
} else {
51+
throw error;
5652
}
57-
58-
throw error;
5953
}

0 commit comments

Comments
 (0)