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; package org.nuclearfog.twidda.backend.api;
import androidx.annotation.NonNull;
/** /**
* Generic exception class used by {@link Connection} interface * Generic exception class used by {@link Connection} interface
* *
@ -144,4 +146,11 @@ public abstract class ConnectionException extends Exception {
* @return time in seconds * @return time in seconds
*/ */
public abstract int getTimeToWait(); 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 java.net.UnknownHostException;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.ResponseBody;
/** /**
* custom exception used by {@link Mastodon} class * 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"; private static final String MESSAGE_NOT_FOUND = "Record not found";
/**
* not defined error
*/
private static final int UNKNOWN_ERROR = -1;
/** private int errorCode = ERROR_NOT_DEFINED;
* 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 String errorMessage = ""; private String errorMessage = "";
/** /**
* *
*/ */
MastodonException(Exception e) { MastodonException(Exception e) {
super(e); super(e);
if (e instanceof UnknownHostException) { if (e instanceof JSONException) {
errorCode = ERROR_NETWORK; errorCode = JSON_FORMAT;
} else if (e instanceof JSONException) { } else if (e instanceof ConnectException || e instanceof UnknownHostException) {
errorCode = ERROR_JSON;
} else if (e instanceof ConnectException) {
errorCode = NO_CONNECTION; errorCode = NO_CONNECTION;
} else if (getCause() instanceof InterruptedException) { } else if (getCause() instanceof InterruptedException) {
errorCode = INTERRUPTED; errorCode = INTERRUPTED;
@ -68,19 +48,15 @@ public class MastodonException extends ConnectionException {
*/ */
MastodonException(Response response) { MastodonException(Response response) {
super(response.message()); super(response.message());
httpCode = response.code(); if (response.body() != null) {
ResponseBody body = response.body();
if (body != null) {
try { try {
String jsonStr = body.string(); String jsonStr = response.body().string();
if (!jsonStr.isEmpty()) { if (!jsonStr.isEmpty()) {
JSONObject json = new JSONObject(jsonStr); JSONObject json = new JSONObject(jsonStr);
String title = json.getString("error"); errorMessage = json.getString("error");
String descr = json.optString("error_description", ""); String descr = json.optString("error_description", "");
if (descr.isEmpty()) { if (!descr.isEmpty()) {
errorMessage = title; errorMessage += ": " + descr;
} else {
errorMessage = title + ": " + descr;
} }
} }
} catch (JSONException | IOException exception) { } 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 @Override
public int getErrorCode() { public int getErrorCode() {
if (errorCode != UNKNOWN_ERROR) return errorCode;
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;
}
} }
@Override @Override
public int getTimeToWait() { public int getTimeToWait() {
return 0; return 0; // not used
} }
@ -138,11 +114,4 @@ public class MastodonException extends ConnectionException {
public String getMessage() { public String getMessage() {
return errorMessage; return errorMessage;
} }
@NonNull
@Override
public String toString() {
return "error_code=" + errorCode + " message=\"" + errorMessage + "\"";
}
} }