/src/grpc-swift/Sources/GRPC/ServerChannelErrorHandler.swift
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020, gRPC Authors All rights reserved. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | import NIOCore |
17 | | |
18 | | /// A handler that passes errors thrown into the server channel to the server error delegate. |
19 | | /// |
20 | | /// A NIO server bootstrap produces two kinds of channels. The first and most common is the "child" channel: |
21 | | /// each of these corresponds to one connection, and has the connection state stored on it. The other kind is |
22 | | /// the "server" channel. Each bootstrap produces only one of these, and it is the channel that owns the listening |
23 | | /// socket. |
24 | | /// |
25 | | /// This channel handler is inserted into the server channel, and is responsible for passing any errors in that pipeline |
26 | | /// to the server error delegate. If there is no error delegate, this handler is not inserted into the pipeline. |
27 | | final class ServerChannelErrorHandler { |
28 | | private let errorDelegate: ServerErrorDelegate |
29 | | |
30 | 0 | init(errorDelegate: ServerErrorDelegate) { |
31 | 0 | self.errorDelegate = errorDelegate |
32 | 0 | } |
33 | | } |
34 | | |
35 | | extension ServerChannelErrorHandler: ChannelInboundHandler { |
36 | | typealias InboundIn = Any |
37 | | typealias InboundOut = Any |
38 | | |
39 | 0 | func errorCaught(context: ChannelHandlerContext, error: Error) { |
40 | 0 | // This handler does not treat errors as fatal to the listening socket, as it's possible they were transiently |
41 | 0 | // occurring in a single connection setup attempt. |
42 | 0 | self.errorDelegate.observeLibraryError(error) |
43 | 0 | context.fireErrorCaught(error) |
44 | 0 | } |
45 | | } |