Repository: apple/swift-nio
File: Sources/NIOTestUtils/NIOHTTP1TestServer.swift (Swift)

Code before:
            }
            self.state = .waitingForChannel(promise)
            return
        case .waitingForChannel:
            preconditionFailure("illegal state \(self.state)")
        case .stopped:
            return
        }

        assert(self.currentClientChannel == nil)
        self.currentClientChannel = channel
        channel.closeFuture.whenSuccess {
            self.currentClientChannel = nil
            self.handleChannels()
            return
        }
        do {
            try channel.pipeline.syncOperations.configureHTTPServerPipeline()
            if self.aggregateBody {
                try channel.pipeline.syncOperations.addHandler(AggregateBodyHandler())
            }
            try channel.pipeline.syncOperations.addHandler(WebServerHandler(webServer: self))
            try channel.pipeline.syncOperations.addHandler(TransformerHandler())
            _ = try channel.syncOptions!.setOption(.autoRead, value: true)
        } catch {
            assertionFailure("Channel initialization failed with: \(error)")
        }
    }

    public convenience init(group: EventLoopGroup) {
        self.init(group: group, aggregateBody: true)
    }

    public init(group: EventLoopGroup, aggregateBody: Bool) {
        self.eventLoop = group.next()
        self.aggregateBody = aggregateBody

        self.serverChannel = try! ServerBootstrap(group: self.eventLoop)
            .childChannelOption(.autoRead, value: false)
            .childChannelInitializer { channel in
                switch self.state {
                case .channelsAvailable(var channels):
                    channels.append(channel)
                    self.state = .channelsAvailable(channels)
                case .waitingForChannel(let promise):
                    self.state = .channelsAvailable([channel])
                    promise.succeed(())
                case .idle:
                    self.state = .channelsAvailable([channel])
                case .stopped:
                    channel.close(promise: nil)

Review comment (suggestion):
Let's just fully `fatalError` here, no reason to be shy.

Code after:
            }
            self.state = .waitingForChannel(promise)
            return
        case .waitingForChannel:
            preconditionFailure("illegal state \(self.state)")
        case .stopped:
            return
        }

        assert(self.currentClientChannel == nil)
        self.currentClientChannel = channel
        channel.closeFuture.whenSuccess {
            self.currentClientChannel = nil
            self.handleChannels()
            return
        }
        do {
            try channel.pipeline.syncOperations.configureHTTPServerPipeline()
            if self.aggregateBody {
                try channel.pipeline.syncOperations.addHandler(AggregateBodyHandler())
            }
            try channel.pipeline.syncOperations.addHandler(WebServerHandler(webServer: self))
            try channel.pipeline.syncOperations.addHandler(TransformerHandler())
            _ = try channel.syncOptions!.setOption(.autoRead, value: true)
        } catch {
            fatalError("Channel initialization failed with: \(error)")
        }
    }

    public convenience init(group: EventLoopGroup) {
        self.init(group: group, aggregateBody: true)
    }

    public init(group: EventLoopGroup, aggregateBody: Bool) {
        self.eventLoop = group.next()
        self.aggregateBody = aggregateBody

        self.serverChannel = try! ServerBootstrap(group: self.eventLoop)
            .childChannelOption(.autoRead, value: false)
            .childChannelInitializer { channel in
                switch self.state {
                case .channelsAvailable(var channels):
                    channels.append(channel)
                    self.state = .channelsAvailable(channels)
                case .waitingForChannel(let promise):
                    self.state = .channelsAvailable([channel])
                    promise.succeed(())
                case .idle:
                    self.state = .channelsAvailable([channel])
                case .stopped:
                    channel.close(promise: nil)