Skip to content

Should middleware use a "next" function? #17

Description

@alshdavid

Currently middleware uses this syntax:

async fn my_middleware<Ctx: 'static + Clone + Send + Sync>(
  req: uhttp::Request, 
  res: uhttp::Response, 
  mut ctx: Ctx
) -> uhttp::Result<Option(uhttp::Request, uhttp::Response, Ctx)> 
{
  Ok(Some(req, res, ctx)) // Next middleware
}

Perhaps it's a good idea to use a "next" function so middleware can support custom error handling. Something like this (not exactly)

async fn my_middleware<Ctx: 'static + Clone + Send + Sync>(
  req: uhttp::Request, 
  res: uhttp::Response, 
  mut ctx: Ctx, next: uhttp::router::MiddlewareFunc<Ctx>,
  next: /* to be decided */
) -> uhttp::Result<()>
{
  // Next middleware 
  match next(req, res, ctx) {
    Ok(_) => Ok(()),
    Err(err) => {
      // Handle error if a handler errors out
      res.write_head(StatusCode::INTERNAL_SERVER_ERROR).await?;
      return Ok(())
    }
  }
}

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