Skip to content

Commit

Permalink
feat: export generic parent EtaError
Browse files Browse the repository at this point in the history
  • Loading branch information
multivoltage committed Mar 17, 2024
1 parent c0c52a5 commit e4e8ebb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/err.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ export class EtaError extends Error {
}
}

export class EtaParseError extends Error {
export class EtaParseError extends EtaError {
constructor(message: string) {
super(message);
this.name = "EtaParser Error";
}
}

export class EtaRuntimeErr extends Error {
export class EtaRuntimeError extends EtaError {
constructor(message: string) {
super(message);
this.name = "EtaRuntime Error";
}
}

export class EtaFileResolutionError extends Error {
export class EtaFileResolutionError extends EtaError {
constructor(message: string) {
super(message);
this.name = "EtaFileResolution Error";
}
}

export class EtaNameResolutionError extends Error {
export class EtaNameResolutionError extends EtaError {
constructor(message: string) {
super(message);
this.name = "EtaNameResolution Error";
Expand Down Expand Up @@ -75,7 +75,7 @@ export function RuntimeErr(originalError: Error, str: string, lineNo: number, pa

const header = filename ? filename + ":" + lineNo + "\n" : "line " + lineNo + "\n";

const err = new EtaRuntimeErr(header + context + "\n\n" + originalError.message);
const err = new EtaRuntimeError(header + context + "\n\n" + originalError.message);

err.name = originalError.name; // the original name (e.g. ReferenceError) may be useful

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Eta as EtaCore } from "./core.ts";
import { readFile, resolvePath } from "./file-handling.ts";
export {
EtaError,
EtaParseError,
EtaRuntimeErr,
EtaRuntimeError,
EtaFileResolutionError,
EtaNameResolutionError,
} from "./err.ts";
Expand Down
13 changes: 9 additions & 4 deletions test/err.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import path from "path";
import {
Eta,
EtaError,
EtaParseError,
EtaRuntimeErr,
EtaRuntimeError,
EtaFileResolutionError,
EtaNameResolutionError,
} from "../src/index";
Expand All @@ -16,6 +17,7 @@ describe("ParseErr", () => {
try {
eta.renderString("template <%", {});
} catch (ex) {
expect(ex).toBeInstanceOf(EtaError);
expect(ex).toBeInstanceOf(EtaParseError);
expect((ex as EtaParseError).name).toBe("EtaParser Error");
expect((ex as EtaParseError).message).toBe(`unclosed tag at line 1 col 10:
Expand All @@ -30,6 +32,7 @@ describe("ParseErr", () => {
try {
eta.compile("template <%");
} catch (ex) {
expect(ex).toBeInstanceOf(EtaError);
expect(ex).toBeInstanceOf(EtaParseError);
expect((ex as EtaParseError).name).toBe("EtaParser Error");
expect((ex as EtaParseError).message).toBe(`unclosed tag at line 1 col 10:
Expand All @@ -50,9 +53,10 @@ describe("RuntimeErr", () => {
try {
eta.render("./runtime-error", {});
} catch (ex) {
expect(ex).toBeInstanceOf(EtaRuntimeErr);
expect((ex as EtaRuntimeErr).name).toBe("ReferenceError");
expect((ex as EtaRuntimeErr).message).toBe(`${errorFilepath}:2
expect(ex).toBeInstanceOf(EtaError);
expect(ex).toBeInstanceOf(EtaRuntimeError);
expect((ex as EtaRuntimeError).name).toBe("ReferenceError");
expect((ex as EtaRuntimeError).message).toBe(`${errorFilepath}:2
1|
>> 2| <%= undefinedVariable %>
3| Lorem Ipsum
Expand All @@ -70,6 +74,7 @@ describe("EtaFileResolutionError", () => {
try {
eta.render("./not-existing-template", {});
} catch (ex) {
expect(ex).toBeInstanceOf(EtaError);
expect(ex).toBeInstanceOf(EtaFileResolutionError);
expect((ex as EtaFileResolutionError).name).toBe("EtaFileResolution Error");
expect((ex as EtaFileResolutionError).message).toBe(
Expand Down

0 comments on commit e4e8ebb

Please sign in to comment.