Skip to content

Commit

Permalink
[futurepress#96] Android: Improves native error messages...
Browse files Browse the repository at this point in the history
including related server IDs into them
  • Loading branch information
birdofpreyru committed Jan 4, 2024
1 parent e012fcf commit 436819b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ package com.drpogodin.reactnativestaticserver
import android.util.Log
import com.facebook.react.bridge.Promise

enum class Errors(val message: String) {
ANOTHER_INSTANCE_IS_ACTIVE(
"Failed to launch, another server instance is active."),
FAIL_GET_LOCAL_IP_ADDRESS("Failed to get local IP adddress"),
FAIL_GET_OPEN_PORT("Failed to get an open port"),
INTERNAL_ERROR("Internal error"),
SERVER_CRASHED("Server crashed"),
STOP_FAILURE("Failed to gracefully shutdown the server");

class Errors(val name: String, val message: String) {
val error: Error
get() = Error(message)
val exception: Exception
Expand Down Expand Up @@ -45,5 +37,40 @@ enum class Errors(val message: String) {

companion object {
const val LOGTAG = "RN_STATIC_SERVER"

fun ANOTHER_INSTANCE_IS_ACTIVE(
activeServerId: Double,
failedToLaunchServerId: Double
): Errors {
return Errors(
"ANOTHER_INSTANCE_IS_ACTIVE",
"Failed to launch server #$failedToLaunchServerId, another server instance (#$activeServerId) is active.")
}

fun FAIL_GET_LOCAL_IP_ADDRESS(): Errors {
return Errors(
"FAIL_GET_LOCAL_IP_ADDRESS",
"Failed to get local IP adddress"
)
}

fun FAIL_GET_OPEN_PORT(): Errors {
return Errors(
"FAIL_GET_OPEN_PORT",
"Failed to get an open port"
)
}

fun INTERNAL_ERROR(serverId: Double): Errors {
return Errors("INTERNAL_ERROR", "Internal error (server #$serverId)")
}

fun SERVER_CRASHED(serverId: Double): Errors {
return Errors("SERVER_CRASHED", "Server #$serverId crashed")
}

fun STOP_FAILURE(serverId: Double): Errors {
return Errors("STOP_FAILURE", "Failed to gracefully shutdown the server #$serverId")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
}
promise.resolve("127.0.0.1")
} catch (e: Exception) {
Errors.FAIL_GET_LOCAL_IP_ADDRESS.reject(promise)
Errors.FAIL_GET_LOCAL_IP_ADDRESS().reject(promise)
}
}

Expand All @@ -78,17 +78,19 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
try {
sem.acquire()
} catch (e: Exception) {
Errors.INTERNAL_ERROR.log(e)
Errors.INTERNAL_ERROR(id).log(e)
.reject(promise, "Failed to acquire a semaphore")
return
}
if (server != null) {
Errors.ANOTHER_INSTANCE_IS_ACTIVE.log().reject(promise)

val activeServerId = server?.id;
if (activeServerId != null) {
Errors.ANOTHER_INSTANCE_IS_ACTIVE(activeServerId, id).log().reject(promise)
sem.release()
return
}
if (pendingPromise != null) {
Errors.INTERNAL_ERROR.log().reject(promise, "Unexpected pending promise")
Errors.INTERNAL_ERROR(id).log().reject(promise, "Unexpected pending promise")
sem.release()
return
}
Expand All @@ -106,7 +108,7 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
emitter.emit("RNStaticServer", event)
} else {
if (signal === Server.CRASHED) {
Errors.SERVER_CRASHED.reject(pendingPromise, details)
Errors.SERVER_CRASHED(id).reject(pendingPromise, details)
} else pendingPromise!!.resolve(details)
pendingPromise = null
sem.release()
Expand All @@ -124,7 +126,7 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
socket.close()
promise.resolve(port)
} catch (e: Exception) {
Errors.FAIL_GET_OPEN_PORT.log(e).reject(promise)
Errors.FAIL_GET_OPEN_PORT().log(e).reject(promise)
}
}

Expand All @@ -134,12 +136,12 @@ class ReactNativeStaticServerModule internal constructor(context: ReactApplicati
try {
sem.acquire()
} catch (e: Exception) {
Errors.INTERNAL_ERROR.log(e)
Errors.INTERNAL_ERROR(server!!.id).log(e)
.reject(promise, "Failed to acquire a semaphore")
return
}
if (pendingPromise != null) {
Errors.INTERNAL_ERROR
Errors.INTERNAL_ERROR(server!!.id)
.reject(pendingPromise, "Unexpected pending promise")
sem.release()
return
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/lighttpd/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.util.function.BiConsumer
* if any, has terminated or crashed before launching a new one!
*/
class Server(
var id: Double,
val id: Double,
var configPath: String,
var errlogPath: String,
private val signalConsumer: BiConsumer<String, String?>
Expand Down

0 comments on commit 436819b

Please sign in to comment.