Refine error messages in general

This commit is contained in:
Mygod
2019-02-23 23:00:14 +08:00
parent 664913a2ff
commit 317e2121c2
4 changed files with 13 additions and 8 deletions

View File

@@ -21,6 +21,7 @@ import be.mygod.vpnhotspot.databinding.ListitemInterfaceBinding
import be.mygod.vpnhotspot.net.TetherType import be.mygod.vpnhotspot.net.TetherType
import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
@@ -57,7 +58,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
if (started) manager.stop() else manager.start() if (started) manager.stop() else manager.start()
} catch (e: IOException) { } catch (e: IOException) {
Timber.w(e) Timber.w(e)
Toast.makeText(mainActivity, e.localizedMessage, Toast.LENGTH_LONG).show() Toast.makeText(mainActivity, e.readableMessage, Toast.LENGTH_LONG).show()
ManageBar.start(itemView.context) ManageBar.start(itemView.context)
} catch (e: InvocationTargetException) { } catch (e: InvocationTargetException) {
if (e.targetException !is SecurityException) Timber.w(e) if (e.targetException !is SecurityException) Timber.w(e)
@@ -65,7 +66,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
while (cause != null) { while (cause != null) {
cause = cause.cause cause = cause.cause
if (cause != null && cause !is InvocationTargetException) { if (cause != null && cause !is InvocationTargetException) {
Toast.makeText(mainActivity, cause.localizedMessage, Toast.LENGTH_LONG).show() Toast.makeText(mainActivity, cause.readableMessage, Toast.LENGTH_LONG).show()
ManageBar.start(itemView.context) ManageBar.start(itemView.context)
break break
} }
@@ -120,7 +121,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
} }
} catch (e: InvocationTargetException) { } catch (e: InvocationTargetException) {
if (Build.VERSION.SDK_INT !in 24..25 || e.cause !is SecurityException) Timber.w(e) else Timber.d(e) if (Build.VERSION.SDK_INT !in 24..25 || e.cause !is SecurityException) Timber.w(e) else Timber.d(e)
e.localizedMessage e.readableMessage
} }
} }
data.notifyChange() data.notifyChange()

View File

@@ -14,6 +14,7 @@ import be.mygod.vpnhotspot.TetheringService
import be.mygod.vpnhotspot.net.TetherType import be.mygod.vpnhotspot.net.TetherType
import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.util.stopAndUnbind import be.mygod.vpnhotspot.util.stopAndUnbind
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber import timber.log.Timber
@@ -74,14 +75,14 @@ sealed class TetheringTileService : TetherListeningTileService(), TetheringManag
func() func()
} catch (e: IOException) { } catch (e: IOException) {
Timber.w(e) Timber.w(e)
Toast.makeText(this, e.localizedMessage, Toast.LENGTH_LONG).show() Toast.makeText(this, e.readableMessage, Toast.LENGTH_LONG).show()
} catch (e: InvocationTargetException) { } catch (e: InvocationTargetException) {
if (e.targetException !is SecurityException) Timber.w(e) if (e.targetException !is SecurityException) Timber.w(e)
var cause: Throwable? = e var cause: Throwable? = e
while (cause != null) { while (cause != null) {
cause = cause.cause cause = cause.cause
if (cause != null && cause !is InvocationTargetException) { if (cause != null && cause !is InvocationTargetException) {
Toast.makeText(this, cause.localizedMessage, Toast.LENGTH_LONG).show() Toast.makeText(this, cause.readableMessage, Toast.LENGTH_LONG).show()
break break
} }
} }

View File

@@ -19,6 +19,8 @@ import java.net.InetAddress
import java.net.NetworkInterface import java.net.NetworkInterface
import java.net.SocketException import java.net.SocketException
val Throwable.readableMessage get() = localizedMessage ?: javaClass.name
/** /**
* This is a hack: we wrap longs around in 1 billion and such. Hopefully every language counts in base 10 and this works * This is a hack: we wrap longs around in 1 billion and such. Hopefully every language counts in base 10 and this works
* marvelously for everybody. * marvelously for everybody.

View File

@@ -9,6 +9,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent import androidx.lifecycle.OnLifecycleEvent
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.util.readableMessage
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.topjohnwu.superuser.NoShellException import com.topjohnwu.superuser.NoShellException
@@ -18,17 +19,17 @@ sealed class SmartSnackbar {
private var holder: View? = null private var holder: View? = null
fun make(@StringRes text: Int): SmartSnackbar = make(app.getText(text)) fun make(@StringRes text: Int): SmartSnackbar = make(app.getText(text))
fun make(text: CharSequence? = ""): SmartSnackbar { fun make(text: CharSequence = ""): SmartSnackbar {
val holder = holder val holder = holder
return if (holder == null) @SuppressLint("ShowToast") { return if (holder == null) @SuppressLint("ShowToast") {
if (Looper.myLooper() == null) Looper.prepare() if (Looper.myLooper() == null) Looper.prepare()
ToastWrapper(Toast.makeText(app, text, Toast.LENGTH_LONG)) ToastWrapper(Toast.makeText(app, text, Toast.LENGTH_LONG))
} else SnackbarWrapper(Snackbar.make(holder, text ?: null.toString(), Snackbar.LENGTH_LONG)) } else SnackbarWrapper(Snackbar.make(holder, text, Snackbar.LENGTH_LONG))
} }
fun make(e: Throwable) = make(when (e) { fun make(e: Throwable) = make(when (e) {
is NoShellException -> e.cause ?: e is NoShellException -> e.cause ?: e
else -> e else -> e
}.localizedMessage) }.readableMessage)
} }
class Register(lifecycle: Lifecycle, private val view: View) : LifecycleObserver { class Register(lifecycle: Lifecycle, private val view: View) : LifecycleObserver {