refactor: loadFont throws FontParseError instead of re-throwing raw error
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
/** @vitest-environment jsdom */
|
/** @vitest-environment jsdom */
|
||||||
|
import { FontParseError } from '../../errors';
|
||||||
import { loadFont } from './loadFont';
|
import { loadFont } from './loadFont';
|
||||||
|
|
||||||
describe('loadFont', () => {
|
describe('loadFont', () => {
|
||||||
@@ -65,8 +66,7 @@ describe('loadFont', () => {
|
|||||||
expect(result).toBe(mockFontInstance);
|
expect(result).toBe(mockFontInstance);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('propagates and logs error when font.load() rejects', async () => {
|
it('throws FontParseError when font.load() rejects', async () => {
|
||||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
||||||
const loadError = new Error('parse failed');
|
const loadError = new Error('parse failed');
|
||||||
const MockFontFace = vi.fn(
|
const MockFontFace = vi.fn(
|
||||||
function(this: any, name: string, buffer: BufferSource, options: FontFaceDescriptors) {
|
function(this: any, name: string, buffer: BufferSource, options: FontFaceDescriptors) {
|
||||||
@@ -75,18 +75,19 @@ describe('loadFont', () => {
|
|||||||
);
|
);
|
||||||
vi.stubGlobal('FontFace', MockFontFace);
|
vi.stubGlobal('FontFace', MockFontFace);
|
||||||
|
|
||||||
await expect(loadFont({ name: 'Broken', weight: 400 }, new ArrayBuffer(8))).rejects.toThrow('parse failed');
|
await expect(loadFont({ name: 'Broken', weight: 400 }, new ArrayBuffer(8))).rejects.toBeInstanceOf(
|
||||||
expect(consoleSpy).toHaveBeenCalledWith(loadError);
|
FontParseError,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('propagates and logs error when document.fonts.add throws', async () => {
|
it('throws FontParseError when document.fonts.add throws', async () => {
|
||||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
||||||
const addError = new Error('add failed');
|
const addError = new Error('add failed');
|
||||||
mockFontFaceSet.add.mockImplementation(() => {
|
mockFontFaceSet.add.mockImplementation(() => {
|
||||||
throw addError;
|
throw addError;
|
||||||
});
|
});
|
||||||
|
|
||||||
await expect(loadFont({ name: 'Broken', weight: 400 }, new ArrayBuffer(8))).rejects.toThrow('add failed');
|
await expect(loadFont({ name: 'Broken', weight: 400 }, new ArrayBuffer(8))).rejects.toBeInstanceOf(
|
||||||
expect(consoleSpy).toHaveBeenCalledWith(addError);
|
FontParseError,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { FontLoadRequestConfig } from '../../../../types';
|
import type { FontLoadRequestConfig } from '../../../../types';
|
||||||
|
import { FontParseError } from '../../errors';
|
||||||
|
|
||||||
export type PartialConfig = Pick<FontLoadRequestConfig, 'weight' | 'name' | 'isVariable'>;
|
export type PartialConfig = Pick<FontLoadRequestConfig, 'weight' | 'name' | 'isVariable'>;
|
||||||
/**
|
/**
|
||||||
@@ -6,6 +7,7 @@ export type PartialConfig = Pick<FontLoadRequestConfig, 'weight' | 'name' | 'isV
|
|||||||
* @param config - The font load request configuration.
|
* @param config - The font load request configuration.
|
||||||
* @param buffer - The buffer containing the font data.
|
* @param buffer - The buffer containing the font data.
|
||||||
* @returns A promise that resolves to the loaded `FontFace`.
|
* @returns A promise that resolves to the loaded `FontFace`.
|
||||||
|
* @throws {@link FontParseError} When the font buffer cannot be parsed or added to the document font set.
|
||||||
*/
|
*/
|
||||||
export async function loadFont(config: PartialConfig, buffer: BufferSource): Promise<FontFace> {
|
export async function loadFont(config: PartialConfig, buffer: BufferSource): Promise<FontFace> {
|
||||||
try {
|
try {
|
||||||
@@ -20,7 +22,6 @@ export async function loadFont(config: PartialConfig, buffer: BufferSource): Pro
|
|||||||
|
|
||||||
return font;
|
return font;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
throw new FontParseError(config.name, error);
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user