vercel_blob/
error.rs

1//! Error handling utilities for the crate
2use thiserror::Error;
3
4use reqwest::Error as ReqwestError;
5
6/// All errors raised by this crate will be instances of VercelBlobError
7#[derive(Error, Debug)]
8pub enum VercelBlobError {
9    #[error("No authentication token. Expected environment variable BLOB_READ_WRITE_TOKEN to contain a token")]
10    NotAuthenticated(),
11    #[error("Invalid request: {0}")]
12    BadRequest(String),
13    #[error("Access denied, please provide a valid token for this resource")]
14    Forbidden(),
15    #[error("The requested store does not exist")]
16    StoreNotFound(),
17    #[error("The requested store has been suspended")]
18    StoreSuspended(),
19    #[error("The requested blob does not exist")]
20    BlobNotFound(),
21    #[error("Internal HTTP error: {0}")]
22    HttpError(#[from] ReqwestError),
23    #[error("Unknown error, please visit https://vercel.com/help ({0}): {1}")]
24    UnknownError(u16, String),
25    #[error("Invalid input: {0}")]
26    InvalidInput(String),
27}
28
29impl VercelBlobError {
30    pub fn unknown_error(status_code: reqwest::StatusCode) -> Self {
31        VercelBlobError::UnknownError(
32            status_code.as_u16(),
33            status_code
34                .canonical_reason()
35                .unwrap_or("Unknown Error")
36                .to_string(),
37        )
38    }
39
40    pub fn required(field_name: &str) -> Self {
41        VercelBlobError::InvalidInput(format!("{} is required", field_name))
42    }
43}
44
45pub(crate) type Result<T> = std::result::Result<T, VercelBlobError>;