-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proper handling of errors returned by telegram API #1446
Open
dkBrazz
wants to merge
27
commits into
rubenlagus:dev
Choose a base branch
from
dkBrazz:api-error-handling
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
7a245fd
Jetty HttpClient implementation of TelegramClient
valkuc 1eb2d95
Merge branch 'dev' into master
rubenlagus 2c35fe4
Proper handling of errors returned by telegram API
dkurochkin-arlo afca116
Wrap response.body().string() access
dkurochkin-arlo a6acbdd
Api Version 8.0
rubenlagus 7678206
Fix argument passing in DefaultAbilities.java
panic08 66a8b55
Merge branch 'dev' into master
rubenlagus 1125cf0
Unify the use of lombok @NonNull instead of Objects.requireNonNull (#…
panic08 ff4537a
Unify the use of lombok @Slf4j instead of e.printStackTrace() (#1443)
panic08 50615c1
Make downloadFileWithId method private in final class (#1441)
panic08 cc6bdbd
Merge branch 'master' into api-error-handling
rubenlagus c3de6f3
Create <java.version> parameter for pom.xml in all modules (#1434)
panic08 2a4557f
Merge branch 'dev' into api-error-handling
rubenlagus b875d1e
Merge branch 'dev' into master
rubenlagus 536c7da
Merge branch 'valkuc-master' into dev
rubenlagus 8208c67
#1425
rubenlagus 5917ec0
Merge branch 'dev' into api-error-handling
rubenlagus 9b9d213
Fix typo
rubenlagus 38b64d8
Merge branch 'dev' into api-error-handling
rubenlagus 875c009
Consistency in the common validation messages (#1459)
aNNiMON 81c580d
Merge branch 'dev' into api-error-handling
rubenlagus 389fa99
Bugfix/fix wrong types (#1460)
khamidjon-khamidov 03888bc
Merge branch 'dev' into api-error-handling
rubenlagus 831dfb1
Undo custom annotation
rubenlagus 942c38c
Merge branch 'dev' into api-error-handling
rubenlagus fc6a29f
Api Version 8.0
rubenlagus 510be19
Merge branch 'dev' into api-error-handling
dkBrazz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...t/src/main/java/org/telegram/telegrambots/client/okhttp/AbstractOkHttpFutureCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.telegram.telegrambots.client.okhttp; | ||
|
||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Slf4j | ||
abstract class AbstractOkHttpFutureCallback<T> extends CompletableFuture<T> implements Callback { | ||
@Override | ||
public void onFailure(@NonNull Call call, @NonNull IOException exception) { | ||
completeExceptionally(exception); | ||
} | ||
|
||
@Override | ||
public void onResponse(@NonNull Call call, @NonNull Response response) { | ||
if (!response.isSuccessful()) { | ||
var messageBuilder = new StringBuilder(); | ||
messageBuilder.append("code: ").append(response.code()); | ||
messageBuilder.append(", message: ").append(response.message()); | ||
|
||
if(response.body() != null) { | ||
try { | ||
messageBuilder.append(", response body: ").append(response.body().string()); | ||
} catch (IOException e) { | ||
log.error("Error reading response body", e); | ||
} | ||
} | ||
|
||
completeExceptionally(new TelegramApiException("Telegram API returned error: " + messageBuilder)); | ||
return; | ||
} | ||
|
||
try(ResponseBody body = response.body()) { | ||
if (body == null) { | ||
completeExceptionally(new TelegramApiException("Telegram api returned empty response")); | ||
} else { | ||
complete(getResponseValue(body)); | ||
} | ||
} catch (Exception e) { | ||
completeExceptionally(e); | ||
} | ||
} | ||
|
||
protected abstract T getResponseValue(ResponseBody body) throws IOException, TelegramApiRequestException; | ||
} |
29 changes: 4 additions & 25 deletions
29
...ts-client/src/main/java/org/telegram/telegrambots/client/okhttp/OkHttpFutureCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,21 @@ | ||
package org.telegram.telegrambots.client.okhttp; | ||
|
||
import lombok.NonNull; | ||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import org.telegram.telegrambots.meta.api.methods.botapimethods.PartialBotApiMethod; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
class OkHttpFutureCallback<T extends Serializable, Method extends PartialBotApiMethod<T>> extends CompletableFuture<T> implements Callback { | ||
class OkHttpFutureCallback<T extends Serializable, Method extends PartialBotApiMethod<T>> extends AbstractOkHttpFutureCallback<T> { | ||
private final Method method; | ||
|
||
OkHttpFutureCallback(Method method) { | ||
this.method = method; | ||
} | ||
|
||
@Override | ||
public void onFailure(@NonNull Call call, @NonNull IOException exception) { | ||
completeExceptionally(exception); | ||
protected T getResponseValue(ResponseBody body) throws IOException, TelegramApiRequestException { | ||
return method.deserializeResponse(body.string()); | ||
} | ||
|
||
@Override | ||
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { | ||
try(ResponseBody body = response.body()) { | ||
if (body == null) { | ||
completeExceptionally(new TelegramApiException("Telegram api returned empty response")); | ||
} else { | ||
try { | ||
complete(method.deserializeResponse(body.string())); | ||
} catch (TelegramApiRequestException e) { | ||
completeExceptionally(e); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
26 changes: 4 additions & 22 deletions
26
...t/src/main/java/org/telegram/telegrambots/client/okhttp/OkHttpFutureDownloadCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,16 @@ | ||
package org.telegram.telegrambots.client.okhttp; | ||
|
||
import lombok.NonNull; | ||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import org.apache.commons.io.IOUtils; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
class OkHttpFutureDownloadCallback extends CompletableFuture<InputStream> implements Callback { | ||
@Override | ||
public void onFailure(@NonNull Call call, @NonNull IOException exception) { | ||
completeExceptionally(exception); | ||
} | ||
class OkHttpFutureDownloadCallback extends AbstractOkHttpFutureCallback<InputStream> { | ||
|
||
@Override | ||
public void onResponse(@NonNull Call call, @NonNull Response response) { | ||
try(ResponseBody body = response.body()) { | ||
if (body == null) { | ||
completeExceptionally(new TelegramApiException("Telegram api returned empty response")); | ||
} else { | ||
complete(new ByteArrayInputStream(IOUtils.toByteArray(body.byteStream()))); | ||
} | ||
} catch (Exception e) { | ||
completeExceptionally(e); | ||
} | ||
protected InputStream getResponseValue(ResponseBody body) throws IOException { | ||
return new ByteArrayInputStream(IOUtils.toByteArray(body.byteStream())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you sure you can do it? shouldn't response.body() be called only once? multiple calls will cause an exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and all future or past calls too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just returns a value of the
body
fieldThis
if
just a check that body exists and accessing it will not give us an NPEBut the
body
is aResponseBody
instance and is consumable and should be consumed onceAnd here I call
.string()
which consumes body and closes it, so this piece does not requiretry-with-resources
notation