ErrorService.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // ErrorService.swift
  3. // TUIRoomKit
  4. //
  5. // Created by CY zhao on 2024/6/13.
  6. //
  7. import Foundation
  8. import RTCRoomEngine
  9. struct RoomError: Error {
  10. let error: TUIError
  11. let message: String
  12. var actions: [Action] = []
  13. init(error: TUIError, message: String = "", showToast: Bool = true) {
  14. self.error = error
  15. self.message = message
  16. if showToast {
  17. actions.append(ViewActions.showToast(payload: ToastInfo(message: message)))
  18. }
  19. }
  20. }
  21. protocol LocalizedError {
  22. var description: String?{get}
  23. var isCommon: Bool{get}
  24. }
  25. extension TUIError: LocalizedError {
  26. var description: String? {
  27. switch self {
  28. case .roomIdNotExist:
  29. return .roomIdNotExist
  30. case .roomIdOccupied:
  31. return .roomIdOccupied
  32. case .roomUserFull:
  33. return .roomUserFull
  34. case .roomNameInvalid:
  35. return .roomNameInvalid
  36. case .roomIdInvalid:
  37. return .roomIdInvalid
  38. case .operationInvalidBeforeEnterRoom:
  39. return .operationInvalidBeforeEnterRoom
  40. case .operationNotSupportedInCurrentRoomType:
  41. return .operationNotSupportedInCurrentRoomType
  42. case .alreadyInOtherRoom:
  43. return .alreadyInOtherRoom
  44. default:
  45. return nil
  46. }
  47. }
  48. var isCommon: Bool {
  49. switch self {
  50. case .roomIdNotExist, .roomIdOccupied, .roomUserFull:
  51. return true
  52. default:
  53. return false
  54. }
  55. }
  56. }
  57. private extension String {
  58. static let roomIdNotExist = localized("The room does not exist, please confirm the room ID or create a room!")
  59. static let operationInvalidBeforeEnterRoom = localized("You need to enter the room to use this function.")
  60. static let operationNotSupportedInCurrentRoomType = localized("This operation is not supported in the current room type.")
  61. static let roomIdInvalid = localized("The room number is invalid. It must be printable ASCII characters and cannot exceed 48 bytes.")
  62. static let roomIdOccupied = localized("The room ID is occupied, please select another room ID.")
  63. static let roomNameInvalid = localized("The room name is invalid. It cannot exceed 30 bytes. If it contains Chinese characters, the character encoding must be UTF-8.")
  64. static let alreadyInOtherRoom = localized("You are already in another room and need to leave the room before joining a new room.")
  65. static let roomUserFull = localized("The room is full and you cannot enter the room temporarily.")
  66. }