Skip to content

Conform Response HTTPError or even Response to HTTPResponseConvertible? (Feature Request) #26

@carlynorama

Description

@carlynorama

OpenAPI has a introduced an ErrorHandlingMiddleware

One needs to conform the errors in ones server to HTTPResponseConvertible to work with it by creating separate status, header and body var conformances of types chosen by the OpenAPI generator.

public protocol HTTPResponseConvertible {

    /// An HTTP status to return in the response.
    var httpStatus: HTTPResponse.Status { get }

    /// The HTTP header fields of the response.
    /// This is optional as default values are provided in the extension.
    var httpHeaderFields: HTTPTypes.HTTPFields { get }

    /// The body of the HTTP response.
    var httpBody: OpenAPIRuntime.HTTPBody? { get }
}

The body conformance is the tricky one because it isn't an underlying SwiftNIO type, but an OpenAPIRuntime type.

It's unclear how common it will be for people to want to use Hummingbird generated HTTPError's or Responses but perhaps this package could either:

  • Extend OpenAPIRuntime.HTTPBody init to take a ResponseBody or a ByteBuffer. It takes A LOT of things already, but nothing a ResponseBody can offer.
  • Extend ResponseBody with a function to provide a OpenAPIRuntime.HTTPBody or something an existing OpenAPIRuntime.HTTPBody init does take.

Then the dev could conform response types as they choose.

Additionally, it's also unclear to this dev how else the OpenAPI Generator folks intend to use this conformance elsewhere / in other middleware.

It could be beneficial to conform Response or at least HTTPError preemptively?

As a note, the OpenAPI generator throws a lot of 500 errors (!!) which this middleware was created to let devs address in their own projects. To catch the early ones it requires extending DecoderError:

It could be a nice touch if this plugin caught those and returned the same default errors the rest of Hummingbird does (while of course allowing overrides on a specific project to match what's spec'd by the API). It might be as simple as creating a conformance to HTTPError in this Package and then the middleware will respond appropriately to the existing Hummingbird Decoding Error handlers?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions