Coverage Report

Created: 2026-03-11 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/grpc-swift/Sources/GRPC/ServerErrorDelegate.swift
Line
Count
Source
1
/*
2
 * Copyright 2019, 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 Foundation
17
import NIOCore
18
import NIOHPACK
19
import NIOHTTP1
20
21
public protocol ServerErrorDelegate: AnyObject {
22
  //! FIXME: Provide more context about where the error was thrown, i.e. using `GRPCError`.
23
  /// Called when an error is thrown in the channel pipeline.
24
  func observeLibraryError(_ error: Error)
25
26
  /// Transforms the given error (thrown somewhere inside the gRPC library) into a new error.
27
  ///
28
  /// This allows library users to transform errors which may be out of their control
29
  /// into more meaningful ``GRPCStatus`` errors before they are sent to the user.
30
  ///
31
  /// - note:
32
  /// Errors returned by this method are not passed to ``observeLibraryError(_:)-5wuhj`` again.
33
  ///
34
  /// - note:
35
  /// This defaults to returning `nil`. In that case, if the original error conforms to ``GRPCStatusTransformable``,
36
  /// that error's ``GRPCStatusTransformable/makeGRPCStatus()`` result will be sent to the user. If that's not the case, either,
37
  /// ``GRPCStatus/processingError`` is returned.
38
  func transformLibraryError(_ error: Error) -> GRPCStatusAndTrailers?
39
40
  /// Called when a request's status or response promise is failed somewhere in the user-provided request handler code.
41
  /// - Parameters:
42
  ///   - error: The original error the status/response promise was failed with.
43
  ///   - headers: The headers of the request whose status/response promise was failed.
44
  func observeRequestHandlerError(_ error: Error, headers: HPACKHeaders)
45
46
  /// Transforms the given status or response promise failure into a new error.
47
  ///
48
  /// This allows library users to transform errors which happen during their handling of the request
49
  /// into more meaningful ``GRPCStatus`` errors before they are sent to the user.
50
  ///
51
  /// - note:
52
  /// Errors returned by this method are not passed to `observe` again.
53
  ///
54
  /// - note:
55
  /// This defaults to returning `nil`. In that case, if the original error conforms to ``GRPCStatusTransformable``,
56
  /// that error's ``GRPCStatusTransformable/makeGRPCStatus()`` result will be sent to the user. If that's not the case, either,
57
  /// ``GRPCStatus/processingError`` is returned.
58
  ///
59
  /// - Parameters:
60
  ///   - error: The original error the status/response promise was failed with.
61
  ///   - headers: The headers of the request whose status/response promise was failed.
62
  func transformRequestHandlerError(
63
    _ error: Error,
64
    headers: HPACKHeaders
65
  ) -> GRPCStatusAndTrailers?
66
}
67
68
extension ServerErrorDelegate {
69
0
  public func observeLibraryError(_ error: Error) {}
70
71
0
  public func transformLibraryError(_ error: Error) -> GRPCStatusAndTrailers? {
72
0
    return nil
73
0
  }
74
75
0
  public func observeRequestHandlerError(_ error: Error, headers: HPACKHeaders) {}
76
77
  public func transformRequestHandlerError(
78
    _ error: Error,
79
    headers: HPACKHeaders
80
0
  ) -> GRPCStatusAndTrailers? {
81
0
    return nil
82
0
  }
83
}