Skip to content

Commit

Permalink
Communication Services - Chat: Exception mapping (#19993)
Browse files Browse the repository at this point in the history
* translate CommunicationErrorResponseException to HttpResponseException
* add exceptions to javadoc
  • Loading branch information
danielgerlag authored Mar 19, 2021
1 parent 60bca44 commit 9258dc2
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package com.azure.communication.chat;

import com.azure.communication.chat.implementation.converters.CreateChatThreadResultConverter;
import com.azure.communication.chat.implementation.models.CommunicationErrorResponseException;
import com.azure.core.exception.HttpResponseException;
import reactor.core.publisher.Mono;

import com.azure.communication.chat.models.ChatThreadItem;
Expand Down Expand Up @@ -57,6 +59,8 @@ public ChatThreadAsyncClient getChatThreadClient(String chatThreadId) {
* Creates a chat thread.
*
* @param options Options for creating a chat thread.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
Expand All @@ -81,6 +85,8 @@ public Mono<CreateChatThreadResult> createChatThread(CreateChatThreadOptions opt
* Creates a chat thread.
*
* @param options Options for creating a chat thread.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
Expand All @@ -98,14 +104,17 @@ public Mono<Response<CreateChatThreadResult>> createChatThreadWithResponse(Creat
*
* @param options Options for creating a chat thread.
* @param context The context to associate with this operation.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
Mono<Response<CreateChatThreadResult>> createChatThread(CreateChatThreadOptions options, Context context) {
context = context == null ? Context.NONE : context;
try {
return this.chatClient.createChatThreadWithResponseAsync(
CreateChatThreadOptionsConverter.convert(options), options.getIdempotencyToken(), context).map(
result -> new SimpleResponse<CreateChatThreadResult>(
CreateChatThreadOptionsConverter.convert(options), options.getIdempotencyToken(), context)
.onErrorMap(CommunicationErrorResponseException.class, e -> translateException(e))
.map(result -> new SimpleResponse<CreateChatThreadResult>(
result, CreateChatThreadResultConverter.convert(result.getValue())));
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand All @@ -115,6 +124,8 @@ Mono<Response<CreateChatThreadResult>> createChatThread(CreateChatThreadOptions
/**
* Gets the list of chat threads of a user.
*
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the paged list of chat threads of a user.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
Expand All @@ -123,9 +134,11 @@ public PagedFlux<ChatThreadItem> listChatThreads() {
try {
return new PagedFlux<>(
() -> withContext(context -> this.chatClient.listChatThreadsSinglePageAsync(
listThreadsOptions.getMaxPageSize(), listThreadsOptions.getStartTime(), context)),
listThreadsOptions.getMaxPageSize(), listThreadsOptions.getStartTime(), context)
.onErrorMap(CommunicationErrorResponseException.class, e -> translateException(e))),
nextLink -> withContext(context -> this.chatClient.listChatThreadsNextSinglePageAsync(
nextLink, context)));
nextLink, context)
.onErrorMap(CommunicationErrorResponseException.class, e -> translateException(e))));
} catch (RuntimeException ex) {
return new PagedFlux<>(() -> monoError(logger, ex));
}
Expand All @@ -135,6 +148,8 @@ public PagedFlux<ChatThreadItem> listChatThreads() {
* Gets the list of chat threads of a user.
*
* @param listThreadsOptions The request options.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the paged list of chat threads of a user.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
Expand All @@ -144,9 +159,11 @@ public PagedFlux<ChatThreadItem> listChatThreads(ListChatThreadsOptions listThre
try {
return new PagedFlux<>(
() -> withContext(context -> this.chatClient.listChatThreadsSinglePageAsync(
serviceListThreadsOptions.getMaxPageSize(), serviceListThreadsOptions.getStartTime(), context)),
serviceListThreadsOptions.getMaxPageSize(), serviceListThreadsOptions.getStartTime(), context)
.onErrorMap(CommunicationErrorResponseException.class, e -> translateException(e))),
nextLink -> withContext(context -> this.chatClient.listChatThreadsNextSinglePageAsync(
nextLink, context)));
nextLink, context)
.onErrorMap(CommunicationErrorResponseException.class, e -> translateException(e))));
} catch (RuntimeException ex) {
return new PagedFlux<>(() -> monoError(logger, ex));
}
Expand Down Expand Up @@ -174,6 +191,8 @@ PagedFlux<ChatThreadItem> listChatThreads(ListChatThreadsOptions listThreadsOpti
* Deletes a chat thread.
*
* @param chatThreadId Chat thread id to delete.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
Expand All @@ -193,6 +212,8 @@ public Mono<Void> deleteChatThread(String chatThreadId) {
* Deletes a chat thread.
*
* @param chatThreadId Chat thread id to delete.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
Expand All @@ -210,15 +231,22 @@ public Mono<Response<Void>> deleteChatThreadWithResponse(String chatThreadId) {
*
* @param chatThreadId Chat thread id to delete.
* @param context The context to associate with this operation.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the completion.
*/
Mono<Response<Void>> deleteChatThread(String chatThreadId, Context context) {
context = context == null ? Context.NONE : context;
try {
return this.chatClient.deleteChatThreadWithResponseAsync(chatThreadId, context);
return this.chatClient.deleteChatThreadWithResponseAsync(chatThreadId, context)
.onErrorMap(CommunicationErrorResponseException.class, e -> translateException(e));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}

private RuntimeException translateException(CommunicationErrorResponseException exception) {
return new HttpResponseException(exception.getMessage(), exception.getResponse(), exception.getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
Expand Down Expand Up @@ -52,6 +53,8 @@ public ChatThreadClient getChatThreadClient(String chatThreadId) {
* Creates a chat thread.
*
* @param options Options for creating a chat thread.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
Expand All @@ -64,6 +67,8 @@ public CreateChatThreadResult createChatThread(CreateChatThreadOptions options)
*
* @param options Options for creating a chat thread.
* @param context The context to associate with this operation.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the response.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
Expand All @@ -78,6 +83,8 @@ public Response<CreateChatThreadResult> createChatThreadWithResponse(CreateChatT
* Deletes a chat thread.
*
* @param chatThreadId Chat thread id to delete.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public void deleteChatThread(String chatThreadId) {
Expand All @@ -89,6 +96,8 @@ public void deleteChatThread(String chatThreadId) {
*
* @param chatThreadId Chat thread id to delete.
* @param context The context to associate with this operation.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
Expand All @@ -100,6 +109,8 @@ public Response<Void> deleteChatThreadWithResponse(String chatThreadId, Context
/**
* Gets the list of chat threads of a user.
*
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the paged list of chat threads of a user.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
Expand All @@ -113,6 +124,8 @@ public PagedIterable<ChatThreadItem> listChatThreads() {
*
* @param listThreadsOptions The request options.
* @param context The context to associate with this operation.
* @throws HttpResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return the paged list of chat threads of a user.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
Expand Down
Loading

0 comments on commit 9258dc2

Please sign in to comment.