Skip to content

Commit 59171f0

Browse files
committed
#22 - Add more error catching
1 parent b6aceca commit 59171f0

5 files changed

Lines changed: 52 additions & 16 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2024. dec4234
3+
* A standard open MIT license applies. Modififcation and usage permitted with credit. No warranties or express guarentees are given in any way.
4+
*
5+
* Github -> https://github.com/dec4234/JavaDestinyAPI
6+
*/
7+
8+
package net.dec4234.javadestinyapi.exceptions;
9+
10+
/**
11+
* When a token is missing or wrong, or some other pre-condition was not met that was needed to execute a request.
12+
* This is a generic error, check the associated error message for more information.
13+
*/
14+
public class InvalidConditionException extends APIException {
15+
16+
public InvalidConditionException(String message) {
17+
super(message);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2024. dec4234
3+
* A standard open MIT license applies. Modififcation and usage permitted with credit. No warranties or express guarentees are given in any way.
4+
*
5+
* Github -> https://github.com/dec4234/JavaDestinyAPI
6+
*/
7+
8+
package net.dec4234.javadestinyapi.exceptions;
9+
10+
/**
11+
* You either have insufficient permission to attempt this OAuth action, or you forgot to authorize yourself.
12+
* See {@link net.dec4234.javadestinyapi.utils.framework.OAuthFlow}
13+
*/
14+
public class OAuthUnauthorizedException extends APIException {
15+
16+
public OAuthUnauthorizedException(String message) {
17+
super(message);
18+
}
19+
}

src/main/java/net/dec4234/javadestinyapi/material/user/BungieUser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class BungieUser extends ContentFramework {
4444
private List<DestinyCharacter> destinyCharacters = null;
4545
private int playTime = -1, crossSaveOverride = -1, membershipType = -1;
4646
private boolean isPublic, isCrossSavePrimary, isOverridden = false;
47+
@Deprecated
4748
private int intendedPlatform = -2;
4849
private InventoryManager inventoryManager;
4950
private CollectionsManager collectionsManager;
@@ -443,6 +444,7 @@ public JsonObject getJE() throws APIException {
443444
return je;
444445
}
445446

447+
@Deprecated
446448
public void setIntendedPlatform(DestinyPlatform destinyPlatform) {
447449
intendedPlatform = destinyPlatform.getPlatformCode();
448450
je = null;

src/main/java/net/dec4234/javadestinyapi/utils/HttpUtils.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import com.google.gson.JsonSyntaxException;
1414
import net.dec4234.javadestinyapi.exceptions.APIException;
1515
import net.dec4234.javadestinyapi.exceptions.APIOfflineException;
16-
import net.dec4234.javadestinyapi.exceptions.AccessTokenInvalidException;
1716
import net.dec4234.javadestinyapi.exceptions.ConnectionException;
17+
import net.dec4234.javadestinyapi.exceptions.InvalidConditionException;
1818
import net.dec4234.javadestinyapi.exceptions.JsonParsingError;
1919
import net.dec4234.javadestinyapi.material.DestinyAPI;
2020
import net.dec4234.javadestinyapi.material.manifest.ManifestEntityTypes;
@@ -154,6 +154,10 @@ public String setTokenViaRefresh() throws APIException {
154154
return starter;
155155
})));
156156

157+
if(response.has("error_description") && response.get("error_description").getAsString().equals("ApplicationTokenKeyIdDoesNotExist")) {
158+
throw new InvalidConditionException("The refresh token is invalid, you likely need to generate new tokens");
159+
}
160+
157161
if(!response.has("access_token")) {
158162
return null;
159163
}
@@ -194,20 +198,6 @@ public void setTokenViaAuth(String oAuthCode) throws APIException {
194198
HttpUtils.bearerToken = accessToken;
195199
}
196200

197-
public boolean checkFor401(String input) throws APIException {
198-
if (input.contains("401 - Unauthorized")) {
199-
try {
200-
setTokenViaRefresh();
201-
throw new AccessTokenInvalidException("The access token used in this OAuth request was not accepted by the server \nI've already taken the liberty of getting a new access token for you :D");
202-
} catch (AccessTokenInvalidException e) {
203-
e.printStackTrace();
204-
return true;
205-
}
206-
}
207-
208-
return false;
209-
}
210-
211201
private JsonObject getJsonObject(String stringResponse) throws APIException {
212202
JsonObject jsonObject;
213203

@@ -221,6 +211,7 @@ private JsonObject getJsonObject(String stringResponse) throws APIException {
221211
if(jsonObject.has("ErrorCode") && jsonObject.get("ErrorCode").getAsInt() == 5) {
222212
throw new APIOfflineException(jsonObject.get("Message").getAsString());
223213
}
214+
224215
return jsonObject;
225216
}
226217

src/main/java/net/dec4234/javadestinyapi/utils/framework/OAuthFlow.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.sun.net.httpserver.*;
1212
import net.dec4234.javadestinyapi.exceptions.APIException;
13+
import net.dec4234.javadestinyapi.exceptions.InvalidConditionException;
1314
import net.dec4234.javadestinyapi.material.DestinyAPI;
1415
import net.dec4234.javadestinyapi.utils.StringUtils;
1516

@@ -49,7 +50,11 @@ public void initOAuthFlow(int port) throws APIException {
4950
* @param port The port to start the server on
5051
*/
5152
public void initOAuthFlowIfNeeded(int port) throws APIException {
52-
if(!DestinyAPI.hasOauthManager() || DestinyAPI.getAccessToken() == null || DestinyAPI.getHttpUtils().setTokenViaRefresh() == null) {
53+
try {
54+
if(!DestinyAPI.hasOauthManager() || DestinyAPI.getAccessToken() == null || DestinyAPI.getHttpUtils().setTokenViaRefresh() == null) {
55+
initOAuthFlow(port);
56+
}
57+
} catch (InvalidConditionException e) {
5358
initOAuthFlow(port);
5459
}
5560
}

0 commit comments

Comments
 (0)