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

Commit

Permalink
feat: pretty-print api errors (#593)
Browse files Browse the repository at this point in the history
Fixes RVT-4139
  • Loading branch information
NathanFlurry committed Dec 1, 2024
1 parent d7a6508 commit 66fd193
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 17 deletions.
1 change: 0 additions & 1 deletion packages/cli/src/commands/actor/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ impl Opts {
build: build_id,
build_tags: build_tags.map(|bt| Some(serde_json::json!(bt))),
runtime: Box::new(models::ActorCreateActorRuntimeRequest {
arguments: None,
environment: env_vars,
}),
network: Some(Box::new(models::ActorCreateActorNetworkRequest {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ async fn main_async() -> ExitCode {
// Don't print anything, already handled
} else if let Some(err) = err.downcast_ref::<errors::UserError>() {
// Don't report error since this is a user error
eprintln!("{err}");
eprintln!("\n{err}");
} else {
// This is an internal error, report error
eprintln!("{err}");
eprintln!("\n{err}");
report_error(err).await;
}

Expand Down
21 changes: 18 additions & 3 deletions scripts/sdk/copy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-env
#!/usr/bin/env -S deno run -A

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

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

let cargoToml = await Deno.readTextFile(cargoTomlPath);
cargoToml = cargoToml.replace(
/\[dependencies\.reqwest\]/,
"[dependencies.reqwest]\ndefault-features = false"
"[dependencies.reqwest]\ndefault-features = false",
);
await Deno.writeTextFile(cargoTomlPath, cargoToml);

const modRsPath = join(rustSdkDir, "src", "apis", "mod.rs");
const patchFilePath = "./scripts/sdk/error.patch";

const patchProcess = new Deno.Command("patch", {
args: [modRsPath, patchFilePath],
stdout: "inherit",
stderr: "inherit",
});
const { success } = await patchProcess.output();
if (!success) {
console.error("Failed to apply patch");
Deno.exit(1);
}
42 changes: 42 additions & 0 deletions scripts/sdk/error.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
diff --git a/sdks/rust/src/apis/mod.rs b/sdks/rust/src/apis/mod.rs
index 73ed6261..caa52f60 100644
--- a/sdks/rust/src/apis/mod.rs
+++ b/sdks/rust/src/apis/mod.rs
@@ -16,16 +16,33 @@ pub enum Error<T> {
ResponseError(ResponseContent<T>),
}

+#[derive(serde::Deserialize)]
+pub struct RivetErrorBody {
+ pub code: String,
+ pub message: String,
+ pub documentation: Option<String>,
+}
+
impl<T> fmt::Display for Error<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (module, e) = match self {
Error::Reqwest(e) => ("reqwest", e.to_string()),
Error::Serde(e) => ("serde", e.to_string()),
Error::Io(e) => ("IO", e.to_string()),
- Error::ResponseError(e) => (
- "response",
- format!("status code {}\n{}", e.status, e.content),
- ),
+ Error::ResponseError(e) => {
+ if let Ok(body) = serde_json::from_str::<RivetErrorBody>(&e.content) {
+ write!(f, "{}", body.message)?;
+ if let Some(docs) = &body.documentation {
+ write!(f, "\n{docs}")?;
+ }
+ return Ok(());
+ }
+
+ (
+ "response",
+ format!("status code {}\n{}", e.status, e.content),
+ )
+ }
};
write!(f, "error in {}: {}", module, e)
}
1 change: 0 additions & 1 deletion sdks/rust/docs/ActorCreateActorRuntimeRequest.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions sdks/rust/src/apis/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions sdks/rust/src/models/actor_create_actor_runtime_request.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 66fd193

Please sign in to comment.