def METHOD_NAME(
    config: CoreConfig, event_sender: AnalyticsEventSender
) -> Callable[[Request, RequestHandler], Awaitable[StreamResponse]]:
    is_debug = (logging.root.level < logging.INFO) or config.runtime.debug
    def exc_info(ex: Exception) -> Optional[Exception]:
        return ex if is_debug else None
    @middleware
    async def error_handler_middleware(request: Request, handler: RequestHandler) -> StreamResponse:
        def message_from_error(e: Exception) -> Tuple[str, str, str]:
            kind = type(e).__name__
            ex_str = str(e)
            return f"Error: {kind}\nMessage: {ex_str}", kind, ex_str
        try:
            return await handler(request)
        except HTTPRedirection as e:
            # redirects are implemented as exceptions in aiohttp for whatever reason...
            raise e
        except HTTPError as e:
            log.warning(f"Request {request} has failed with exception: {str(e)}")
            raise e
        except NotFoundError as e:
            message, _, _ = message_from_error(e)
            log.info(f"Request {request} has failed with exception: {message}", exc_info=exc_info(e))
            raise HTTPNotFound(text=message) from e
        except (ClientError, AttributeError) as e:
            message, kind, ex_str = message_from_error(e)
            log.info(f"Request {request} has failed with exception: {message}", exc_info=exc_info(e))
            await event_sender.core_event(
                CoreEvent.ClientError, {"version": version(), "kind": kind, "message": ex_str}
            )
            raise HTTPBadRequest(text=message) from e
        except Exception as e:
            message, kind, ex_str = message_from_error(e)
            log.warning(f"Request {request} has failed with exception: {message}", exc_info=exc_info(e))
            await event_sender.core_event(
                CoreEvent.ServerError, {"version": version(), "kind": kind, "message": ex_str}
            )
            raise HTTPBadRequest(text=message) from e
    return error_handler_middleware