Make errors' Display form more useful
This commit is contained in:
parent
f4e6a71999
commit
b7685eb6db
|
@ -2,6 +2,8 @@
|
||||||
//!
|
//!
|
||||||
//! We expose a high-level API that deals with "files."
|
//! We expose a high-level API that deals with "files."
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use rand::{distributions::Alphanumeric, Rng};
|
use rand::{distributions::Alphanumeric, Rng};
|
||||||
|
@ -40,26 +42,26 @@ type Result<T> = std::result::Result<T, Error>;
|
||||||
/// An API error.
|
/// An API error.
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("Failed to initialize the client")]
|
#[error("Failed to initialize the client: {0}")]
|
||||||
InitError(Box<dyn std::error::Error + Send + Sync>),
|
InitError(Box<dyn std::error::Error + Send + Sync>),
|
||||||
|
|
||||||
#[error("Request error")]
|
#[error("Request error: {0}")]
|
||||||
RequestError(#[from] reqwest::Error), // TODO: Better errors
|
RequestError(#[from] reqwest::Error), // TODO: Better errors
|
||||||
|
|
||||||
#[error("Failed to decode response")]
|
#[error("Failed to decode response ({status}): {error}")]
|
||||||
DecodeError {
|
DecodeError {
|
||||||
status: StatusCode,
|
status: StatusCode,
|
||||||
bytes: Bytes,
|
bytes: Bytes,
|
||||||
error: serde_json::Error,
|
error: serde_json::Error,
|
||||||
},
|
},
|
||||||
|
|
||||||
#[error("API error")]
|
#[error("API error ({status}): {info}")]
|
||||||
ApiError {
|
ApiError {
|
||||||
status: StatusCode,
|
status: StatusCode,
|
||||||
info: ApiErrorInfo,
|
info: ApiErrorInfo,
|
||||||
},
|
},
|
||||||
|
|
||||||
#[error("I/O error")]
|
#[error("I/O error: {0}")]
|
||||||
IoError(#[from] std::io::Error),
|
IoError(#[from] std::io::Error),
|
||||||
|
|
||||||
#[error("Too many collisions")]
|
#[error("Too many collisions")]
|
||||||
|
@ -195,6 +197,19 @@ impl Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ApiErrorInfo {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Unstructured(bytes) => {
|
||||||
|
write!(f, "[Unstructured] {}", String::from_utf8_lossy(bytes))
|
||||||
|
}
|
||||||
|
Self::Structured(e) => {
|
||||||
|
write!(f, "{:?}", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Api {
|
impl Api {
|
||||||
pub fn new(credentials: Credentials) -> Result<Self> {
|
pub fn new(credentials: Credentials) -> Result<Self> {
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
|
|
|
@ -12,7 +12,7 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("GitHub API error")]
|
#[error("GitHub API error: {0}")]
|
||||||
ApiError(#[from] ApiError),
|
ApiError(#[from] ApiError),
|
||||||
|
|
||||||
#[error("Not Found")]
|
#[error("Not Found")]
|
||||||
|
|
Loading…
Reference in a new issue