summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/cron/01_cron.ts18
-rw-r--r--ext/cron/lib.rs7
-rw-r--r--tests/unit/cron_test.ts10
3 files changed, 23 insertions, 12 deletions
diff --git a/ext/cron/01_cron.ts b/ext/cron/01_cron.ts
index 017da8ae2..b5c556a67 100644
--- a/ext/cron/01_cron.ts
+++ b/ext/cron/01_cron.ts
@@ -41,7 +41,9 @@ export function formatToCronSchedule(
} else if (end === undefined && every !== undefined) {
return "*/" + every;
} else {
- throw new TypeError("Invalid cron schedule");
+ throw new TypeError(
+ `Invalid cron schedule: start=${start}, end=${end}, every=${every}`,
+ );
}
} else {
if (typeof exact === "number") {
@@ -103,10 +105,14 @@ function cron(
handler2?: () => Promise<void> | void,
) {
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) {
- 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);
@@ -119,13 +125,15 @@ function cron(
if (typeof handlerOrOptions1 === "function") {
handler = handlerOrOptions1;
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") {
handler = handler2;
options = handlerOrOptions1;
} 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(
diff --git a/ext/cron/lib.rs b/ext/cron/lib.rs
index ede01ba45..95e38179d 100644
--- a/ext/cron/lib.rs
+++ b/ext/cron/lib.rs
@@ -116,12 +116,15 @@ where
fn validate_cron_name(name: &str) -> Result<(), AnyError> {
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| {
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(())
}
diff --git a/tests/unit/cron_test.ts b/tests/unit/cron_test.ts
index 02573a898..5f14f6c78 100644
--- a/tests/unit/cron_test.ts
+++ b/tests/unit/cron_test.ts
@@ -15,7 +15,7 @@ Deno.test(function noNameTest() {
// @ts-ignore test
() => Deno.cron(),
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
() => Deno.cron("foo"),
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
() => Deno.cron("foo", "*/1 * * * *"),
TypeError,
- "Deno.cron requires a handler",
+ "Cannot create cron job: a handler is required",
);
});
@@ -66,7 +66,7 @@ Deno.test(function invalidNameTest() {
() => {},
),
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", "* * * * *", () => {}, () => {});
},
TypeError,
- "Deno.cron requires a single handler",
+ "Cannot create cron job, a single handler is required: two handlers were specified",
);
});