Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #3891 - Choose a value for error.reason independent of the …
Browse files Browse the repository at this point in the history
…underlying int value
  • Loading branch information
zugaldia committed Feb 25, 2016
1 parent 96883fc commit 1b12ee1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class OfflineRegionError {
public static final String REASON_CONNECTION = "REASON_CONNECTION";
public static final String REASON_OTHER = "REASON_OTHER";

private int reason;
private @ErrorReason String reason;

/**
/* An error message from the request handler, e.g. a server message or a system message
Expand All @@ -44,17 +44,7 @@ private OfflineRegionError() {
*/

public @ErrorReason String getReason() {
if (reason == 1) {
return REASON_SUCCESS;
} else if (reason == 2) {
return REASON_NOT_FOUND;
} else if (reason == 3) {
return REASON_SERVER;
} else if (reason == 4) {
return REASON_CONNECTION;
} else {
return REASON_OTHER;
}
return reason;
}

public String getMessage() {
Expand Down
21 changes: 19 additions & 2 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1950,9 +1950,26 @@ void JNICALL setOfflineRegionObserver(JNIEnv *env, jobject obj, jobject offlineR
JNIEnv* env2;
jboolean renderDetach = attach_jni_thread(theJVM, &env2, "Offline Thread");

// Choose a value for error.reason independent of the underlying int value
std::string errorReason;
if (error.reason == mbgl::Response::Error::Reason::Success) {
errorReason = "REASON_SUCCESS";
} else if (error.reason == mbgl::Response::Error::Reason::NotFound) {
errorReason = "REASON_NOT_FOUND";
} else if (error.reason == mbgl::Response::Error::Reason::Server) {
errorReason = "REASON_SERVER";
} else if (error.reason == mbgl::Response::Error::Reason::Connection) {
errorReason = "REASON_CONNECTION";
} else if (error.reason == mbgl::Response::Error::Reason::Other) {
errorReason = "REASON_OTHER";
} else {
mbgl::Log::Error(mbgl::Event::JNI, "Unsupported Response::Error::Reason value.");
return;
}

// Error object
jobject jerror = env2->NewObject(offlineRegionErrorClass, offlineRegionErrorConstructorId);
env2->SetIntField(jerror, offlineRegionErrorReasonId, (uint8_t)error.reason);
env2->SetObjectField(jerror, offlineRegionErrorReasonId, std_string_to_jstring(env2, errorReason));
env2->SetObjectField(jerror, offlineRegionErrorMessageId, std_string_to_jstring(env2, error.message));
env2->CallVoidMethod(observerCallback, offlineRegionObserveronErrorId, jerror);

Expand Down Expand Up @@ -2734,7 +2751,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
}

offlineRegionErrorReasonId = env->GetFieldID(offlineRegionErrorClass, "reason", "I");
offlineRegionErrorReasonId = env->GetFieldID(offlineRegionErrorClass, "reason", "Ljava/lang/String;");
if (offlineRegionErrorReasonId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
Expand Down

0 comments on commit 1b12ee1

Please sign in to comment.