Skip to content

Commit

Permalink
Better HTTP error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Dec 9, 2023
1 parent 7f202a8 commit e2780e0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rbxcloud"
version = "0.6.0"
version = "0.6.1"
description = "CLI and SDK for the Roblox Open Cloud APIs"
authors = ["Stephen Leitnick"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ The library built for the CLI tool is available to use directly in Rust projects
To use `rbxcloud` in a Rust project, simply add `rbxcloud` to the `Cargo.toml` dependency list.
```toml
[dependencies]
rbxcloud = "0.6.0"
rbxcloud = "0.6.1"
```

Alternatively, use `cargo add`.
Expand Down
4 changes: 2 additions & 2 deletions docs/cli/cli-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ There are a few different ways to install the `rbxcloud` CLI.
### [Aftman](https://github.com/LPGhatguy/aftman) <small>(Preferred)</small>
Run the `aftman add` command within your project directory. This will add `rbxcloud` to the project's `aftman.toml` file (or create one if it doesn't yet exist).
```sh
$ aftman add Sleitnick/[email protected].0
$ aftman add Sleitnick/[email protected].1
```

Next, run `aftman install` to install `rbxcloud`.
Expand All @@ -17,7 +17,7 @@ Add `rbxcloud` under the `[tools]` section of your `foreman.toml` file.
```toml
# foreman.toml
[tools]
rbxcloud = { github = "Sleitnick/rbxcloud", version = "0.6.0" }
rbxcloud = { github = "Sleitnick/rbxcloud", version = "0.6.1" }
```

Next, run `foreman install` to install `rbxcloud`.
Expand Down
2 changes: 1 addition & 1 deletion docs/lib/lib-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
To use `rbxcloud` in a Rust project, simply add `rbxcloud` to the `Cargo.toml` dependency list.
```toml
[dependencies]
rbxcloud = "0.6.0"
rbxcloud = "0.6.1"
```

Alternatively, use `cargo add`.
Expand Down
59 changes: 25 additions & 34 deletions src/rbx/experience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ pub struct PublishExperienceResponse {
pub version_number: u64,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct PublishExperienceErrorResponse {
message: String,
}

/// Publish a place under a specific experience.
pub async fn publish_experience(
params: &PublishExperienceParams,
Expand All @@ -55,6 +61,7 @@ pub async fn publish_experience(
placeId=params.place_id,
versionType=params.version_type,
);

let res = client
.post(url)
.header("x-api-key", &params.api_key)
Expand All @@ -63,44 +70,28 @@ pub async fn publish_experience(
.send()
.await?;
let status = res.status();

if !status.is_success() {
let code = status.as_u16();
if code == 400 {
return Err(Error::HttpStatusError {
code,
msg: "invalid request or file content".to_string(),
});
} else if code == 401 {
return Err(Error::HttpStatusError {
code,
msg: "api key not valid for operation".to_string(),
});
} else if code == 403 {
return Err(Error::HttpStatusError {
code,
msg: "publish not allowed on place".to_string(),
});
} else if code == 404 {
return Err(Error::HttpStatusError {
code,
msg: "place or universe does not exist".to_string(),
});
} else if code == 409 {
return Err(Error::HttpStatusError {
code,
msg: "place not part of the universe".to_string(),
});
} else if code == 500 {
return Err(Error::HttpStatusError {
let msg_text = res.text().await;
let msg_json = msg_text
.map(|txt| {
serde_json::from_str::<PublishExperienceErrorResponse>(txt.as_str())
.unwrap_or(PublishExperienceErrorResponse { message: txt })
})
.map(|err| err.message);

let msg = msg_json.unwrap_or(status.canonical_reason().unwrap_or_default().to_string());

return match code {
400 | 401 | 403 | 404 | 409 | 500 => Err(Error::HttpStatusError { code, msg }),
_ => Err(Error::HttpStatusError {
code,
msg: "internal server error".to_string(),
});
}
return Err(Error::HttpStatusError {
code,
msg: status.canonical_reason().unwrap_or_default().to_string(),
});
msg: status.canonical_reason().unwrap_or_default().to_string(),
}),
};
}

let body = res.json::<PublishExperienceResponse>().await?;
Ok(body)
}

0 comments on commit e2780e0

Please sign in to comment.