From 49caa545e1d6227ecc952c89b6ddf40969a9bbaf Mon Sep 17 00:00:00 2001 From: develric Date: Wed, 9 Feb 2022 12:50:26 +0100 Subject: [PATCH 1/4] Adding XmlRpcErrorType enum and using it in BaseNetworkError type. --- .../android/fluxc/network/BaseRequest.java | 28 +++++++++++++++++++ .../fluxc/network/xmlrpc/XMLRPCRequest.java | 8 ++++++ .../network/xmlrpc/XMLRPCRequestBuilder.kt | 3 +- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/network/BaseRequest.java b/fluxc/src/main/java/org/wordpress/android/fluxc/network/BaseRequest.java index e9dfaa849f..a94fc4f314 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/network/BaseRequest.java +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/network/BaseRequest.java @@ -19,6 +19,8 @@ import com.android.volley.toolbox.HttpHeaderParser; import org.wordpress.android.fluxc.FluxCError; +import org.wordpress.android.fluxc.network.xmlrpc.XMLRPCRequest; +import org.wordpress.android.fluxc.network.xmlrpc.XMLRPCRequest.XmlRpcErrorType; import org.wordpress.android.fluxc.store.AccountStore.AuthenticateErrorPayload; import org.wordpress.android.fluxc.utils.ErrorUtils.OnUnexpectedError; import org.wordpress.android.util.AppLog; @@ -28,6 +30,9 @@ import javax.net.ssl.SSLHandshakeException; +import static org.wordpress.android.fluxc.network.xmlrpc.XMLRPCRequest.XmlRpcErrorType.METHOD_NOT_ALLOWED; +import static org.wordpress.android.fluxc.network.xmlrpc.XMLRPCRequest.XmlRpcErrorType.NOT_SET; + public abstract class BaseRequest extends Request { public static final int DEFAULT_REQUEST_TIMEOUT = 30000; public static final int UPLOAD_REQUEST_READ_TIMEOUT = 60000; @@ -62,6 +67,7 @@ public static class BaseNetworkError implements FluxCError { public GenericErrorType type; public String message; public VolleyError volleyError; + public XmlRpcErrorType xmlRpcErrorType = NOT_SET; public BaseNetworkError(@NonNull BaseNetworkError error) { this.message = error.message; @@ -80,14 +86,32 @@ public BaseNetworkError(@NonNull GenericErrorType error, @NonNull VolleyError vo this.type = error; this.volleyError = volleyError; } + public BaseNetworkError(@NonNull GenericErrorType error, + @NonNull VolleyError volleyError, + @NonNull XmlRpcErrorType xmlRpcErrorType) { + this.message = ""; + this.type = error; + this.volleyError = volleyError; + this.xmlRpcErrorType = xmlRpcErrorType; + } public BaseNetworkError(@NonNull VolleyError volleyError) { this.type = GenericErrorType.UNKNOWN; this.message = ""; this.volleyError = volleyError; } + public BaseNetworkError(@NonNull VolleyError volleyError, @NonNull XmlRpcErrorType xmlRpcErrorType) { + this.type = GenericErrorType.UNKNOWN; + this.message = ""; + this.volleyError = volleyError; + this.xmlRpcErrorType = xmlRpcErrorType; + } public BaseNetworkError(@NonNull GenericErrorType error) { this.type = error; } + public BaseNetworkError(@NonNull GenericErrorType error, @NonNull XmlRpcErrorType xmlRpcErrorType) { + this.type = error; + this.xmlRpcErrorType = xmlRpcErrorType; + } public boolean isGeneric() { return type != null; } @@ -309,6 +333,10 @@ private BaseNetworkError getBaseNetworkError(VolleyError volleyError) { switch (volleyError.networkResponse.statusCode) { case 404: return new BaseNetworkError(GenericErrorType.NOT_FOUND, volleyError.getMessage(), volleyError); + case 405: + return this instanceof XMLRPCRequest + ? new BaseNetworkError(volleyError, METHOD_NOT_ALLOWED) + : new BaseNetworkError(volleyError); case 451: return new BaseNetworkError(GenericErrorType.CENSORED, volleyError.getMessage(), volleyError); case 500: diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/network/xmlrpc/XMLRPCRequest.java b/fluxc/src/main/java/org/wordpress/android/fluxc/network/xmlrpc/XMLRPCRequest.java index de3ca005ef..07d5dcbbd0 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/network/xmlrpc/XMLRPCRequest.java +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/network/xmlrpc/XMLRPCRequest.java @@ -41,6 +41,13 @@ public class XMLRPCRequest extends BaseRequest { private final Object[] mParams; private final XmlSerializer mSerializer = Xml.newSerializer(); + public enum XmlRpcErrorType { + NOT_SET, + METHOD_NOT_ALLOWED, + UNABLE_TO_READ_SITE, + AUTH_REQUIRED + } + public XMLRPCRequest(String url, XMLRPC method, List params, Listener listener, BaseErrorListener errorListener) { super(Method.POST, url, errorListener); @@ -119,6 +126,7 @@ public BaseNetworkError deliverBaseNetworkError(@NonNull BaseNetworkError error) switch (error.type) { case HTTP_AUTH_ERROR: payload.error.type = AuthenticationErrorType.HTTP_AUTH_ERROR; + payload.error.xmlRpcErrorType = XmlRpcErrorType.AUTH_REQUIRED; break; case INVALID_SSL_CERTIFICATE: payload.error.type = AuthenticationErrorType.INVALID_SSL_CERTIFICATE; diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/network/xmlrpc/XMLRPCRequestBuilder.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/network/xmlrpc/XMLRPCRequestBuilder.kt index a2c3953ddd..c15668a1bb 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/network/xmlrpc/XMLRPCRequestBuilder.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/network/xmlrpc/XMLRPCRequestBuilder.kt @@ -5,6 +5,7 @@ import org.wordpress.android.fluxc.generated.endpoint.XMLRPC import org.wordpress.android.fluxc.network.BaseRequest import org.wordpress.android.fluxc.network.BaseRequest.BaseNetworkError import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.INVALID_RESPONSE +import org.wordpress.android.fluxc.network.xmlrpc.XMLRPCRequest.XmlRpcErrorType import org.wordpress.android.fluxc.network.xmlrpc.XMLRPCRequestBuilder.Response.Error import org.wordpress.android.fluxc.network.xmlrpc.XMLRPCRequestBuilder.Response.Success import javax.inject.Inject @@ -37,7 +38,7 @@ class XMLRPCRequestBuilder try { clazz.cast(obj)?.let { listener(it) } } catch (e: ClassCastException) { - errorListener.invoke(BaseNetworkError(INVALID_RESPONSE)) + errorListener.invoke(BaseNetworkError(INVALID_RESPONSE, XmlRpcErrorType.UNABLE_TO_READ_SITE)) } }, errorListener) } From be0d9e9e14243f51facc4c26272e9a7c91d4102c Mon Sep 17 00:00:00 2001 From: develric Date: Wed, 9 Feb 2022 12:50:54 +0100 Subject: [PATCH 2/4] Adding XmlRpcErrorType SiteError. --- .../android/fluxc/store/SiteStore.kt | 10 +++++++++- .../android/fluxc/utils/SiteErrorUtils.java | 20 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/SiteStore.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/store/SiteStore.kt index 52abe2b1bb..36bb40ad4e 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/SiteStore.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/SiteStore.kt @@ -99,6 +99,7 @@ import org.wordpress.android.fluxc.store.SiteStore.DomainAvailabilityErrorType.I import org.wordpress.android.fluxc.store.SiteStore.DomainSupportedStatesErrorType.INVALID_COUNTRY_CODE import org.wordpress.android.fluxc.store.SiteStore.ExportSiteErrorType.GENERIC_ERROR import org.wordpress.android.fluxc.store.SiteStore.PlansErrorType.NOT_AVAILABLE +import org.wordpress.android.fluxc.store.SiteStore.SelfHostedErrorType.NOT_SET import org.wordpress.android.fluxc.store.SiteStore.SiteErrorType.DUPLICATE_SITE import org.wordpress.android.fluxc.store.SiteStore.SiteErrorType.UNAUTHORIZED import org.wordpress.android.fluxc.store.SiteStore.SiteErrorType.UNKNOWN_SITE @@ -397,7 +398,8 @@ open class SiteStore data class SiteError @JvmOverloads constructor( @JvmField val type: SiteErrorType, - @JvmField val message: String? = null + @JvmField val message: String? = null, + @JvmField val selfHostedErrorType: SelfHostedErrorType = NOT_SET, ) : OnChangedError data class SiteEditorsError internal constructor( @@ -802,6 +804,12 @@ open class SiteStore GENERIC_ERROR } + enum class SelfHostedErrorType { + NOT_SET, + XML_RPC_SERVICES_DISABLED, + UNABLE_TO_READ_SITE + } + enum class DeleteSiteErrorType { INVALID_SITE, UNAUTHORIZED, // user don't have permission to delete AUTHORIZATION_REQUIRED, // missing access token diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/utils/SiteErrorUtils.java b/fluxc/src/main/java/org/wordpress/android/fluxc/utils/SiteErrorUtils.java index 7c61190134..b8d201667b 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/utils/SiteErrorUtils.java +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/utils/SiteErrorUtils.java @@ -1,6 +1,7 @@ package org.wordpress.android.fluxc.utils; import org.wordpress.android.fluxc.network.BaseRequest.BaseNetworkError; +import org.wordpress.android.fluxc.store.SiteStore.SelfHostedErrorType; import org.wordpress.android.fluxc.store.SiteStore.SiteError; import org.wordpress.android.fluxc.store.SiteStore.SiteErrorType; @@ -14,6 +15,23 @@ public static SiteError genericToSiteError(BaseNetworkError error) { break; } } - return new SiteError(errorType, error.message); + + SiteError siteError = new SiteError(errorType, error.message); + + switch (error.xmlRpcErrorType) { + case METHOD_NOT_ALLOWED: + siteError = new SiteError(errorType, error.message, SelfHostedErrorType.XML_RPC_SERVICES_DISABLED); + break; + case UNABLE_TO_READ_SITE: + siteError = new SiteError(errorType, error.message, SelfHostedErrorType.UNABLE_TO_READ_SITE); + break; + case AUTH_REQUIRED: + case NOT_SET: + default: + // Nothing to do + break; + } + + return siteError; } } From b9660ce1f80a20378e64d8c0c3bbb46704be1bc8 Mon Sep 17 00:00:00 2001 From: develric Date: Wed, 9 Feb 2022 12:51:32 +0100 Subject: [PATCH 3/4] Adding XmlRpcErrorType to AuthenticationError. --- .../android/fluxc/store/AccountStore.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/AccountStore.java b/fluxc/src/main/java/org/wordpress/android/fluxc/store/AccountStore.java index 98c26e172b..42a2976b42 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/AccountStore.java +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/AccountStore.java @@ -39,6 +39,7 @@ import org.wordpress.android.fluxc.network.rest.wpcom.auth.Authenticator; import org.wordpress.android.fluxc.network.rest.wpcom.auth.Authenticator.AuthEmailResponsePayload; import org.wordpress.android.fluxc.network.rest.wpcom.auth.Authenticator.Token; +import org.wordpress.android.fluxc.network.xmlrpc.XMLRPCRequest.XmlRpcErrorType; import org.wordpress.android.fluxc.persistence.AccountSqlUtils; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; @@ -49,6 +50,8 @@ import javax.inject.Inject; import javax.inject.Singleton; +import static org.wordpress.android.fluxc.network.xmlrpc.XMLRPCRequest.XmlRpcErrorType.NOT_SET; + /** * In-memory based and persisted in SQLite. */ @@ -74,6 +77,10 @@ public AuthenticateErrorPayload(@NonNull AuthenticationError error) { public AuthenticateErrorPayload(@NonNull AuthenticationErrorType errorType) { this.error = new AuthenticationError(errorType, ""); } + public AuthenticateErrorPayload(@NonNull AuthenticationErrorType errorType, + @NonNull XmlRpcErrorType xmlRpcErrorType) { + this.error = new AuthenticationError(errorType, "", xmlRpcErrorType); + } } public static class AuthEmailPayload extends Payload { @@ -446,10 +453,19 @@ public OnAuthEmailSent(boolean isSignup) { public static class AuthenticationError implements OnChangedError { public AuthenticationErrorType type; public String message; + public XmlRpcErrorType xmlRpcErrorType = NOT_SET; + public AuthenticationError(AuthenticationErrorType type, @NonNull String message) { this.type = type; this.message = message; } + public AuthenticationError(AuthenticationErrorType type, + @NonNull String message, + XmlRpcErrorType xmlRpcErrorType) { + this.type = type; + this.message = message; + this.xmlRpcErrorType = xmlRpcErrorType; + } } public static class OnSubscriptionsChanged extends OnChanged { From dfbdcdde73db70e3919425aca6d403fba918798b Mon Sep 17 00:00:00 2001 From: develric Date: Wed, 9 Feb 2022 12:55:55 +0100 Subject: [PATCH 4/4] Fixing ktlint. --- .../main/java/org/wordpress/android/fluxc/store/SiteStore.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/store/SiteStore.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/store/SiteStore.kt index 36bb40ad4e..f566793b7a 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/store/SiteStore.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/store/SiteStore.kt @@ -399,7 +399,7 @@ open class SiteStore data class SiteError @JvmOverloads constructor( @JvmField val type: SiteErrorType, @JvmField val message: String? = null, - @JvmField val selfHostedErrorType: SelfHostedErrorType = NOT_SET, + @JvmField val selfHostedErrorType: SelfHostedErrorType = NOT_SET ) : OnChangedError data class SiteEditorsError internal constructor(