Skip to content

Commit

Permalink
Revert "Do not save site if there are errors"
Browse files Browse the repository at this point in the history
This reverts commit 3cea52a.
  • Loading branch information
IanVS committed Dec 10, 2024
1 parent 4a3e286 commit 7f78c8f
Showing 1 changed file with 7 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,7 @@ class MainActivity: FlutterActivity() {
return result.error("unhandled_error", err.message, null)
}

val validationResult = validateOrDeleteSite(siteDir);
if (validationResult is ValidationResult.FailureReason) {
return result.error("failure", validationResult.value, null)
} else if (validationResult is ValidationResult.BooleanResult && !validationResult.value) {
if (!validateOrDeleteSite(siteDir)) {
return result.error("failure", "Enrollment failed due to invalid config", null)
}

Expand Down Expand Up @@ -228,10 +225,7 @@ class MainActivity: FlutterActivity() {
return result.error("failure", err.toString(), null)
}

val validationResult = validateOrDeleteSite(siteDir);
if (validationResult is ValidationResult.FailureReason) {
return result.error("failure", validationResult.value, null)
} else if (validationResult is ValidationResult.BooleanResult && !validationResult.value) {
if (!validateOrDeleteSite(siteDir)) {
return result.error("failure", "Site config was incomplete, please review and try again", null)
}

Expand All @@ -240,28 +234,19 @@ class MainActivity: FlutterActivity() {
result.success(null)
}

sealed class ValidationResult {
data class FailureReason(val value: String) : ValidationResult()
data class BooleanResult(val value: Boolean) : ValidationResult()
}

private fun validateOrDeleteSite(siteDir: File): ValidationResult {
private fun validateOrDeleteSite(siteDir: File): Boolean {
try {
// Try to render a full site, if this fails the config was bad somehow
val site = Site(context, siteDir);
if (site.errors.isNotEmpty()) {
// Return the first error, so the user can at least know one thing to solve
return ValidationResult.FailureReason(site.errors[0])
}
Site(context, siteDir)
} catch(err: java.io.FileNotFoundException) {
Log.e(TAG, "Site not found at $siteDir")
return ValidationResult.BooleanResult(false)
return false
} catch(err: Exception) {
Log.e(TAG, "Deleting site at $siteDir due to error: $err")
siteDir.deleteRecursively()
return ValidationResult.BooleanResult(false)
return false
}
return ValidationResult.BooleanResult(true)
return true
}

private fun startSite(call: MethodCall, result: MethodChannel.Result) {
Expand Down

0 comments on commit 7f78c8f

Please sign in to comment.