2
0

ServerErrorDelegate.swift 3.9 KB

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