Skip to content

Commit

Permalink
[stack]: redesign errors - 1: drop anyhow (#2155)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulmathur16 authored Jun 15, 2024
1 parent 5218d50 commit 5f4b225
Show file tree
Hide file tree
Showing 70 changed files with 857 additions and 376 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions benches/data_loader_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use criterion::Criterion;
use hyper::body::Bytes;
use reqwest::Request;
use tailcall::core::config::Batch;
use tailcall::core::error::{file, http};
use tailcall::core::http::{DataLoaderRequest, HttpDataLoader, Response};
use tailcall::core::ir::IoId;
use tailcall::core::runtime::TargetRuntime;
Expand All @@ -23,7 +24,7 @@ struct MockHttpClient {

#[async_trait::async_trait]
impl HttpIO for MockHttpClient {
async fn execute(&self, _req: Request) -> anyhow::Result<Response<Bytes>> {
async fn execute(&self, _req: Request) -> Result<Response<Bytes>, http::Error> {
Ok(Response::empty())
}
}
Expand All @@ -39,11 +40,11 @@ struct File;

#[async_trait::async_trait]
impl FileIO for File {
async fn write<'a>(&'a self, _: &'a str, _: &'a [u8]) -> anyhow::Result<()> {
async fn write<'a>(&'a self, _: &'a str, _: &'a [u8]) -> Result<(), file::Error> {
unimplemented!("Not needed for this bench")
}

async fn read<'a>(&'a self, _: &'a str) -> anyhow::Result<String> {
async fn read<'a>(&'a self, _: &'a str) -> Result<String, file::Error> {
unimplemented!("Not needed for this bench")
}
}
Expand Down
7 changes: 4 additions & 3 deletions benches/impl_path_string_for_evaluation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use reqwest::{Client, Request};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use tailcall::core::blueprint::{Server, Upstream};
use tailcall::core::cache::InMemoryCache;
use tailcall::core::error::{file, http};
use tailcall::core::http::{RequestContext, Response};
use tailcall::core::ir::{EvaluationContext, ResolverContextLike};
use tailcall::core::path::PathString;
Expand Down Expand Up @@ -70,7 +71,7 @@ impl Http {

#[async_trait]
impl HttpIO for Http {
async fn execute(&self, mut request: Request) -> anyhow::Result<Response<Bytes>> {
async fn execute(&self, mut request: Request) -> Result<Response<Bytes>, http::Error> {
if self.http2_only {
*request.version_mut() = reqwest::Version::HTTP_2;
}
Expand All @@ -90,11 +91,11 @@ impl EnvIO for Env {
struct File;
#[async_trait]
impl FileIO for File {
async fn write<'a>(&'a self, _: &'a str, _: &'a [u8]) -> anyhow::Result<()> {
async fn write<'a>(&'a self, _: &'a str, _: &'a [u8]) -> Result<(), file::Error> {
unimplemented!("Not needed for this bench")
}

async fn read<'a>(&'a self, _: &'a str) -> anyhow::Result<String> {
async fn read<'a>(&'a self, _: &'a str) -> Result<String, file::Error> {
unimplemented!("Not needed for this bench")
}
}
Expand Down
104 changes: 97 additions & 7 deletions src/cli/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,100 @@
use crate::core::Errata;
use std::string::FromUtf8Error;

impl From<rustls::Error> for Errata {
fn from(error: rustls::Error) -> Self {
let cli_error = Errata::new("Failed to create TLS Acceptor");
let message = error.to_string();
use derive_more::From;
use inquire::InquireError;
use opentelemetry::logs::LogError;
use opentelemetry::metrics::MetricsError;
use opentelemetry::trace::TraceError;
use tokio::task::JoinError;

cli_error.description(message)
}
use crate::core::rest;
use crate::core::valid::ValidationError;

#[derive(From, thiserror::Error, Debug)]
pub enum Error {
#[error("Metrics Error")]
Metrics(MetricsError),

#[error("Rest Error")]
Rest(rest::error::Error),

#[error("Serde Json Error")]
SerdeJson(serde_json::Error),

#[error("IO Error")]
IO(std::io::Error),

#[error("Telemetry Trace Error : {0}")]
TelemetryTrace(String),

#[error("Failed to send message")]
MessageSendFailure,

#[error("Hyper Error")]
Hyper(hyper::Error),

#[error("Rustls Error")]
Rustls(rustls::Error),

#[error("Join Error")]
Join(JoinError),

#[error("Opentelemetry Global Error")]
OpentelemetryGlobal(opentelemetry::global::Error),

#[error("Trace Error")]
Trace(TraceError),

#[error("Log Error")]
Log(LogError),

#[error("Utf8 Error")]
Utf8(FromUtf8Error),

#[error("Inquire Error")]
Inquire(InquireError),

#[error("Serde Yaml Error")]
SerdeYaml(serde_yaml::Error),

#[error("Invalid Header Name")]
InvalidHeaderName(hyper::header::InvalidHeaderName),

#[error("Invalid Header Value")]
InvalidHeaderValue(hyper::header::InvalidHeaderValue),

#[error("rquickjs Error")]
RQuickjs(rquickjs::Error),

#[error("Trying to reinitialize an already initialized QuickJS runtime")]
ReinitializeQuickjsRuntime,

#[error("Runtime not initialized")]
RuntimeNotInitialized,

#[error("Deserialize Failed")]
DeserializeFailed,

#[error("Not a function error")]
InvalidFunction,

#[error("Init Process Observer Error")]
InitProcessObserver,

#[error("JS Runtime is stopped")]
JsRuntimeStopped,

#[error("Rustls internal error")]
RustlsInternal,

#[error("Reqwest middleware error")]
ReqwestMiddleware(reqwest_middleware::Error),

#[error("Reqwest error")]
Reqwest(reqwest::Error),

#[error("Validation Error : {0}")]
Validation(ValidationError<std::string::String>),
}

pub type Result<A> = std::result::Result<A, Error>;
9 changes: 5 additions & 4 deletions src/cli/javascript/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ impl<'js> FromJs<'js> for WorkerResponse {
mod test {
use std::collections::BTreeMap;

use anyhow::Result;
use headers::{HeaderName, HeaderValue};
use hyper::body::Bytes;
use pretty_assertions::assert_eq;
Expand All @@ -214,18 +213,19 @@ mod test {
use rquickjs::{Context, FromJs, IntoJs, Object, Runtime, String as JsString};

use super::*;
use crate::core::error::worker;
use crate::core::http::Response;
use crate::core::worker::{Command, WorkerRequest, WorkerResponse};

fn create_test_response() -> Result<WorkerResponse> {
fn create_test_response() -> Result<WorkerResponse, worker::Error> {
let mut headers = HeaderMap::new();
headers.insert("content-type", "application/json".parse().unwrap());
let response = crate::core::http::Response {
status: reqwest::StatusCode::OK,
headers,
body: Bytes::from("Hello, World!"),
};
let js_response: Result<WorkerResponse> = response.try_into();
let js_response: Result<WorkerResponse, worker::Error> = response.try_into();
js_response
}

Expand All @@ -245,7 +245,8 @@ mod test {
#[test]
fn test_from_js_response() {
let js_response = create_test_response().unwrap();
let response: Result<crate::core::http::Response<Bytes>> = js_response.try_into();
let response: Result<crate::core::http::Response<Bytes>, worker::Error> =
js_response.try_into();
assert!(response.is_ok());
let response = response.unwrap();
assert_eq!(response.status, reqwest::StatusCode::OK);
Expand Down
5 changes: 2 additions & 3 deletions src/cli/javascript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod runtime;

pub use runtime::Runtime;

use crate::cli::Result;
use crate::core::{blueprint, WorkerIO};

pub fn init_worker_io<T, V>(script: blueprint::Script) -> Arc<dyn WorkerIO<T, V> + Send + Sync>
Expand All @@ -18,9 +19,7 @@ where
(Arc::new(Runtime::new(script))) as _
}

fn create_header_map(
headers: BTreeMap<String, String>,
) -> anyhow::Result<reqwest::header::HeaderMap> {
fn create_header_map(headers: BTreeMap<String, String>) -> Result<reqwest::header::HeaderMap> {
let mut header_map = reqwest::header::HeaderMap::new();
for (key, value) in headers.iter() {
let key = HeaderName::from_bytes(key.as_bytes())?;
Expand Down
Loading

0 comments on commit 5f4b225

Please sign in to comment.