1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor(ext/cron): align error messages (#25300)

Aligns the error messages in the cron extension to be in-line with the
Deno style guide.

https://github.com/denoland/deno/issues/25269
This commit is contained in:
Ian Bull 2024-09-04 23:27:58 -07:00 committed by GitHub
parent acd01eb2b4
commit 17b5e98b82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 12 deletions

View file

@ -41,7 +41,9 @@ export function formatToCronSchedule(
} else if (end === undefined && every !== undefined) { } else if (end === undefined && every !== undefined) {
return "*/" + every; return "*/" + every;
} else { } else {
throw new TypeError("Invalid cron schedule"); throw new TypeError(
`Invalid cron schedule: start=${start}, end=${end}, every=${every}`,
);
} }
} else { } else {
if (typeof exact === "number") { if (typeof exact === "number") {
@ -103,10 +105,14 @@ function cron(
handler2?: () => Promise<void> | void, handler2?: () => Promise<void> | void,
) { ) {
if (name === undefined) { if (name === undefined) {
throw new TypeError("Deno.cron requires a unique name"); throw new TypeError(
"Cannot create cron job, a unique name is required: received 'undefined'",
);
} }
if (schedule === undefined) { if (schedule === undefined) {
throw new TypeError("Deno.cron requires a valid schedule"); throw new TypeError(
"Cannot create cron job, a schedule is required: received 'undefined'",
);
} }
schedule = parseScheduleToString(schedule); schedule = parseScheduleToString(schedule);
@ -119,13 +125,15 @@ function cron(
if (typeof handlerOrOptions1 === "function") { if (typeof handlerOrOptions1 === "function") {
handler = handlerOrOptions1; handler = handlerOrOptions1;
if (handler2 !== undefined) { if (handler2 !== undefined) {
throw new TypeError("Deno.cron requires a single handler"); throw new TypeError(
"Cannot create cron job, a single handler is required: two handlers were specified",
);
} }
} else if (typeof handler2 === "function") { } else if (typeof handler2 === "function") {
handler = handler2; handler = handler2;
options = handlerOrOptions1; options = handlerOrOptions1;
} else { } else {
throw new TypeError("Deno.cron requires a handler"); throw new TypeError("Cannot create cron job: a handler is required");
} }
const rid = op_cron_create( const rid = op_cron_create(

View file

@ -116,12 +116,15 @@ where
fn validate_cron_name(name: &str) -> Result<(), AnyError> { fn validate_cron_name(name: &str) -> Result<(), AnyError> {
if name.len() > 64 { if name.len() > 64 {
return Err(type_error("Cron name is too long")); return Err(type_error(format!(
"Cron name cannot exceed 64 characters: current length {}",
name.len()
)));
} }
if !name.chars().all(|c| { if !name.chars().all(|c| {
c.is_ascii_whitespace() || c.is_ascii_alphanumeric() || c == '_' || c == '-' c.is_ascii_whitespace() || c.is_ascii_alphanumeric() || c == '_' || c == '-'
}) { }) {
return Err(type_error("Invalid cron name. Only alphanumeric characters, whitespace, hyphens, and underscores are allowed")); return Err(type_error("Invalid cron name: only alphanumeric characters, whitespace, hyphens, and underscores are allowed"));
} }
Ok(()) Ok(())
} }

View file

@ -15,7 +15,7 @@ Deno.test(function noNameTest() {
// @ts-ignore test // @ts-ignore test
() => Deno.cron(), () => Deno.cron(),
TypeError, TypeError,
"Deno.cron requires a unique name", "Cannot create cron job, a unique name is required: received 'undefined'",
); );
}); });
@ -24,7 +24,7 @@ Deno.test(function noSchedule() {
// @ts-ignore test // @ts-ignore test
() => Deno.cron("foo"), () => Deno.cron("foo"),
TypeError, TypeError,
"Deno.cron requires a valid schedule", "Cannot create cron job, a schedule is required: received 'undefined'",
); );
}); });
@ -33,7 +33,7 @@ Deno.test(function noHandler() {
// @ts-ignore test // @ts-ignore test
() => Deno.cron("foo", "*/1 * * * *"), () => Deno.cron("foo", "*/1 * * * *"),
TypeError, TypeError,
"Deno.cron requires a handler", "Cannot create cron job: a handler is required",
); );
}); });
@ -66,7 +66,7 @@ Deno.test(function invalidNameTest() {
() => {}, () => {},
), ),
TypeError, TypeError,
"Cron name is too long", "Cron name cannot exceed 64 characters: current length 70",
); );
}); });
@ -388,7 +388,7 @@ Deno.test("error on two handlers", () => {
Deno.cron("abc", "* * * * *", () => {}, () => {}); Deno.cron("abc", "* * * * *", () => {}, () => {});
}, },
TypeError, TypeError,
"Deno.cron requires a single handler", "Cannot create cron job, a single handler is required: two handlers were specified",
); );
}); });