Strange use of Result Types
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 48/100
Research direction
Start in source/foundation/commands.rs at the Serve::handle implementation cited in the issue. Trace how command handlers represent exit codes and errors, including how console.error is rendered and how callers handle the result. Decide whether this branch should return an error and whether the result type should change; done means the behavior is consistent with the command conventions and covered by relevant tests.
Written by the indexing model from the issue text.
Description
I just looked at this code in source/foundation/commands.rs:51
#[crate::console::command(long_running)]
impl Serve {
async fn handle(self, app: &Application, console: &mut Console) -> crate::Result<u8> {
if console.is_captured() {
console.error("the `serve` command requires the application's binary; use HTTP requests to test the application in-process");
return Ok(1);
}
let host = self.host.unwrap_or_else(|| app.config().server.host.clone());
let port = self.port.unwrap_or_else(|| app.config().server.port);
use crate::ErrorContext as _;
app.clone()
.serve_with_output((host.as_str(), port), console)
.await
.with_context(|| format!("failed to serve on `{host}:{port}`"))?;
Ok(0)
}
}
And I wonder, is there any reason to use Ok(0) and Ok(1) instead of Ok(()) and Err(<the stuff thats in console.error>) to handle this, or at least Ok(<u8>) and Err() for the console.error path? It feels like its a 1:1 port of PHP code without harnessing the extreme power of the type system. So people believe me, I even asked a clanker to validate my observation:
The signature Result<u8> is defensible in general, for commands where a non-zero exit is a legitimate outcome rather than an error (grep, diff, check).
The usage in this code is not. console.error(...); return Ok(1); is an error disguised as a success. It throws away the error value, and the in-process caller, which is the only one that can reach that branch, gets a bare 1 instead of something it can inspect or propagate. The only thing it buys is control over how the message is formatted, and that problem belongs in the error renderer.
That branch should return Err. Unless other commands need non-zero success codes, the signature should be crate::Result<()>.
Just my observation after a few seconds in the code base. I really love the idea to get to use a modern language with a type system that actually helps building stuff. But we should also be careful not to just let AI do 1:1 ports from PHP so it just re-implements all the weird stuff from PHP in Rust.
I am very happy to see this direction and I'm really looking forward to the new era.
- Dominant language
- Rust
- Stars
- 2.2k
- Forks
- 105
- Avg merge
- 7h 19m
- Merged PRs (30d)
- 41
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from laravel/rust
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
laravel/rust#98 · 8 comments · 10 reactions ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neuralmagic/crucible#579 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
laravel/rust-framework#864 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
eclipse-score/kyron#249 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
seanmonstar/reqwest#3157 ·
Maintainers usually reply within 2 days