Skip to content

Commit

Permalink
fix: move location of try { statement to handle more errors
Browse files Browse the repository at this point in the history
Also added test for when `bciAvIdToString()` throws.
  • Loading branch information
klown committed Nov 3, 2023
1 parent 106fb01 commit 7900134
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/SvgUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("SvgUtils module", () => {
const expectedString = "B823";
const bciAvIdArray =[ 12335, "/", 8499 ]; // VERB+EN
const expectedConcatenation = "B106/B12";
const invalidBciAvId = 1;

beforeAll(async () => {
await initAdaptivePaletteGlobals();
Expand All @@ -47,4 +48,8 @@ describe("SvgUtils module", () => {
expect(result).toBe(expectedConcatenation);
});

test("Unknown BCI-AV-ID", () => {
expect(() => { bciAvIdToString(invalidBciAvId); }).toThrow();
});

});
9 changes: 5 additions & 4 deletions src/SvgUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { BlissSVGBuilder } from "bliss-svg-builder";
import { BciAvId } from "./BlissSymbol";
import { adaptivePaletteGlobals } from "./GlobalData";

export const DEFAULT_SVG_MARKUP_STRING = "B3"; // Question mark

/**
* Convert the given `BciAvId` to a SVG builder code string. If the `BciAvId`
* argument is an array of BCI-AV-IDs and punctuation, concatenate the array
Expand Down Expand Up @@ -50,16 +52,15 @@ export function bciAvIdToString (bciAvId: BciAvId) {
* @return {String} - The corresponding SVG markup, or the empty string.
*/
export function getSvgMarkupString (bciAvId: BciAvId) {

let builder;
const svgBuilderArgument = bciAvIdToString(bciAvId);
try {
const svgBuilderArgument = bciAvIdToString(bciAvId);
builder = new BlissSVGBuilder(svgBuilderArgument);
}
catch (err) {
console.error(err);
console.error(`GETSVGMARKUPSTRING(): using question mark for ${svgBuilderArgument} from bci-av-id = ${bciAvId}`);
builder = new BlissSVGBuilder("B3"); // question mark
console.error(`GETSVGMARKUPSTRING(): using question mark for SVG builder argument from bci-av-id = ${bciAvId}`);
builder = new BlissSVGBuilder(DEFAULT_SVG_MARKUP_STRING); // question mark
}
return builder.svgCode;
}
Expand Down

0 comments on commit 7900134

Please sign in to comment.