Nugine / s3s

S3 Service Adapter
Apache License 2.0
132 stars 33 forks source link

Expose Error Serialization To Consumers to Allow Ergnomoic Middleware #157

Closed Eosis closed 1 month ago

Eosis commented 1 month ago

Description

As part of work I have done recently to prevent un-bounded memory growth in s3s, I put a basic middleware in front of my service that checks the signature mechanism and content-length, denying requests that will incur massive memory usage.

In doing this, I realized I would need to write my own serialization to send the error response using Hyper. This PR exposes the internal serialize_error method so it can used in consumer's middleware.

I was not quite sure how you would prefer this to be exposed / if you think this is a good idea at all. Let me know your thoughts / suggestions.


impl Service<hyper::Request<hyper::body::Incoming>> for PreCheck {
    type Response = hyper::Response<Body>;

    type Error = S3Error;

    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn call(&self, req: Req) -> Self::Future {
        if (some_check_on_req(req)) {
            // Handle the request as normal
            return self.inner.call(req);
        }

        // Oh Dear, I don't want to handle this one!
        let response = s3s::serialize_error(s3_error!(
            AccountProblem,
            "You ain't paid your bills!"
        ), true).map(Into::into);

         Box::pin(
            future::ready(
                response
            )
         )
    }
}

Caveats

This is currently built atop the PR I opened earlier, #156 .


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Eosis commented 1 month ago

CI Failure seems to be related to how I have exposed this. I will wait for general feedback on the PR before I dive into fixing that.

Eosis commented 1 month ago

I've switched to using a method on the S3Error type rather than exposing it at the top level. Let me know what you think. :+1: