Skip to content

Commit

Permalink
Improve exceptions (#141)
Browse files Browse the repository at this point in the history
Project is target ES2017 anyway so Error can be extended directly instead of faking error objects. As a result exceptions thrown in tests now yield the correct error message and stack trace
  • Loading branch information
kayahr authored and domoritz committed Jun 22, 2019
1 parent 797dfc7 commit 8cdd6cc
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 53 deletions.
15 changes: 3 additions & 12 deletions src/Error/BaseError.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
export abstract class BaseError implements Error {
private callStack: any;

public constructor() {
this.callStack = new Error().stack;
export abstract class BaseError extends Error {
public constructor(message?: string) {
super(message);
}

public get stack(): string {
return this.callStack;
}

public abstract get name(): string;
public abstract get message(): string;
}
11 changes: 2 additions & 9 deletions src/Error/DiagnosticError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ import { BaseError } from "./BaseError";

export class DiagnosticError extends BaseError {
public constructor(private diagnostics: ReadonlyArray<ts.Diagnostic>) {
super();
}

public get name(): string {
return "DiagnosticError";
}
public get message(): string {
return this.diagnostics
super(diagnostics
.map((diagnostic) => ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"))
.join("\n\n");
.join("\n\n"));
}

public getDiagnostics() {
Expand Down
9 changes: 1 addition & 8 deletions src/Error/LogicError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ import { BaseError } from "./BaseError";

export class LogicError extends BaseError {
public constructor(private msg: string) {
super();
}

public get name(): string {
return "LogicError";
}
public get message(): string {
return this.msg;
super(msg);
}
}
9 changes: 1 addition & 8 deletions src/Error/NoRootTypeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import { BaseError } from "./BaseError";

export class NoRootTypeError extends BaseError {
public constructor(private type: string) {
super();
}

public get name(): string {
return "NoRootTypeError";
}
public get message(): string {
return `No root type "${this.type}" found`;
super(`No root type "${type}" found`);
}

public getType(): string {
Expand Down
10 changes: 2 additions & 8 deletions src/Error/UnknownNodeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ import { BaseError } from "./BaseError";

export class UnknownNodeError extends BaseError {
public constructor(private node: ts.Node, private reference?: ts.Node) {
super();
}

public get name(): string {
return "UnknownNodeError";
}
public get message(): string {
return `Unknown node "${this.node.getFullText()}`;
super(`Unknown node "${node.getFullText()}`);
}

public getNode(): ts.Node {
return this.node;
}

public getReference(): ts.Node | undefined {
return this.reference;
}
Expand Down
9 changes: 1 addition & 8 deletions src/Error/UnknownTypeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import { BaseError } from "./BaseError";

export class UnknownTypeError extends BaseError {
public constructor(private type: BaseType) {
super();
}

public get name(): string {
return "UnknownTypeError";
}
public get message(): string {
return `Unknown type "${this.type.getId()}"`;
super(`Unknown type "${type.getId()}"`);
}

public getType(): BaseType {
Expand Down

0 comments on commit 8cdd6cc

Please sign in to comment.