From 23fa0bd05dbefcea2f7525144ff382846fde1f87 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov <zharinov@users.noreply.github.com> Date: Thu, 30 Jan 2025 19:32:08 -0300 Subject: [PATCH] feat(schema): Add logging utilities for catch calls (#33950) --- lib/modules/manager/composer/schema.ts | 19 ++++++++------- lib/util/schema-utils.spec.ts | 33 ++++++++++++++++++++++++++ lib/util/schema-utils.ts | 21 ++++++++++++++++ 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/lib/modules/manager/composer/schema.ts b/lib/modules/manager/composer/schema.ts index e1d9ffa47f..4fabd2eaa4 100644 --- a/lib/modules/manager/composer/schema.ts +++ b/lib/modules/manager/composer/schema.ts @@ -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'; @@ -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[] = []; @@ -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'), + ), ]), ), }), diff --git a/lib/util/schema-utils.spec.ts b/lib/util/schema-utils.spec.ts index ccca2f70b9..4614b7b80c 100644 --- a/lib/util/schema-utils.spec.ts +++ b/lib/util/schema-utils.spec.ts @@ -1,5 +1,6 @@ import { codeBlock } from 'common-tags'; import { z } from 'zod'; +import { logger } from '../../test/util'; import { Json, Json5, @@ -10,6 +11,8 @@ import { Toml, UtcDate, Yaml, + withDebugMessage, + withTraceMessage, } from './schema-utils'; describe('util/schema-utils', () => { @@ -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', + ); + }); + }); }); diff --git a/lib/util/schema-utils.ts b/lib/util/schema-utils.ts index 5acc34f015..b9bc249d50 100644 --- a/lib/util/schema-utils.ts +++ b/lib/util/schema-utils.ts @@ -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'; @@ -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; + }; +} -- GitLab