Skip to content

Commit

Permalink
Merge pull request #431 from vimeo/invalid-token-error-definition
Browse files Browse the repository at this point in the history
Adding InvalidToken error
  • Loading branch information
anthonycr authored Jun 3, 2020
2 parents cbd1f17 + 20965e2 commit de40c8f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
14 changes: 14 additions & 0 deletions api-core/src/main/java/com/vimeo/networking2/VimeoResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ sealed class VimeoResponse<in T>(open val httpStatusCode: Int) {
override val httpStatusCode: Int
) : Error("API error: ${reason.errorCode ?: NA}", httpStatusCode)

/**
* The Vimeo API returned a fatal error that indicates that the token used to make the request is invalid. The
* token should be discarded and a new token should be acquired. Further requests should not be made with the
* token.
*
* @param reason Info on the error.
* @param httpStatusCode HTTP status code, [HTTP_NONE] if not applicable or if the error was
* created locally.
*/
data class InvalidToken(
val reason: ApiError?,
override val httpStatusCode: Int
) : Error("Unauthorized error: ${reason?.errorCode ?: NA}", httpStatusCode)

/**
* An exception was thrown when making the request, e.g. the internet connection failed and a
* [java.io.IOException] is thrown. This should only be used if a response was not received from the server. If
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.vimeo.networking.logging.ClientLogger;
import com.vimeo.networking2.AlbumPrivacy;
import com.vimeo.networking2.AlbumPrivacyUtils;
import com.vimeo.networking2.ApiConstants;
import com.vimeo.networking2.ApiError;
import com.vimeo.networking2.InvalidParameter;
import com.vimeo.networking2.VimeoResponse;
Expand Down Expand Up @@ -64,6 +63,12 @@
@SuppressWarnings("unused")
public final class VimeoNetworkUtil {

@NotNull
private static final String INVALID_TOKEN_VALUE = "Bearer error=\"invalid_token\"";

@NotNull
private static final String AUTHENTICATE_HEADER = "WWW-Authenticate";

@Nullable
private static Gson sGson;

Expand Down Expand Up @@ -210,11 +215,14 @@ public static <ResponseType_T> VimeoResponse.Error getErrorFromResponse(@NotNull
}
final VimeoResponse.Error vimeoError;
String errorBody = null;
final boolean isUnauthorizedError = INVALID_TOKEN_VALUE.equals(response.headers().get(AUTHENTICATE_HEADER));
if (response.errorBody() != null) {
try {
errorBody = response.errorBody().string();
final ApiError apiError = getMoshi().adapter(ApiError.class).fromJson(errorBody);
if (apiError != null) {
if (isUnauthorizedError) {
return new VimeoResponse.Error.InvalidToken(apiError, response.code());
} else if (apiError != null) {
return new VimeoResponse.Error.Api(apiError, response.code());
} else {
return new VimeoResponse.Error.Unknown(errorBody, response.code());
Expand All @@ -224,8 +232,14 @@ public static <ResponseType_T> VimeoResponse.Error getErrorFromResponse(@NotNull
if (errorBody == null) {
errorBody = "Unable to read error body";
}
return new VimeoResponse.Error.Unknown(errorBody, response.code());
if (isUnauthorizedError) {
return new VimeoResponse.Error.InvalidToken(null, response.code());
} else {
return new VimeoResponse.Error.Unknown(errorBody, response.code());
}
}
} else if (isUnauthorizedError) {
return new VimeoResponse.Error.InvalidToken(null, response.code());
} else {
return new VimeoResponse.Error.Unknown("Null error body", response.code());
}
Expand Down

0 comments on commit de40c8f

Please sign in to comment.