Files
swift-nio/Sources/NIO/NIOCloseOnErrorHandler.swift
T
Romain Pouclet d093ae0659 Add a Channel Handler that closes the channel (#967)
Motivation:
Because of #600, many users will start leaking Channels because they
don't close on error.

Modifications:

* Add ChannelHandlerError
* Add ChannelHandlerErrorTest

Result:

User will have access to an handler to use to automatically close
the channel in case of unhandled errors
2019-04-30 12:06:24 +01:00

29 lines
921 B
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2019 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// A `ChannelInboundHandler` that closes the channel when an error is caught
public final class NIOCloseOnErrorHandler: ChannelInboundHandler {
public typealias InboundIn = NIOAny
/// Initialize a `NIOCloseOnErrorHandler`
public init() {}
public func errorCaught(context: ChannelHandlerContext, error: Error) {
context.fireErrorCaught(error)
context.close(promise: nil)
}
}