streamline middleware error handling

Uses the convention where a middleware with an arity of 4 is assumed to be the
error handler.