Skip to content

Commit

Permalink
feat: export EtaNameResolutionError
Browse files Browse the repository at this point in the history
  • Loading branch information
multivoltage committed Mar 2, 2024
1 parent bcc19d7 commit ee0a1d4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/err.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export class EtaFileResolutionError extends Error {
}
}

export class EtaNameResolutionError extends Error {
constructor(message: string) {
super(message);
this.name = "EtaNameResolution Error";
}
}

/**
* Throws an EtaError with a nicely formatted error and message showing where in the template the error occurred.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Eta as EtaCore } from "./core.ts";
import { readFile, resolvePath } from "./file-handling.ts";
export { EtaParseError, EtaRuntimeErr, EtaFileResolutionError } from "./err.ts";
export {
EtaParseError,
EtaRuntimeErr,
EtaFileResolutionError,
EtaNameResolutionError,
} from "./err.ts";

export class Eta extends EtaCore {
readFile = readFile;
Expand Down
4 changes: 2 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EtaError } from "./err.ts";
import { EtaNameResolutionError } from "./err.ts";

/* TYPES */
import type { Options } from "./config.ts";
Expand Down Expand Up @@ -31,7 +31,7 @@ function handleCache(this: Eta, template: string, options: Partial<Options>): Te
if (cachedTemplate) {
return cachedTemplate;
} else {
throw new EtaError("Failed to get template '" + template + "'");
throw new EtaNameResolutionError("Failed to get template '" + template + "'");
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion test/err.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* global it, expect, describe */

import path from "path";
import { Eta, EtaParseError, EtaRuntimeErr, EtaFileResolutionError } from "../src/index";
import {
Eta,
EtaParseError,
EtaRuntimeErr,
EtaFileResolutionError,
EtaNameResolutionError,
} from "../src/index";

describe("ParseErr", () => {
const eta = new Eta();
Expand Down Expand Up @@ -106,3 +112,19 @@ describe("EtaFileResolutionError", () => {
}
});
});

describe("EtaNameResolutionError", () => {
const eta = new Eta({ debug: true, views: path.join(__dirname, "templates") });

it("error throws correctly", () => {
const template = "@not-existing-tp";

try {
eta.render(template, {});
} catch (ex) {
expect(ex).toBeInstanceOf(EtaNameResolutionError);
expect((ex as EtaNameResolutionError).name).toBe("EtaNameResolution Error");
expect((ex as EtaNameResolutionError).message).toBe(`Failed to get template '${template}'`);
}
});
});

0 comments on commit ee0a1d4

Please sign in to comment.