PluginError.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import protocol SwiftProtobuf.Message
  2. enum PluginError: Error {
  3. case unknown(Error)
  4. case internalInconcictency(details: String?)
  5. case unsupportedMethodCall(method: String)
  6. case invalidMethodCall(method: String, details: String?)
  7. case messageDeserializationFailure(type: Message.Type, underlyingError: Error)
  8. case messageSerializationFailure(type: Message.Type, underlyingError: Error)
  9. case notInitialized
  10. case connectionLost
  11. var asFlutterError: FlutterError {
  12. switch self {
  13. case .unknown(let error as NSError):
  14. return makeFlutterError(code: "\(error.domain):\(error.code)", message: error.localizedDescription, details: error.userInfo)
  15. case .internalInconcictency(let details):
  16. let extra = details.map { " (\($0))" } ?? ""
  17. return makeFlutterError(message: "internal inconsistency" + extra)
  18. case .unsupportedMethodCall(let method):
  19. return makeFlutterError(message: "the method \"\(method)\" is not supported")
  20. case .invalidMethodCall(let method, let details):
  21. let extra = details.map { " (\($0))" } ?? ""
  22. return makeFlutterError(message: "invalid \"\(method)\" method call" + extra)
  23. case .messageDeserializationFailure(let type, let underlyingError):
  24. return makeFlutterError(message: "failed to deserialize a message of type \(type) (\(underlyingError))")
  25. case .messageSerializationFailure(let type, let underlyingError):
  26. return makeFlutterError(message: "failed to serialize a message of type \(type) (\(underlyingError))")
  27. case .notInitialized:
  28. return makeFlutterError(message: "not initialized")
  29. case .connectionLost:
  30. return makeFlutterError(message: "connection lost")
  31. }
  32. }
  33. private func makeFlutterError(code: String? = nil, message: String?, details: Any? = nil) -> FlutterError {
  34. return FlutterError(code: code ?? "\(self)", message: message, details: details)
  35. }
  36. }