Skip to content

Commit ecb64c1

Browse files
authored
38 normalize exceptions (#39)
* Change inheritances of exceptions to base one. * Modify HTTP method names, document throws. * Fix Jspecify annotations * Improve documentation * Change HTTP call methods on services * Improve error handling doc on interfaces --------- Co-authored-by: J.C <73438877+1hiking@users.noreply.github.com>
1 parent 443132e commit ecb64c1

12 files changed

Lines changed: 183 additions & 94 deletions

File tree

‎src/main/java/io/github/hikingc/matrixsdk/api/Event.java‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
/// All operations in this interface are blocking. Implementations must ensure thread safety and
2121
/// avoid synchronization blocks that cause carrier thread pinning during network I/O.
2222
///
23+
/// Unless otherwise noted, every method in this interface throws [MatrixIOException] if the request
24+
/// or response payload cannot be processed, and [MatrixNetworkException] if the server's response
25+
/// status is not successful.
26+
///
2327
/// @see <a href="https://spec.matrix.org/v1.19/client-server-api/#events">Matrix Client-Server API
2428
/// Specification for Events</a>
2529
public interface Event {
@@ -29,12 +33,16 @@ public interface Event {
2933
/// @param roomId the room ID where the event is.
3034
/// @param eventId the event ID to retrieve.
3135
/// @return the full event.
36+
/// @throws MatrixIOException when the payload cannot be processed.
37+
/// @throws MatrixNetworkException when the response status is not successful.
3238
ClientEvent getEvent(RoomID roomId, String eventId);
3339

3440
/// Returns currently-joined members
3541
///
3642
/// @param roomId the room ID to fetch data from.
3743
/// @return a list of room members.
44+
/// @throws MatrixIOException when the payload cannot be processed.
45+
/// @throws MatrixNetworkException when the response status is not successful.
3846
RoomMembers getJoinedMembers(RoomID roomId);
3947

4048
/// Returns a filterable list of members and their current membership state in a room.
@@ -47,14 +55,18 @@ public interface Event {
4755
/// @param notMembership the kind of membership to exclude from the results. Defaults to no
4856
/// filtering if unspecified.
4957
/// @return a list of [ClientEvent]s with the membership information of room members.
58+
/// @throws MatrixIOException when the payload cannot be processed.
59+
/// @throws MatrixNetworkException when the response status is not successful.
5060
List<RoomMemberEvent> getMembers(
5161
RoomID roomId, String at, Membership membership, Membership notMembership);
5262

5363
/// Get the state events for the current state of a room.
5464
///
5565
/// @param roomId the room ID to fetch data from.
56-
/// @return the current state of the room
57-
List<ClientEvent<?>> getStateEvents(RoomID roomId);
66+
/// @return the current state of the room.
67+
/// @throws MatrixIOException when the payload cannot be processed.
68+
/// @throws MatrixNetworkException when the response status is not successful.
69+
List<StateEvent<?>> getStateEvents(RoomID roomId);
5870

5971
/// Looks up the contents of a state event in a room. If the user is joined to the room then the
6072
/// state is taken from the current state of the room. If the user has left the room then the
@@ -64,6 +76,8 @@ List<RoomMemberEvent> getMembers(
6476
/// @param eventType the type of state to look up.
6577
/// @param stateKey the room to look up the state in.
6678
/// @return the content of the event, including all additional metadata fields.
79+
/// @throws MatrixIOException when the payload cannot be processed.
80+
/// @throws MatrixNetworkException when the response status is not successful.
6781
@SuppressWarnings("java:S1452")
6882
// Caller doesn't know content type ahead of time; polymorphic dispatch via @JsonTypeInfo resolves
6983
// it
@@ -78,6 +92,7 @@ List<RoomMemberEvent> getMembers(
7892
/// @param dir the [ChronologicalDirection] in which to search
7993
/// @return [Messages] with available data.
8094
/// @throws MatrixIOException when the payload cannot be processed.
95+
/// @throws MatrixNetworkException when the response status is not successful.
8196
/// @throws NullPointerException when the roomId is null.
8297
Messages getMessages(RoomID roomId, ChronologicalDirection dir, QueryParametersMessages params);
8398

@@ -88,6 +103,8 @@ List<RoomMemberEvent> getMembers(
88103
/// @param dir the [ChronologicalDirection] in which to search
89104
/// @param timestamp the timestamp to search from, as given in milliseconds since the Unix epoch.
90105
/// @return [EventMetadata] if an event was found.
106+
/// @throws MatrixIOException when the payload cannot be processed.
107+
/// @throws MatrixNetworkException when the response status is not successful.
91108
EventMetadata getEventClosestToTimestamp(
92109
RoomID roomId, ChronologicalDirection dir, int timestamp);
93110

@@ -96,6 +113,8 @@ EventMetadata getEventClosestToTimestamp(
96113
///
97114
/// @param roomId the room ID to fetch data from.
98115
/// @return [RoomInfo] with current state of the room.
116+
/// @throws MatrixIOException when the payload cannot be processed.
117+
/// @throws MatrixNetworkException when the response status is not successful.
99118
RoomInfo getInitialSync(RoomID roomId);
100119

101120
/// Sends a state event.
@@ -106,15 +125,19 @@ EventMetadata getEventClosestToTimestamp(
106125
/// @return a [String] representing a unique identifier of the event.
107126
/// @throws MatrixIOException when the payload cannot be processed.
108127
/// @throws MatrixNetworkException when the response status is not successful.
128+
/// @throws MatrixIOException when the payload cannot be processed.
129+
/// @throws MatrixNetworkException when the response status is not successful.
109130
String sendStateEvent(RoomID roomId, String stateKey, StateEventContent content);
110131

111132
/// Sends a message event.
112133
///
113-
/// @param roomId the room ID where to send the event.
114-
/// @param txnId for this event. Clients should generate an ID unique across requests with the
134+
/// @param roomId the room ID where to send the event.
135+
/// @param txnId for this event. Clients should generate an ID unique across requests with the
115136
/// same access token; it will be used by the server to ensure idempotency of requests.
116137
/// @param content of any type of message event.
117138
/// @return a [String] representing a unique identifier of the event.
139+
/// @throws MatrixIOException when the payload cannot be processed.
140+
/// @throws MatrixNetworkException when the response status is not successful.
118141
String sendMessageEvent(RoomID roomId, String txnId, MessageEventContent content);
119142

120143
/// Strips all information out of an event which isn’t critical to the integrity of the

‎src/main/java/io/github/hikingc/matrixsdk/api/Filter.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import io.github.hikingc.matrixsdk.api.filters.FilterDefinition;
44
import io.github.hikingc.matrixsdk.api.identifiers.UserID;
5+
import io.github.hikingc.matrixsdk.exceptions.MatrixIOException;
6+
import io.github.hikingc.matrixsdk.exceptions.MatrixNetworkException;
57

68
/// Core interface for executing protocol operations for filtering.
79
///
810
/// All operations in this interface are blocking. Implementations must ensure thread safety and
911
/// avoid synchronization blocks that cause carrier thread pinning during network I/O.
1012
///
13+
/// Unless otherwise noted, every method in this interface throws [MatrixIOException] if the request
14+
/// or response payload cannot be processed, and [MatrixNetworkException] if the server's response
15+
/// status is not successful.
16+
///
1117
/// @see <a href="https://spec.matrix.org/v1.19/client-server-api/#filtering>Matrix Client-Server
1218
/// API Specification for Filters</a>
1319
public interface Filter {
@@ -18,12 +24,16 @@ public interface Filter {
1824
/// @param userId the [UserID] of whoever is uploading the server.
1925
/// @param filter the definition of the filter.
2026
/// @return an ID of the filter definition, usable in supported endpoints.
27+
/// @throws MatrixIOException when the payload cannot be processed.
28+
/// @throws MatrixNetworkException when the response status is not successful.
2129
String publishFilter(UserID userId, FilterDefinition filter);
2230

2331
/// Retrieve a [FilterDefinition] from the homeserver.
2432
///
2533
/// @param userId the [UserID] to download a filter for.
2634
/// @param filterId the filter ID to download.
2735
/// @return a [FilterDefinition] with all uploaded data.
36+
/// @throws MatrixIOException when the payload cannot be processed.
37+
/// @throws MatrixNetworkException when the response status is not successful.
2838
FilterDefinition getFilter(UserID userId, String filterId);
2939
}

‎src/main/java/io/github/hikingc/matrixsdk/api/Room.java‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
/// All operations in this interface are blocking. Implementations must ensure thread safety and
1818
/// avoid synchronization blocks that cause carrier thread pinning during network I/O.
1919
///
20+
/// Unless otherwise noted, every method in this interface throws [MatrixIOException] if the request
21+
/// or response payload cannot be processed, and [MatrixNetworkException] if the server's response
22+
/// status is not successful.
23+
///
2024
/// @see <a href="https://spec.matrix.org/v1.19/client-server-api/#rooms">Matrix Client-Server API
2125
/// Specification for Rooms</a>
2226
public interface Room {
2327
/// Creates a room based on the received [InitialRoomConfiguration].
2428
///
2529
/// @param configuration of the room.
2630
/// @return the created room’s ID.
27-
/// @throws MatrixIOException when the payload cannot be processed
28-
/// @throws MatrixNetworkException when the response status is not successful
31+
/// @throws MatrixIOException when the payload cannot be processed.
32+
/// @throws MatrixNetworkException when the response status is not successful.
2933
String create(InitialRoomConfiguration configuration);
3034

3135
/// Requests the server to resolve a room alias if not possible, the server will use the
@@ -34,18 +38,24 @@ public interface Room {
3438
/// @param roomAlias the room alias.
3539
/// @return a [ResolvedAlias] containing the room ids for the requested alias and which servers
3640
/// are aware of it.
41+
/// @throws MatrixIOException when the payload cannot be processed.
42+
/// @throws MatrixNetworkException when the response status is not successful.
3743
ResolvedAlias resolveAlias(RoomAlias roomAlias);
3844

3945
/// Sets a room alias to a room.
4046
///
4147
/// @param roomAlias a [RoomAlias].
4248
/// @param roomId the [RoomID] to receive the alias.
49+
/// @throws MatrixIOException when the payload cannot be processed.
50+
/// @throws MatrixNetworkException when the response status is not successful.
4351
void setAlias(RoomAlias roomAlias, RoomID roomId);
4452

4553
/// Requests the server to remove a mapping of a room alias to a room id. On success, servers
4654
/// might modify `m.room.canonical_alias`
4755
///
4856
/// @param roomAlias the [RoomAlias] to remove.
57+
/// @throws MatrixIOException when the payload cannot be processed.
58+
/// @throws MatrixNetworkException when the response status is not successful.
4959
void deleteAlias(RoomAlias roomAlias);
5060

5161
/// Requests a list of aliases maintained by the local server for the given room, requires to be
@@ -56,12 +66,16 @@ public interface Room {
5666
///
5767
/// @param roomId the [RoomID] to find local aliases of.
5868
/// @return a [List] of Room aliases.
69+
/// @throws MatrixIOException when the payload cannot be processed.
70+
/// @throws MatrixNetworkException when the response status is not successful.
5971
List<String> getAliasesOfARoom(RoomID roomId);
6072

6173
/// Requests the server to retrieve a list of the user's current rooms (in simple terms whoever
6274
/// calls this method).
6375
///
6476
/// @return a [List] of the rooms.
77+
/// @throws MatrixIOException when the payload cannot be processed.
78+
/// @throws MatrixNetworkException when the response status is not successful.
6579
List<String> getJoinedRooms();
6680

6781
/// Send an invitation to a user to participate in a room, this endpoint requires the caller to be
@@ -80,7 +94,10 @@ public interface Room {
8094
/// @param request a [JoinRoomRequest] where additional information can be passed.
8195
/// @param via the servers to attempt to join the room through. One of the servers must be
8296
/// participating in the room.
83-
/// @return the room ID
97+
/// @return the room ID.
98+
/// @throws MatrixIOException when the payload cannot be processed.
99+
/// @throws MatrixNetworkException when the response status is not successful.
100+
/// @throws IllegalArgumentException when using an incorrect [Validator].
84101
String joinByRoomIdOrAliasIfAllowed(
85102
Validator roomIdOrAlias, JoinRoomRequest request, List<String> via);
86103

@@ -90,7 +107,9 @@ String joinByRoomIdOrAliasIfAllowed(
90107
/// @param request a [JoinRoomRequest] where additional information can be passed.
91108
/// @param via the servers to attempt to join the room through. One of the servers must be
92109
/// participating in the room.
93-
/// @return the room ID
110+
/// @return the room ID.
111+
/// @throws MatrixIOException when the payload cannot be processed.
112+
/// @throws MatrixNetworkException when the response status is not successful.
94113
String joinByRoomIdIfAllowed(RoomID roomId, JoinRoomRequest request, List<String> via);
95114

96115
/// Knock on a room to ask for permission to join. Acceptance of this request happens out of band.
@@ -100,6 +119,8 @@ String joinByRoomIdOrAliasIfAllowed(
100119
/// @param via the servers to attempt to join the room through. One of the servers must be
101120
/// participating in the room.
102121
/// @return the room ID of the knocked room.
122+
/// @throws MatrixIOException when the payload cannot be processed.
123+
/// @throws MatrixNetworkException when the response status is not successful.
103124
String knockOn(Validator roomIdOrAlias, String reason, List<String> via);
104125

105126
/// Sends a request to leave the room, upon success, you will forget all messages from this room.
@@ -171,8 +192,7 @@ String joinByRoomIdOrAliasIfAllowed(
171192
/// @param since a pagination token from a previous request, allowing you to get the next or
172193
/// previous batch of rooms. The direction of pagination is specified by which token is
173194
/// supplied.
174-
/// @return a [PublicRoomDirectory] containing [io.github.hikingc.matrixsdk.api.rooms.models.PublishedRoomsChunk] records of the published
175-
/// rooms on the server.
195+
/// @return a [PublicRoomDirectory] containing records of the published rooms on the server.
176196
/// @throws MatrixIOException when the payload cannot be processed.
177197
/// @throws NullPointerException when the roomId is null.
178198
/// @see #getPublishedRoomDirectory(PublicRoomRequest)
@@ -182,18 +202,21 @@ String joinByRoomIdOrAliasIfAllowed(
182202
/// Lists a server’s published room directory.
183203
///
184204
/// @param request a [PublicRoomRequest] with additional filters for the request.
185-
/// @return a [PublicRoomDirectory] containing [io.github.hikingc.matrixsdk.api.rooms.models.PublishedRoomsChunk] records of the published
186-
/// rooms on the server.
205+
/// @return a [PublicRoomDirectory] containing records of the published rooms on the server.
206+
/// @throws MatrixIOException when the payload cannot be processed.
207+
/// @throws MatrixNetworkException when the response status is not successful.
187208
PublicRoomDirectory getPublishedRoomDirectory(PublicRoomRequest request);
188209

189210
/// Retrieves a summary for a room. The response data might yield outdated, partial or even with
190211
/// no data.
191212
///
192-
/// @param roomIdOrAlias a [RoomID] or [RoomAlias] of the room to target
213+
/// @param roomIdOrAlias a [RoomID] or [RoomAlias] of the room to target.
193214
/// @param via the servers to attempt to request the summary from when the local server cannot
194-
/// generate it
215+
/// generate it.
195216
/// @return a [RoomSummary] containing all the information about the room.
196217
/// @throws MatrixIOException when the payload cannot be processed.
197218
/// @throws NullPointerException when the roomId is null.
219+
/// @throws MatrixIOException when the payload cannot be processed.
220+
/// @throws MatrixNetworkException when the response status is not successful.
198221
RoomSummary getRoomSummary(Validator roomIdOrAlias, List<String> via);
199222
}

‎src/main/java/io/github/hikingc/matrixsdk/api/UserData.java‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
import io.github.hikingc.matrixsdk.api.identifiers.UserID;
44
import io.github.hikingc.matrixsdk.api.userdata.UserProfile;
55
import io.github.hikingc.matrixsdk.api.userdata.UsersFound;
6+
import io.github.hikingc.matrixsdk.exceptions.MatrixIOException;
7+
import io.github.hikingc.matrixsdk.exceptions.MatrixNetworkException;
68

79
/// Core interface for executing protocol operations against User data.
810
///
911
/// All operations in this interface are blocking. Implementations must ensure thread safety and
1012
/// avoid synchronization blocks that cause carrier thread pinning during network I/O.
1113
///
14+
/// Unless otherwise noted, every method in this interface throws [MatrixIOException] if the request
15+
/// or response payload cannot be processed, and [MatrixNetworkException] if the server's response
16+
/// status is not successful.
17+
///
1218
/// @see <a href="https://spec.matrix.org/v1.18/client-server-api/#user-data">Matrix Client-Server
1319
/// API Specification for User Data</a>
1420
public interface UserData {
@@ -22,32 +28,42 @@ public interface UserData {
2228
/// @param limit the maximum number of results.
2329
/// @param searchTerm the term to search for.
2430
/// @return all the [UsersFound] by the server.
31+
/// @throws MatrixIOException when the payload cannot be processed.
32+
/// @throws MatrixNetworkException when the response status is not successful.
2533
UsersFound searchUsersByTerm(Integer limit, String searchTerm);
2634

2735
/// Get the profile of a user
2836
///
29-
/// @param userId the [UserID] to profile.
30-
/// @return the corresponding [UserProfile].
37+
/// @param userId the [UserID] to target.
38+
/// @return its [UserProfile].
39+
/// @throws MatrixIOException when the payload cannot be processed.
40+
/// @throws MatrixNetworkException when the response status is not successful.
3141
UserProfile getUserProfile(UserID userId);
3242

3343
/// Get the value of a profile field for a user
3444
///
35-
/// @param userId the [UserID] to profile.
36-
/// @param keyName a property field
37-
/// @return the value of the key property.
45+
/// @param userId the [UserID] to target.
46+
/// @param keyName the key name.
47+
/// @return the corresponding value of the pair.
48+
/// @throws MatrixIOException when the payload cannot be processed.
49+
/// @throws MatrixNetworkException when the response status is not successful.
3850
String getUserProfileByProperty(
3951
UserID userId, String keyName); // only 1 property allowed so no Map
4052

4153
/// Set or update a profile field for a user.
4254
///
43-
/// @param userId the [UserID] that'll receive the K-V.
44-
/// @param keyName the key to insert in the profile.
45-
/// @param valueName the value for the key.
55+
/// @param userId the [UserID] to target.
56+
/// @param keyName the key name.
57+
/// @param valueName the value name.
58+
/// @throws MatrixIOException when the payload cannot be processed.
59+
/// @throws MatrixNetworkException when the response status is not successful.
4660
void setUserProfileProperty(UserID userId, String keyName, String valueName);
4761

4862
/// Remove a specific field from a user’s profile.
4963
///
50-
/// @param userId the [UserID] that'll have his K-V deleted.
51-
/// @param keyName the key to be deleted.
64+
/// @param userId the [UserID] that'll have a key-value pair removed from its profile.
65+
/// @param keyName the key name.
66+
/// @throws MatrixIOException when the payload cannot be processed.
67+
/// @throws MatrixNetworkException when the response status is not successful.
5268
void deleteUserProfileProperty(UserID userId, String keyName);
5369
}

‎src/main/java/io/github/hikingc/matrixsdk/exceptions/MatrixIOException.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.github.hikingc.matrixsdk.exceptions;
22

33
/// Thrown to indicate that the code has attempted to process an I/O event to which it has failed.
4-
public class MatrixIOException extends RuntimeException {
4+
public class MatrixIOException extends MatrixException {
55
/// Constructs a [MatrixIOException] with a message.
66
///
77
/// @param message The detail message. The detail message is saved for later retrieval by the

‎src/main/java/io/github/hikingc/matrixsdk/exceptions/MatrixNetworkException.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.github.hikingc.matrixsdk.exceptions;
22

33
/// Thrown to indicate that the code has not received a successful HTTP status code.
4-
public class MatrixNetworkException extends RuntimeException {
4+
public class MatrixNetworkException extends MatrixException {
55
/// Constructs a [MatrixNetworkException] with a message.
66
///
77
/// @param message The detail message. The detail message is saved for later retrieval by the

0 commit comments

Comments
 (0)