use http::header::{InvalidHeaderName, InvalidHeaderValue, ToStrError};
use js_sys::Object;
use thiserror::Error;
use wasm_bindgen::{JsCast, JsValue};
#[derive(Debug, Error)]
pub enum Error {
#[error("base64 decode error")]
Base64DecodeError(#[from] base64::DecodeError),
#[error("failed to parse headers")]
HeaderParsingError,
#[error("failed to convert header value to string")]
HeaderValueError(#[from] ToStrError),
#[error("http error")]
HttpError(#[from] http::Error),
#[error("invalid content type: {0}")]
InvalidContentType(String),
#[error("invalid header name")]
InvalidHeaderName(#[from] InvalidHeaderName),
#[error("invalid header value")]
InvalidHeaderValue(#[from] InvalidHeaderValue),
#[error("js api error: {0}")]
JsError(String),
#[error("malformed response")]
MalformedResponse,
#[error("missing content-type header in grpc response")]
MissingContentTypeHeader,
#[error("missing response body in HTTP call")]
MissingResponseBody,
#[error("grpc error")]
TonicStatusError(#[from] tonic::Status),
}
impl Error {
pub(crate) fn js_error(value: JsValue) -> Self {
let message = js_object_display(&value);
Self::JsError(message)
}
}
fn js_object_display(option: &JsValue) -> String {
let object: &Object = option.unchecked_ref();
ToString::to_string(&object.to_string())
}