Skip to content

Commit

Permalink
refactor: unify queue error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito committed Jan 5, 2025
1 parent 0da4b75 commit cc81c16
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 48 deletions.
17 changes: 5 additions & 12 deletions src/api-parser-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ import { compile } from 'stylis';

import { apiv1 as userAgents } from '../data/user-agents.json';
import { APIDirect, APIv1 } from './data';
import { LOOP_LIMIT, addError, checkErrors } from './errors';
import type { APIResponse, FontObjectV1 } from './types';
import { orderObject, weightListGen } from './utils';
import { validate } from './validate';

const baseurl = 'https://fonts.googleapis.com/css?subset=';
const queue = Limiter(18);

const results: FontObjectV1[] = [];
const errs: string[] = [];

const queue = Limiter(18);

export const fetchCSS = async (
font: APIResponse,
Expand Down Expand Up @@ -203,7 +202,7 @@ const processQueue = async (
}
consola.success(`Parsed ${id}`);
} catch (error) {
errs.push(`${font.family} experienced an error. ${String(error)}`);
addError(`${font.family} experienced an error. ${String(error)}`);
}
};

Expand All @@ -214,18 +213,12 @@ const processQueue = async (
*/
export const parsev1 = async (force: boolean, noValidate: boolean) => {
for (const font of APIDirect) {
checkErrors(LOOP_LIMIT);
queue.add(() => processQueue(font, force));
}

await queue.flush();

if (errs.length > 0) {
for (const error of errs) {
consola.error(error);
}

throw new Error('Some fonts experienced errors during parsing.');
}
checkErrors();

// Order the font objects alphabetically for consistency and not create huge diffs
const unordered: FontObjectV1 = Object.assign({}, ...results);
Expand Down
17 changes: 5 additions & 12 deletions src/api-parser-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ import { compile } from 'stylis';

import { apiv2 as userAgents } from '../data/user-agents.json';
import { APIDirect, APIv2 } from './data';
import { LOOP_LIMIT, addError, checkErrors } from './errors';
import type { APIResponse, FontObjectV2 } from './types';
import { orderObject, weightListGen } from './utils';
import { validate } from './validate';

const baseurl = 'https://fonts.googleapis.com/css2?family=';
const queue = Limiter(18);

const results: FontObjectV2[] = [];
const errs: string[] = [];

const queue = Limiter(18);

export const fetchCSS = async (
fontFamily: string,
Expand Down Expand Up @@ -261,7 +260,7 @@ const processQueue = async (
}
consola.success(`Parsed ${id}`);
} catch (error) {
errs.push(`${font.family} experienced an error. ${String(error)}`);
addError(`${font.family} experienced an error. ${String(error)}`);
}
};

Expand All @@ -272,18 +271,12 @@ const processQueue = async (
*/
export const parsev2 = async (force: boolean, noValidate: boolean) => {
for (const font of APIDirect) {
checkErrors(LOOP_LIMIT);
queue.add(() => processQueue(font, force));
}

await queue.flush();

if (errs.length > 0) {
for (const err of errs) {
consola.error(err);
}

throw new Error('Some fonts experienced errors during parsing.');
}
checkErrors();

// Order the font objects alphabetically for consistency and not create huge diffs
const unordered: FontObjectV2 = Object.assign({}, ...results);
Expand Down
23 changes: 23 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import consola from 'consola';

const errs: string[] = [];

export const LOOP_LIMIT = 5;

export const addError = (error: string) => {
errs.push(error);
};

export const checkErrors = (limit = 0) => {
if (errs.length > limit) {
for (const err of errs) {
consola.error(err);
}

if (limit > 0) {
throw new Error('Too many errors occurred during parsing. Stopping...');
}

throw new Error('Some fonts experienced errors during parsing.');
}
};
18 changes: 6 additions & 12 deletions src/icons-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
processCSS as processV2CSS,
} from './api-parser-v2';
import { APIIconDirect, APIIconStatic, APIIconVariable } from './data';
import { LOOP_LIMIT, addError, checkErrors } from './errors';
import type {
APIIconResponse,
FontObjectV2,
Expand All @@ -23,11 +24,10 @@ import {
parseCSS as parseVariableCSS,
} from './variable-parser';

const queue = Limiter(18);

const resultsStatic: FontObjectV2[] = [];
const resultsVariable: FontObjectVariable = {};
const errs: string[] = [];

const queue = Limiter(18);

const processQueue = async (icon: APIIconResponse, force: boolean) => {
try {
Expand Down Expand Up @@ -76,7 +76,7 @@ const processQueue = async (icon: APIIconResponse, force: boolean) => {
}
consola.success(`Parsed ${id}`);
} catch (error) {
errs.push(`${icon.family} experienced an error. ${String(error)}`);
addError(`${icon.family} experienced an error. ${String(error)}`);
}
};

Expand All @@ -87,18 +87,12 @@ const processQueue = async (icon: APIIconResponse, force: boolean) => {
*/
export const parseIcons = async (force: boolean) => {
for (const icon of APIIconDirect) {
checkErrors(LOOP_LIMIT);
queue.add(() => processQueue(icon, force));
}

await queue.flush();

if (errs.length > 0) {
for (const err of errs) {
consola.error(err);
}

throw new Error('Some icons experienced errors during parsing.');
}
checkErrors();

// Order the font objects alphabetically for consistency and not create huge diffs
const unorderedStatic: FontObjectV2 = Object.assign({}, ...resultsStatic);
Expand Down
18 changes: 6 additions & 12 deletions src/variable-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { compile } from 'stylis';

import { apiv2 as userAgents } from '../data/user-agents.json';
import { APIVariableDirect } from './data';
import { LOOP_LIMIT, addError, checkErrors } from './errors';
import type {
FontObjectVariable,
FontObjectVariableDirect,
Expand All @@ -20,11 +21,10 @@ import { validate } from './validate';

export type Links = Record<string, string>;

const results: FontObjectVariable = {};
const errs: string[] = [];

const queue = Limiter(10);

const results: FontObjectVariable = {};

// CSS API needs axes to given in alphabetical order or request throws e.g. (a,b,c,A,B,C)
export const sortAxes = (axesArr: string[]) => {
const upper = axesArr
Expand Down Expand Up @@ -264,7 +264,7 @@ const processQueue = async (font: FontObjectVariableDirect) => {
results[font.id] = { ...font, variants: variantsObject };
consola.success(`Parsed ${font.id}`);
} catch (error) {
errs.push(`${font.family} experienced an error. ${String(error)}`);
addError(`${font.family} experienced an error. ${String(error)}`);
}
};

Expand All @@ -274,18 +274,12 @@ const processQueue = async (font: FontObjectVariableDirect) => {
*/
export const parseVariable = async (noValidate: boolean) => {
for (const font of APIVariableDirect) {
checkErrors(LOOP_LIMIT);
queue.add(() => processQueue(font));
}

await queue.flush();

if (errs.length > 0) {
for (const err of errs) {
consola.error(err);
}

throw new Error('Some fonts experienced errors during parsing.');
}
checkErrors();

if (!noValidate) {
validate('variable', results);
Expand Down

0 comments on commit cc81c16

Please sign in to comment.