Skip to content

Commit 7431319

Browse files
added error handling for the composer commands
1 parent d2b5ea5 commit 7431319

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,9 @@ public class MessageComposerViewModel: ObservableObject {
372372

373373
if let composerCommand = composerCommand {
374374
commandsHandler.showSuggestions(for: composerCommand)
375-
.sink { [weak self] suggestionInfo in
375+
.sink { _ in
376+
log.debug("Finished showing suggestions")
377+
} receiveValue: { [weak self] suggestionInfo in
376378
withAnimation {
377379
self?.suggestions[suggestionInfo.key] = suggestionInfo.value
378380
}

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/CommandsHandler.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protocol CommandHandler {
1717

1818
func showSuggestions(
1919
for command: ComposerCommand
20-
) -> Future<SuggestionInfo, Never>
20+
) -> Future<SuggestionInfo, Error>
2121

2222
func handleCommand(
2323
for text: Binding<String>,
@@ -61,15 +61,14 @@ class CommandsHandler: CommandHandler {
6161

6262
func showSuggestions(
6363
for command: ComposerCommand
64-
) -> Future<SuggestionInfo, Never> {
64+
) -> Future<SuggestionInfo, Error> {
6565
for handler in commands {
6666
if handler.id == command.id {
6767
return handler.showSuggestions(for: command)
6868
}
6969
}
7070

71-
// TODO: gracefully
72-
fatalError("misconfiguration of commands")
71+
return StreamChatError.wrongConfig.asFailedPromise()
7372
}
7473

7574
func handleCommand(

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/MentionsSuggester.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public struct MentionsSuggester: CommandHandler {
5959

6060
func showSuggestions(
6161
for command: ComposerCommand
62-
) -> Future<SuggestionInfo, Never> {
62+
) -> Future<SuggestionInfo, Error> {
6363
showMentionSuggestions(
6464
for: command.typingSuggestion.text,
6565
mentionRange: command.typingSuggestion.locationRange
@@ -71,10 +71,10 @@ public struct MentionsSuggester: CommandHandler {
7171
private func showMentionSuggestions(
7272
for typingMention: String,
7373
mentionRange: NSRange
74-
) -> Future<SuggestionInfo, Never> {
74+
) -> Future<SuggestionInfo, Error> {
7575
guard let channel = channelController.channel,
7676
let currentUserId = channelController.client.currentUserId else {
77-
return resolve(with: SuggestionInfo(key: "", value: []))
77+
return StreamChatError.missingData.asFailedPromise()
7878
}
7979

8080
if mentionAllAppUsers {
@@ -136,16 +136,20 @@ public struct MentionsSuggester: CommandHandler {
136136
}
137137
}
138138

139-
private func resolve(with users: SuggestionInfo) -> Future<SuggestionInfo, Never> {
139+
private func resolve(with users: SuggestionInfo) -> Future<SuggestionInfo, Error> {
140140
Future { promise in
141141
promise(.success(users))
142142
}
143143
}
144-
145-
private func searchAllUsers(for typingMention: String) -> Future<SuggestionInfo, Never> {
144+
145+
private func searchAllUsers(for typingMention: String) -> Future<SuggestionInfo, Error> {
146146
Future { promise in
147147
let query = queryForMentionSuggestionsSearch(typingMention: typingMention)
148-
userSearchController.search(query: query) { _ in
148+
userSearchController.search(query: query) { error in
149+
if let error = error {
150+
promise(.failure(error))
151+
return
152+
}
149153
let users = Array(userSearchController.users)
150154
let suggestionInfo = SuggestionInfo(key: id, value: users)
151155
promise(.success(suggestionInfo))
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Copyright © 2021 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import Combine
6+
import Foundation
7+
8+
public struct StreamChatError: Error {
9+
10+
/// The specific error code.
11+
let errorCode: StreamChatErrorCode
12+
13+
/// The additional error message description.
14+
let description: String?
15+
16+
/// The additional information dictionary.
17+
let additionalInfo: [String: Any]?
18+
19+
static let unknown = StreamChatError(
20+
errorCode: StreamChatErrorCode.unknown,
21+
description: nil,
22+
additionalInfo: nil
23+
)
24+
25+
static let missingData = StreamChatError(
26+
errorCode: StreamChatErrorCode.missingData,
27+
description: nil,
28+
additionalInfo: nil
29+
)
30+
31+
static let wrongConfig = StreamChatError(
32+
errorCode: StreamChatErrorCode.wrongConfig,
33+
description: nil,
34+
additionalInfo: nil
35+
)
36+
}
37+
38+
extension StreamChatError: Equatable {
39+
40+
public static func == (lhs: StreamChatError, rhs: StreamChatError) -> Bool {
41+
lhs.errorCode == rhs.errorCode
42+
}
43+
}
44+
45+
extension StreamChatError {
46+
47+
func asFailedPromise<T>() -> Future<T, Error> {
48+
Future { promise in
49+
promise(.failure(self))
50+
}
51+
}
52+
}
53+
54+
/// Error codes for errors happening in the app.
55+
public enum StreamChatErrorCode: Int {
56+
case unknown = 101_000
57+
case missingData = 101_001
58+
case wrongConfig = 101_002
59+
}

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
841B64C427744DB60016FF3B /* ComposerModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841B64C327744DB60016FF3B /* ComposerModels.swift */; };
1111
841B64C82774BA770016FF3B /* CommandsHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841B64C72774BA770016FF3B /* CommandsHandler.swift */; };
12+
841B64CA2775BBC10016FF3B /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841B64C92775BBC10016FF3B /* Errors.swift */; };
1213
842383E02767394200888CFC /* ChatChannelDataSource_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842383DF2767394200888CFC /* ChatChannelDataSource_Tests.swift */; };
1314
842383E427678A4D00888CFC /* QuotedMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842383E327678A4D00888CFC /* QuotedMessageView.swift */; };
1415
842F0BB8276B3518002C400C /* QuotedMessageView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842F0BB7276B3518002C400C /* QuotedMessageView_Tests.swift */; };
@@ -295,6 +296,7 @@
295296
4A65451E274BA170003C5FA8 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
296297
841B64C327744DB60016FF3B /* ComposerModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposerModels.swift; sourceTree = "<group>"; };
297298
841B64C72774BA770016FF3B /* CommandsHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandsHandler.swift; sourceTree = "<group>"; };
299+
841B64C92775BBC10016FF3B /* Errors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Errors.swift; sourceTree = "<group>"; };
298300
842383DF2767394200888CFC /* ChatChannelDataSource_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatChannelDataSource_Tests.swift; sourceTree = "<group>"; };
299301
842383E327678A4D00888CFC /* QuotedMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuotedMessageView.swift; sourceTree = "<group>"; };
300302
842E979C275E0AD000A52E7B /* StreamChatSwiftUI.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = StreamChatSwiftUI.xctestplan; sourceTree = "<group>"; };
@@ -834,6 +836,7 @@
834836
8465FD372746A95600AF091E /* ImageLoading.swift */,
835837
8465FD4A2746A95600AF091E /* BundleExtensions.swift */,
836838
8465FD3B2746A95600AF091E /* StringExtensions.swift */,
839+
841B64C92775BBC10016FF3B /* Errors.swift */,
837840
8465FD382746A95600AF091E /* Common */,
838841
);
839842
path = Utils;
@@ -1404,6 +1407,7 @@
14041407
8465FD992746A95700AF091E /* ReactionsBubbleView.swift in Sources */,
14051408
8465FDB02746A95700AF091E /* DateUtils.swift in Sources */,
14061409
84DEC8E12760D24100172876 /* MessageRepliesView.swift in Sources */,
1410+
841B64CA2775BBC10016FF3B /* Errors.swift in Sources */,
14071411
8465FD7A2746A95700AF091E /* VideoPlayerView.swift in Sources */,
14081412
8465FD8B2746A95700AF091E /* FilePickerView.swift in Sources */,
14091413
8465FDA92746A95700AF091E /* AutoLayoutHelpers.swift in Sources */,

0 commit comments

Comments
 (0)