Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

Commit 66fd193

Browse files
committedDec 1, 2024
feat: pretty-print api errors (#593)
Fixes RVT-4139
1 parent d7a6508 commit 66fd193

File tree

7 files changed

+84
-17
lines changed

7 files changed

+84
-17
lines changed
 

‎packages/cli/src/commands/actor/create.rs

-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ impl Opts {
231231
build: build_id,
232232
build_tags: build_tags.map(|bt| Some(serde_json::json!(bt))),
233233
runtime: Box::new(models::ActorCreateActorRuntimeRequest {
234-
arguments: None,
235234
environment: env_vars,
236235
}),
237236
network: Some(Box::new(models::ActorCreateActorNetworkRequest {

‎packages/cli/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ async fn main_async() -> ExitCode {
6464
// Don't print anything, already handled
6565
} else if let Some(err) = err.downcast_ref::<errors::UserError>() {
6666
// Don't report error since this is a user error
67-
eprintln!("{err}");
67+
eprintln!("\n{err}");
6868
} else {
6969
// This is an internal error, report error
70-
eprintln!("{err}");
70+
eprintln!("\n{err}");
7171
report_error(err).await;
7272
}
7373

‎scripts/sdk/copy.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-env
1+
#!/usr/bin/env -S deno run -A
22

33
import { assertExists } from "jsr:@std/assert";
44
import { emptyDir, copy } from "jsr:@std/fs";
@@ -12,12 +12,27 @@ await emptyDir(rustSdkDir);
1212

1313
const eeRepoPath = Deno.env.get("EE_REPO_PATH");
1414
assertExists(eeRepoPath, "EE_REPO_PATH environment variable is not set");
15-
await copy(join(eeRepoPath, "sdks", "full", "rust"), rustSdkDir, { overwrite: true });
15+
await copy(join(eeRepoPath, "sdks", "api", "full", "rust"), rustSdkDir, {
16+
overwrite: true,
17+
});
1618

1719
let cargoToml = await Deno.readTextFile(cargoTomlPath);
1820
cargoToml = cargoToml.replace(
1921
/\[dependencies\.reqwest\]/,
20-
"[dependencies.reqwest]\ndefault-features = false"
22+
"[dependencies.reqwest]\ndefault-features = false",
2123
);
2224
await Deno.writeTextFile(cargoTomlPath, cargoToml);
2325

26+
const modRsPath = join(rustSdkDir, "src", "apis", "mod.rs");
27+
const patchFilePath = "./scripts/sdk/error.patch";
28+
29+
const patchProcess = new Deno.Command("patch", {
30+
args: [modRsPath, patchFilePath],
31+
stdout: "inherit",
32+
stderr: "inherit",
33+
});
34+
const { success } = await patchProcess.output();
35+
if (!success) {
36+
console.error("Failed to apply patch");
37+
Deno.exit(1);
38+
}

‎scripts/sdk/error.patch

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
diff --git a/sdks/rust/src/apis/mod.rs b/sdks/rust/src/apis/mod.rs
2+
index 73ed6261..caa52f60 100644
3+
--- a/sdks/rust/src/apis/mod.rs
4+
+++ b/sdks/rust/src/apis/mod.rs
5+
@@ -16,16 +16,33 @@ pub enum Error<T> {
6+
ResponseError(ResponseContent<T>),
7+
}
8+
9+
+#[derive(serde::Deserialize)]
10+
+pub struct RivetErrorBody {
11+
+ pub code: String,
12+
+ pub message: String,
13+
+ pub documentation: Option<String>,
14+
+}
15+
+
16+
impl<T> fmt::Display for Error<T> {
17+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18+
let (module, e) = match self {
19+
Error::Reqwest(e) => ("reqwest", e.to_string()),
20+
Error::Serde(e) => ("serde", e.to_string()),
21+
Error::Io(e) => ("IO", e.to_string()),
22+
- Error::ResponseError(e) => (
23+
- "response",
24+
- format!("status code {}\n{}", e.status, e.content),
25+
- ),
26+
+ Error::ResponseError(e) => {
27+
+ if let Ok(body) = serde_json::from_str::<RivetErrorBody>(&e.content) {
28+
+ write!(f, "{}", body.message)?;
29+
+ if let Some(docs) = &body.documentation {
30+
+ write!(f, "\n{docs}")?;
31+
+ }
32+
+ return Ok(());
33+
+ }
34+
+
35+
+ (
36+
+ "response",
37+
+ format!("status code {}\n{}", e.status, e.content),
38+
+ )
39+
+ }
40+
};
41+
write!(f, "error in {}: {}", module, e)
42+
}

‎sdks/rust/docs/ActorCreateActorRuntimeRequest.md

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎sdks/rust/src/apis/mod.rs

+21-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎sdks/rust/src/models/actor_create_actor_runtime_request.rs

+1-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
This repository has been archived.