ServerErrorDelegate.swift 3.2 KB

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