Handle invalid args in signal routes

This commit is contained in:
Jamie Kyle 2023-11-27 15:50:23 -08:00 committed by GitHub
parent ceb1564e6b
commit 88f004ac81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,8 @@ import 'urlpattern-polyfill';
import { URL } from 'url'; import { URL } from 'url';
import { z } from 'zod'; import { z } from 'zod';
import { strictAssert } from './assert'; import { strictAssert } from './assert';
import * as log from '../logging/log';
import * as Errors from '../types/errors';
function toUrl(input: URL | string): URL | null { function toUrl(input: URL | string): URL | null {
if (input instanceof URL) { if (input instanceof URL) {
@ -152,10 +154,29 @@ function _route<Key extends string, Args extends object>(
for (const matcher of config.patterns) { for (const matcher of config.patterns) {
const result = matcher(url); const result = matcher(url);
if (result) { if (result) {
return { let rawArgs;
key, try {
args: config.schema.parse(config.parse(result, url)), rawArgs = config.parse(result, url);
}; } catch (error) {
log.error(
`Failed to parse route ${key} from URL ${url.toString()}`,
Errors.toLogFormat(error)
);
return null;
}
const parseResult = config.schema.safeParse(rawArgs);
if (parseResult.success) {
const args = parseResult.data;
return {
key,
args,
};
}
log.error(
`Failed to parse route ${key} from URL ${url.toString()}`,
parseResult.error.format()
);
return null;
} }
} }
return null; return null;