exception error message fix

This commit is contained in:
nuclearfog 2023-09-09 20:31:18 +02:00
parent 5c453f7dff
commit c78df7a9c1
No known key found for this signature in database
GPG Key ID: 03488A185C476379
2 changed files with 41 additions and 63 deletions

View File

@ -1,5 +1,7 @@
package org.nuclearfog.twidda.backend.api;
import androidx.annotation.NonNull;
/**
* Generic exception class used by {@link Connection} interface
*
@ -144,4 +146,11 @@ public abstract class ConnectionException extends Exception {
* @return time in seconds
*/
public abstract int getTimeToWait();
@NonNull
@Override
public String toString() {
return "error_code=" + getErrorCode() + " message=\"" + getMessage() + "\"";
}
}

View File

@ -13,7 +13,6 @@ import java.net.ConnectException;
import java.net.UnknownHostException;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* custom exception used by {@link Mastodon} class
@ -26,37 +25,18 @@ public class MastodonException extends ConnectionException {
private static final String MESSAGE_NOT_FOUND = "Record not found";
/**
* not defined error
*/
private static final int UNKNOWN_ERROR = -1;
/**
* error caused by network connection
*/
private static final int ERROR_NETWORK = -2;
/**
* error caused by parsing json format
*/
private static final int ERROR_JSON = -3;
private int httpCode = 0;
private int errorCode = UNKNOWN_ERROR;
private int errorCode = ERROR_NOT_DEFINED;
private String errorMessage = "";
/**
*
*/
MastodonException(Exception e) {
super(e);
if (e instanceof UnknownHostException) {
errorCode = ERROR_NETWORK;
} else if (e instanceof JSONException) {
errorCode = ERROR_JSON;
} else if (e instanceof ConnectException) {
if (e instanceof JSONException) {
errorCode = JSON_FORMAT;
} else if (e instanceof ConnectException || e instanceof UnknownHostException) {
errorCode = NO_CONNECTION;
} else if (getCause() instanceof InterruptedException) {
errorCode = INTERRUPTED;
@ -68,19 +48,15 @@ public class MastodonException extends ConnectionException {
*/
MastodonException(Response response) {
super(response.message());
httpCode = response.code();
ResponseBody body = response.body();
if (body != null) {
if (response.body() != null) {
try {
String jsonStr = body.string();
String jsonStr = response.body().string();
if (!jsonStr.isEmpty()) {
JSONObject json = new JSONObject(jsonStr);
String title = json.getString("error");
errorMessage = json.getString("error");
String descr = json.optString("error_description", "");
if (descr.isEmpty()) {
errorMessage = title;
} else {
errorMessage = title + ": " + descr;
if (!descr.isEmpty()) {
errorMessage += ": " + descr;
}
}
} catch (JSONException | IOException exception) {
@ -89,6 +65,27 @@ public class MastodonException extends ConnectionException {
}
}
}
switch (response.code()) {
case 404:
if (errorMessage.startsWith(MESSAGE_NOT_FOUND)) {
errorCode = RESOURCE_NOT_FOUND;
break;
}
// fall through
case 401:
case 403:
errorCode = HTTP_FORBIDDEN;
break;
case 429:
errorCode = RATE_LIMIT_EX;
break;
case 503:
errorCode = SERVICE_UNAVAILABLE;
break;
}
}
/**
@ -102,34 +99,13 @@ public class MastodonException extends ConnectionException {
@Override
public int getErrorCode() {
if (errorCode != UNKNOWN_ERROR)
return errorCode;
switch (httpCode) {
case 404:
if (errorMessage.startsWith(MESSAGE_NOT_FOUND)) {
return RESOURCE_NOT_FOUND;
}
// fall through
case 401:
case 403:
return HTTP_FORBIDDEN;
case 429:
return RATE_LIMIT_EX;
case 503:
return SERVICE_UNAVAILABLE;
default:
return errorCode;
}
return errorCode;
}
@Override
public int getTimeToWait() {
return 0;
return 0; // not used
}
@ -138,11 +114,4 @@ public class MastodonException extends ConnectionException {
public String getMessage() {
return errorMessage;
}
@NonNull
@Override
public String toString() {
return "error_code=" + errorCode + " message=\"" + errorMessage + "\"";
}
}