Skip to content

Commit

Permalink
Add detail error code for internal errors (#86)
Browse files Browse the repository at this point in the history
* add detail error code for ActivityNotfoundException and io exception error

* fix test case build error.

* upgrade minSdkVersion to 19; update version to 5.6.0
  • Loading branch information
plateaukao committed Sep 1, 2020
1 parent 93d5388 commit 15370f4
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 75 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Expand Up @@ -15,7 +15,7 @@ android {

defaultConfig {
applicationId "com.linecorp.linesdktest"
minSdkVersion 18
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand Down
6 changes: 3 additions & 3 deletions line-sdk/build.gradle
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

version = "5.5.1"
version = "5.6.0"

publish {
userOrg = 'line'
Expand All @@ -23,9 +23,9 @@ android {
publishNonDefault true

defaultConfig {
minSdkVersion 17
minSdkVersion 19
targetSdkVersion 29
versionCode 5_05_01
versionCode 5_06_00
versionName version

consumerProguardFiles 'consumer-proguard-rules.pro'
Expand Down
77 changes: 56 additions & 21 deletions line-sdk/src/main/java/com/linecorp/linesdk/LineApiError.java
Expand Up @@ -2,11 +2,12 @@

import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Objects;

import androidx.annotation.Nullable;

/**
* Represents an error that is thrown by the Social API.
Expand All @@ -24,35 +25,60 @@ public LineApiError[] newArray(int size) {
}
};

/**
* Represents detail error reasons.
*/
public enum ErrorCode {
/**
* Login intent can't be handled by system.
*/
LOGIN_ACTIVITY_NOT_FOUND,
/**
* Http response result can't be successfully parsed.
*/
HTTP_RESPONSE_PARSE_ERROR,
/**
* The default value when the detail error reason is not defined yet.
*/
NOT_DEFINED,
}

private static final int DEFAULT_HTTP_RESPONSE_CODE = -1;
public static final LineApiError DEFAULT = new LineApiError(
DEFAULT_HTTP_RESPONSE_CODE,
"" /* message */);
"" /* message */,
ErrorCode.NOT_DEFINED);

private final int httpResponseCode;
@Nullable
private final String message;

private final ErrorCode errorCode;

public LineApiError(@Nullable Exception e) {
this(DEFAULT_HTTP_RESPONSE_CODE, toString(e));
this(DEFAULT_HTTP_RESPONSE_CODE, toString(e), ErrorCode.NOT_DEFINED);
}

public LineApiError(@Nullable String message) {
this(DEFAULT_HTTP_RESPONSE_CODE, message);
this(DEFAULT_HTTP_RESPONSE_CODE, message, ErrorCode.NOT_DEFINED);
}

public LineApiError(int httpResponseCode, @Nullable Exception e) {
this(httpResponseCode, toString(e));
public static LineApiError createWithHttpResponseCode(int httpResponseCode, @Nullable String errorString) {
return new LineApiError(httpResponseCode, errorString, ErrorCode.NOT_DEFINED);
}

public LineApiError(int httpResponseCode, @Nullable String message) {
this.httpResponseCode = httpResponseCode;
this.message = message;
public static LineApiError createWithHttpResponseCode(int httpResponseCode, @Nullable Exception e) {
return LineApiError.createWithHttpResponseCode(httpResponseCode, toString(e));
}

private LineApiError(@NonNull Parcel in) {
httpResponseCode = in.readInt();
message = in.readString();
public LineApiError(@Nullable Exception e, ErrorCode errorCode) {
this(DEFAULT_HTTP_RESPONSE_CODE, toString(e), errorCode);
}

public LineApiError(int httpResponseCode, @Nullable String message, ErrorCode errorCode) {
this.httpResponseCode = httpResponseCode;
this.message = message;
this.errorCode = errorCode;
}

/**
Expand All @@ -62,6 +88,7 @@ private LineApiError(@NonNull Parcel in) {
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(httpResponseCode);
dest.writeString(message);
dest.writeInt(errorCode == null ? -1 : errorCode.ordinal());
}

/**
Expand Down Expand Up @@ -104,28 +131,35 @@ public String getMessage() {
return message;
}

/**
* @hide
*/
protected LineApiError(Parcel in) {
this.httpResponseCode = in.readInt();
this.message = in.readString();
int tmpErrorCode = in.readInt();
this.errorCode = tmpErrorCode == -1 ? null : ErrorCode.values()[tmpErrorCode];
}

/**
* @hide
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

if (!(o instanceof LineApiError)) return false;
LineApiError that = (LineApiError) o;

if (httpResponseCode != that.httpResponseCode) return false;
return message != null ? message.equals(that.message) : that.message == null;
return getHttpResponseCode() == that.getHttpResponseCode() &&
Objects.equals(getMessage(), that.getMessage()) &&
errorCode == that.errorCode;
}

/**
* @hide
*/
@Override
public int hashCode() {
int result = httpResponseCode;
result = 31 * result + (message != null ? message.hashCode() : 0);
return result;
return Objects.hash(getHttpResponseCode(), getMessage(), errorCode);
}

/**
Expand All @@ -136,6 +170,7 @@ public String toString() {
return "LineApiError{" +
"httpResponseCode=" + httpResponseCode +
", message='" + message + '\'' +
", errorCode='" + errorCode + '\'' +
'}';
}
}
Expand Up @@ -2,8 +2,6 @@

import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.linecorp.linesdk.LineApiError;
import com.linecorp.linesdk.LineApiResponse;
Expand All @@ -12,6 +10,11 @@
import com.linecorp.linesdk.LineIdToken;
import com.linecorp.linesdk.LineProfile;

import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import static com.linecorp.linesdk.utils.ParcelUtils.readEnum;
import static com.linecorp.linesdk.utils.ParcelUtils.writeEnum;

Expand Down Expand Up @@ -210,42 +213,32 @@ public LineApiError getErrorData() {
* @hide
*/
@Override
public boolean equals(final Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }

final LineLoginResult that = (LineLoginResult) o;

if (responseCode != that.responseCode) { return false; }
if (nonce != null ? !nonce.equals(that.nonce) : that.nonce != null) { return false; }
if (lineProfile != null ? !lineProfile.equals(that.lineProfile) : that.lineProfile != null) {
return false;
}
if (lineIdToken != null ? !lineIdToken.equals(that.lineIdToken) : that.lineIdToken != null) {
return false;
}
if (friendshipStatusChanged != null ? !friendshipStatusChanged.equals(that.friendshipStatusChanged) :
that.friendshipStatusChanged != null) { return false; }
if (lineCredential != null ? !lineCredential.equals(that.lineCredential) :
that.lineCredential != null) {
return false;
}
return errorData.equals(that.errorData);
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof LineLoginResult)) return false;
LineLoginResult that = (LineLoginResult) o;
return getResponseCode() == that.getResponseCode() &&
Objects.equals(getNonce(), that.getNonce()) &&
Objects.equals(getLineProfile(), that.getLineProfile()) &&
Objects.equals(getLineIdToken(), that.getLineIdToken()) &&
Objects.equals(getFriendshipStatusChanged(), that.getFriendshipStatusChanged()) &&
Objects.equals(getLineCredential(), that.getLineCredential()) &&
getErrorData().equals(that.getErrorData());
}

/**
* @hide
*/
@Override
public int hashCode() {
int result = responseCode.hashCode();
result = 31 * result + (nonce != null ? nonce.hashCode() : 0);
result = 31 * result + (lineProfile != null ? lineProfile.hashCode() : 0);
result = 31 * result + (lineIdToken != null ? lineIdToken.hashCode() : 0);
result = 31 * result + (friendshipStatusChanged != null ? friendshipStatusChanged.hashCode() : 0);
result = 31 * result + (lineCredential != null ? lineCredential.hashCode() : 0);
result = 31 * result + errorData.hashCode();
return result;
return Objects.hash(
getResponseCode(),
getNonce(),
getLineProfile(),
getLineIdToken(),
getFriendshipStatusChanged(),
getLineCredential(),
getErrorData());
}

/**
Expand Down
Expand Up @@ -8,6 +8,7 @@
import android.text.TextUtils;

import com.linecorp.linesdk.LineAccessToken;
import com.linecorp.linesdk.LineApiError;
import com.linecorp.linesdk.LineApiResponse;
import com.linecorp.linesdk.LineCredential;
import com.linecorp.linesdk.LineIdToken;
Expand Down Expand Up @@ -118,7 +119,7 @@ void startLineAuthentication() {
authenticationStatus.setSentRedirectUri(request.getRedirectUri());
} catch (ActivityNotFoundException e) {
authenticationStatus.authenticationIntentHandled();
activity.onAuthenticationFinished(LineLoginResult.internalError(e));
activity.onAuthenticationFinished(LineLoginResult.internalError(new LineApiError(e, LineApiError.ErrorCode.LOGIN_ACTIVITY_NOT_FOUND)));
}
}

Expand Down
Expand Up @@ -5,11 +5,6 @@
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;

import com.linecorp.android.security.TLSSocketFactory;
import com.linecorp.linesdk.BuildConfig;
import com.linecorp.linesdk.LineApiError;
Expand All @@ -32,6 +27,11 @@

import javax.net.ssl.HttpsURLConnection;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;

import static com.linecorp.linesdk.utils.UriUtils.appendQueryParams;

/**
Expand Down Expand Up @@ -347,7 +347,7 @@ private static <T> LineApiResponse<T> getChannelServiceResponse(
&& httpResponseCode != HttpURLConnection.HTTP_NO_CONTENT) {
return LineApiResponse.createAsError(
LineApiResponseCode.SERVER_ERROR,
new LineApiError(
LineApiError.createWithHttpResponseCode(
httpResponseCode,
errorResponseParser.getResponseData(inputStream)));
}
Expand All @@ -361,7 +361,8 @@ private static <T> LineApiResponse<T> getChannelServiceResponse(
// Evaluates response data parsing error as INTERNAL_ERROR
return LineApiResponse.createAsError(
LineApiResponseCode.INTERNAL_ERROR,
new LineApiError(httpResponseCode, e));
new LineApiError(e, LineApiError.ErrorCode.HTTP_RESPONSE_PARSE_ERROR)
);
}
}

Expand Down
Expand Up @@ -18,7 +18,7 @@
public class LineApiErrorTest {
@Test
public void testParcelable() {
LineApiError expected = new LineApiError(5, "testMessage");
LineApiError expected = LineApiError.createWithHttpResponseCode(5, "testMessage");

Parcel parcel = Parcel.obtain();
expected.writeToParcel(parcel, 0);
Expand All @@ -31,10 +31,10 @@ public void testParcelable() {

@Test
public void testEquals() {
LineApiError expected = new LineApiError(5, "testMessage");
LineApiError expected = LineApiError.createWithHttpResponseCode(5, "testMessage");

assertTrue(expected.equals(new LineApiError(5, "testMessage")));
assertFalse(expected.equals(new LineApiError(4, "testMessage")));
assertFalse(expected.equals(new LineApiError(5, "testMessage2")));
assertTrue(expected.equals(LineApiError.createWithHttpResponseCode(5, "testMessage")));
assertFalse(expected.equals(LineApiError.createWithHttpResponseCode(4, "testMessage")));
assertFalse(expected.equals(LineApiError.createWithHttpResponseCode(5, "testMessage2")));
}
}
@@ -1,8 +1,5 @@
package com.linecorp.linesdk.api.internal;

import androidx.annotation.NonNull;

import com.linecorp.linesdk.BuildConfig;
import com.linecorp.linesdk.LineApiError;
import com.linecorp.linesdk.LineApiResponse;
import com.linecorp.linesdk.LineApiResponseCode;
Expand All @@ -21,6 +18,8 @@

import java.net.HttpURLConnection;

import androidx.annotation.NonNull;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.never;
Expand All @@ -37,7 +36,7 @@ public class AutoRefreshLineApiClientProxyTest {
private static class Results {
private static final LineApiResponse<?> UNAUTHORIZED = LineApiResponse.createAsError(
LineApiResponseCode.SERVER_ERROR,
new LineApiError(HttpURLConnection.HTTP_UNAUTHORIZED, "errorMessage"));
LineApiError.createWithHttpResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED, "errorMessage"));
private static final LineApiResponse<?> NETWORK_ERROR = LineApiResponse.createAsError(
LineApiResponseCode.NETWORK_ERROR,
LineApiError.DEFAULT);
Expand Down
Expand Up @@ -3,8 +3,6 @@
import android.net.Uri;
import android.os.Parcel;

import androidx.annotation.NonNull;

import com.linecorp.linesdk.LineAccessToken;
import com.linecorp.linesdk.LineApiError;
import com.linecorp.linesdk.LineApiResponseCode;
Expand All @@ -23,6 +21,8 @@
import java.util.Arrays;
import java.util.Date;

import androidx.annotation.NonNull;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

Expand Down

0 comments on commit 15370f4

Please sign in to comment.