Skip to content

Commit

Permalink
Improve external handler error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
NatKarmios committed Mar 18, 2023
1 parent fc2a1df commit a5d3b96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.content.Intent
import com.orgzly.android.App
import com.orgzly.android.data.DataRepository
import com.orgzly.android.external.types.ExternalHandlerFailure
import com.orgzly.android.external.types.Response
import javax.inject.Inject

Expand Down Expand Up @@ -40,7 +39,7 @@ abstract class ExternalAccessActionHandler : ExternalIntentParser {
fullNameActions[intent.action!!]
?.let { it(intent, context) }
?.let { Response(true, if (it is Unit) null else it) }
} catch (e: ExternalHandlerFailure) {
} catch (e: Exception) {
Response(false, e.message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.orgzly.android.external.actionhandlers
import android.content.Intent
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonParseException
import com.google.gson.JsonParser
import com.orgzly.android.data.DataRepository
import com.orgzly.android.db.entity.NoteView
Expand All @@ -22,31 +21,26 @@ interface ExternalIntentParser {
val rawJson = getStringExtra("NOTE_PAYLOAD")
val json = try {
JsonParser.parseString(rawJson)
.let { if (it.isJsonObject) it.asJsonObject else null }
} catch (e: JsonParseException) {
null
}

return try {
json!!
NotePayload(
(json.getString("title") ?: title)!!,
json.getString("content"),
json.getString("state"),
json.getString("priority"),
json.getString("scheduled"),
json.getString("deadline"),
json.getString("closed"),
(json.getString("tags") ?: "")
.split(" +".toRegex())
.filter { it.isNotEmpty() },
OrgProperties().apply {
json["properties"]?.asMap?.forEach { (k, v) -> this[k] = v }
}
)
} catch (e: NullPointerException) {
throw ExternalHandlerFailure("invalid payload")
.let { if (it.isJsonObject) it.asJsonObject else null }!!
} catch (e: Exception) {
throw ExternalHandlerFailure("failed to parse json: ${e.message}\n$rawJson")
}
return NotePayload(
json.getString("title") ?: title
?: throw ExternalHandlerFailure("no title supplied!\n$rawJson"),
json.getString("content"),
json.getString("state"),
json.getString("priority"),
json.getString("scheduled"),
json.getString("deadline"),
json.getString("closed"),
(json.getString("tags") ?: "")
.split(" +".toRegex())
.filter { it.isNotEmpty() },
OrgProperties().apply {
json["properties"]?.asMap?.forEach { (k, v) -> this[k] = v }
}
)
}

private fun getNoteByQuery(rawQuery: String?): NoteView {
Expand Down

0 comments on commit a5d3b96

Please sign in to comment.