ServerErrorDelegate.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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: class {
  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(*, deprecated, message: "Please use the new transformLibraryError that returns a GRPCStatusAndMetadata instead.")
  37. func transformLibraryError(_ error: Error) -> GRPCStatus?
  38. /// Called when a request's status or response promise is failed somewhere in the user-provided request handler code.
  39. /// - Parameters:
  40. /// - error: The original error the status/response promise was failed with.
  41. /// - request: The headers of the request whose status/response promise was failed.
  42. func observeRequestHandlerError(_ error: Error, request: HTTPRequestHead)
  43. /// Transforms the given status or response promise failure into a new error.
  44. ///
  45. /// This allows library users to transform errors which happen during their handling of the request
  46. /// into more meaningful `GRPCStatus` errors before they are sent to the user.
  47. ///
  48. /// - note:
  49. /// Errors returned by this method are not passed to `observe` again.
  50. ///
  51. /// - note:
  52. /// This defaults to returning `nil`. In that case, if the original error conforms to `GRPCStatusTransformable`,
  53. /// that error's `asGRPCStatus()` result will be sent to the user. If that's not the case, either,
  54. /// `GRPCStatus.processingError` is returned.
  55. ///
  56. /// - Parameters:
  57. /// - error: The original error the status/response promise was failed with.
  58. /// - request: The headers of the request whose status/response promise was failed.
  59. func transformRequestHandlerError(_ error: Error, request: HTTPRequestHead) -> GRPCStatusAndMetadata?
  60. @available(*, deprecated, message: "Please use the new transformLibraryError that returns a GRPCStatusAndMetadata instead.")
  61. func transformRequestHandlerError(_ error: Error, request: HTTPRequestHead) -> GRPCStatus?
  62. }
  63. public extension ServerErrorDelegate {
  64. func observeLibraryError(_ error: Error) { }
  65. func transformLibraryError(_ error: Error) -> GRPCStatusAndMetadata? { return nil }
  66. func transformLibraryError(_ error: Error) -> GRPCStatus? { return nil }
  67. func observeRequestHandlerError(_ error: Error, request: HTTPRequestHead) { }
  68. func transformRequestHandlerError(_ error: Error, request: HTTPRequestHead) -> GRPCStatusAndMetadata? { return nil }
  69. func transformRequestHandlerError(_ error: Error, request: HTTPRequestHead) -> GRPCStatus? { return nil }
  70. }