Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,53 @@ cargo run --example token_generation
cargo run --example api_calls
```

## Error Handling

SDK methods return `openapi_sdk::Result<T>`, whose error type is `openapi_sdk::Error`.
If your application already has its own error enum, add a `From<openapi_sdk::Error>`
implementation and the `?` operator will work in your existing `Result` pipeline:

```rust
use openapi_sdk::{Client, Error as OpenapiError};
use std::error::Error;
use std::fmt;

#[derive(Debug)]
enum MyError {
Openapi(OpenapiError),
}

impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Openapi(error) => write!(f, "{error}"),
}
}
}

impl Error for MyError {}

impl From<OpenapiError> for MyError {
fn from(error: OpenapiError) -> Self {
Self::Openapi(error)
}
}

async fn fetch_users() -> Result<String, MyError> {
let client = Client::new("<your_access_token>".to_string())?;
let users = client
.request::<serde_json::Value>(
"GET",
"https://test.imprese.openapi.it/advance",
None,
None,
)
.await?;

Ok(users)
}
```

## Testing

Run tests with:
Expand Down
47 changes: 47 additions & 0 deletions docs/crates-io.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,53 @@ cargo run --example token_generation
cargo run --example api_calls
```

## Error Handling

SDK methods return `openapi_sdk::Result<T>`, whose error type is `openapi_sdk::Error`.
If your application already has its own error enum, add a `From<openapi_sdk::Error>`
implementation and the `?` operator will work in your existing `Result` pipeline:

```rust
use openapi_sdk::{Client, Error as OpenapiError};
use std::error::Error;
use std::fmt;

#[derive(Debug)]
enum MyError {
Openapi(OpenapiError),
}

impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Openapi(error) => write!(f, "{error}"),
}
}
}

impl Error for MyError {}

impl From<OpenapiError> for MyError {
fn from(error: OpenapiError) -> Self {
Self::Openapi(error)
}
}

async fn fetch_users() -> Result<String, MyError> {
let client = Client::new("<your_access_token>".to_string())?;
let users = client
.request::<serde_json::Value>(
"GET",
"https://test.imprese.openapi.it/advance",
None,
None,
)
.await?;

Ok(users)
}
```

## Testing

Run tests with:
Expand Down
2 changes: 1 addition & 1 deletion examples/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("POST API Response: {}", result);

Ok(())
}
}
2 changes: 1 addition & 1 deletion examples/token_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("Token created successfully!");
Ok(())
}
}
Loading