Skip to content

Commit

Permalink
feat(schema): Add logging utilities for catch calls (#33950)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Jan 30, 2025
1 parent 545bf31 commit 23fa0bd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/modules/manager/composer/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { z } from 'zod';
import { logger } from '../../../logger';
import { readLocalFile } from '../../../util/fs';
import { regEx } from '../../../util/regex';
import { Json, LooseArray, LooseRecord } from '../../../util/schema-utils';
import {
Json,
LooseArray,
LooseRecord,
withDebugMessage,
} from '../../../util/schema-utils';
import { BitbucketTagsDatasource } from '../../datasource/bitbucket-tags';
import { GitTagsDatasource } from '../../datasource/git-tags';
import { GithubTagsDatasource } from '../../datasource/github-tags';
Expand Down Expand Up @@ -126,10 +131,7 @@ export type ReposArray = z.infer<typeof ReposArray>;
export const Repos = z
.union([ReposRecord, ReposArray])
.default([]) // Prevents warnings for packages without repositories field
.catch(({ error: err }) => {
logger.debug({ err }, 'Composer: invalid "repositories" field');
return [];
})
.catch(withDebugMessage([], 'Composer: invalid "repositories" field'))
.transform((repos) => {
let packagist = true;
const repoUrls: string[] = [];
Expand Down Expand Up @@ -242,10 +244,9 @@ export const ComposerExtract = z
.pipe(Json)
.pipe(Lockfile)
.nullable()
.catch(({ error: err }) => {
logger.debug({ err }, 'Composer: lockfile parsing error');
return null;
}),
.catch(
withDebugMessage(null, 'Composer: lockfile parsing error'),
),
]),
),
}),
Expand Down
33 changes: 33 additions & 0 deletions lib/util/schema-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { codeBlock } from 'common-tags';
import { z } from 'zod';
import { logger } from '../../test/util';
import {
Json,
Json5,
Expand All @@ -10,6 +11,8 @@ import {
Toml,
UtcDate,
Yaml,
withDebugMessage,
withTraceMessage,
} from './schema-utils';

describe('util/schema-utils', () => {
Expand Down Expand Up @@ -494,4 +497,34 @@ describe('util/schema-utils', () => {
});
});
});

describe('logging utils', () => {
it('logs debug message and returns fallback value', () => {
const Schema = z
.string()
.catch(withDebugMessage('default string', 'Debug message'));

const result = Schema.parse(42);

expect(result).toBe('default string');
expect(logger.logger.debug).toHaveBeenCalledWith(
{ err: expect.any(z.ZodError) },
'Debug message',
);
});

it('logs trace message and returns fallback value', () => {
const Schema = z
.string()
.catch(withTraceMessage('default string', 'Trace message'));

const result = Schema.parse(42);

expect(result).toBe('default string');
expect(logger.logger.trace).toHaveBeenCalledWith(
{ err: expect.any(z.ZodError) },
'Trace message',
);
});
});
});
21 changes: 21 additions & 0 deletions lib/util/schema-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as JSONC from 'jsonc-parser';
import { DateTime } from 'luxon';
import type { JsonArray, JsonValue } from 'type-fest';
import { type ZodEffects, type ZodType, type ZodTypeDef, z } from 'zod';
import { logger } from '../logger';
import type { PackageDependency } from '../modules/manager/types';
import { parse as parseToml } from './toml';
import { parseSingleYaml, parseYaml } from './yaml';
Expand Down Expand Up @@ -278,3 +279,23 @@ export function withDepType<
return deps;
});
}

export function withDebugMessage<Input, Output>(
value: Output,
msg: string,
): (ctx: { error: z.ZodError; input: Input }) => Output {
return ({ error: err }) => {
logger.debug({ err }, msg);
return value;
};
}

export function withTraceMessage<Input, Output>(
value: Output,
msg: string,
): (ctx: { error: z.ZodError; input: Input }) => Output {
return ({ error: err }) => {
logger.trace({ err }, msg);
return value;
};
}

0 comments on commit 23fa0bd

Please sign in to comment.