ServerErrorDelegate.swift 3.4 KB

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