Skip to content

Commit f1e064e

Browse files
sshaderConvex, Inc.
authored andcommitted
Fix error handling for custom test query (#27004)
This fixes two bugs: * We weren't wrapping some of our errors in `ErrorMetadata::bad_request` so the messages weren't making it to the user * If you have never pushed Convex functions, we'd fail to run the function with a "your version is too old" error message. This is fixed by conditionally setting the UdfConfig (which is what includes the npm version) in the transaction (which never gets committed). GitOrigin-RevId: fe10ae6ad1befd75cb424bf2407b9e34149a4079
1 parent df0edff commit f1e064e

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

crates/application/src/lib.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,23 +1928,26 @@ impl<RT: Runtime> Application<RT> {
19281928

19291929
let mut tx = self.begin(identity.clone()).await?;
19301930

1931-
// Use the last pushed version. If there hasn't been a push
1932-
// yet, act like the most recent version.
1933-
let server_version = UdfConfigModel::new(&mut tx, component.into())
1934-
.get()
1935-
.await?
1936-
.map(|udf_config| udf_config.server_version.clone())
1937-
.unwrap_or_else(|| Version::parse("1000.0.0").unwrap());
1931+
let mut udf_config_model = UdfConfigModel::new(&mut tx, component.into());
1932+
let udf_config = match udf_config_model.get().await? {
1933+
Some(udf_config) => udf_config.into_value(),
1934+
None => {
1935+
// If there hasn't been a push
1936+
// yet, act like the most recent version.
1937+
let udf_config = UdfConfig {
1938+
server_version: Version::new(1000, 0, 0),
1939+
import_phase_rng_seed: self.runtime.with_rng(|rng| rng.gen()),
1940+
import_phase_unix_timestamp: self.runtime.unix_timestamp(),
1941+
};
1942+
udf_config_model.set(udf_config.clone()).await?;
1943+
udf_config
1944+
},
1945+
};
19381946

19391947
// 1. analyze the module
19401948
// We can analyze this module by itself, without combining it with the existing
19411949
// modules since this module should be self-contained and not import
19421950
// from other modules.
1943-
let udf_config = UdfConfig {
1944-
server_version,
1945-
import_phase_rng_seed: self.runtime.with_rng(|rng| rng.gen()),
1946-
import_phase_unix_timestamp: self.runtime.unix_timestamp(),
1947-
};
19481951

19491952
let analyze_results = self
19501953
.analyze(
@@ -1973,10 +1976,16 @@ impl<RT: Runtime> Application<RT> {
19731976
if function.name.is_default_export() {
19741977
analyzed_function = Some(function.clone());
19751978
} else {
1976-
anyhow::bail!("Only `export default` is supported.");
1979+
anyhow::bail!(ErrorMetadata::bad_request(
1980+
"InvalidTestQuery",
1981+
"Only `export default` is supported."
1982+
));
19771983
}
19781984
}
1979-
let analyzed_function = analyzed_function.context("Missing default export.")?;
1985+
let analyzed_function = analyzed_function.context(ErrorMetadata::bad_request(
1986+
"InvalidTestQuery",
1987+
"Default export is not a Convex function.",
1988+
))?;
19801989

19811990
let source_package_id = SourcePackageModel::new(&mut tx, component.into())
19821991
.put(source_package)
@@ -2015,16 +2024,22 @@ impl<RT: Runtime> Application<RT> {
20152024
}| { (result, log_lines) },
20162025
),
20172026
UdfType::Mutation => {
2018-
anyhow::bail!("Mutations are not supported in the REPL yet.")
2027+
anyhow::bail!(ErrorMetadata::bad_request(
2028+
"UnsupportedTestQuery",
2029+
"Mutations are not supported in the REPL yet."
2030+
))
20192031
},
20202032
UdfType::Action => {
2021-
anyhow::bail!("Actions are not supported in the REPL yet.")
2033+
anyhow::bail!(ErrorMetadata::bad_request(
2034+
"UnsupportedTestQuery",
2035+
"Actions are not supported in the REPL yet."
2036+
))
20222037
},
20232038
UdfType::HttpAction => {
2024-
anyhow::bail!(
2025-
"HTTP actions are not supported in the REPL. A \"not found\" message should \
2026-
be returned instead."
2027-
)
2039+
anyhow::bail!(ErrorMetadata::bad_request(
2040+
"UnsupportedTestQuery",
2041+
"HTTP actions are not supported in the REPL yet."
2042+
))
20282043
},
20292044
}?;
20302045
let log_lines = RedactedLogLines::from_log_lines(log_lines, block_logging);

0 commit comments

Comments
 (0)