refactor(android): error messages come from the web's copy, one source for both apps (DRY B6)
release / web (push) Successful in 1m27s
release / go (push) Successful in 1m54s
release / govulncheck (push) Successful in 18s
release / android (push) Failing after 1m10s
release / integration (push) Successful in 4m9s
release / Build signed APK (push) Successful in 4m43s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
release / web (push) Successful in 1m27s
release / go (push) Successful in 1m54s
release / govulncheck (push) Successful in 18s
release / android (push) Failing after 1m10s
release / integration (push) Successful in 4m9s
release / Build signed APK (push) Successful in 4m43s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
Android's ErrorCopy was a hand-kept copy of web/src/lib/styles/ error-copy.json that had drifted: 14 of the web's codes were missing (a server shutting down, a bad setting value, the backup and duplicates codes), so those read "Something went wrong." on the phone, and only 2 of the web's 7 codes appended the server's specifics. Now app/build.gradle.kts copies error-copy.json and a new error-detail-codes.json (the list web/src/lib/api/errors.ts kept inline) into the app's Java resources, and ErrorCopy reads them. The three codes only Android had (auth_required, not_found, cleartext_public) move into the shared file. PasswordRules' too-short line is the shared password_too_short copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -100,6 +100,41 @@ android {
|
||||
}
|
||||
}
|
||||
|
||||
// The app's error messages are the web app's: web/src/lib/styles holds the
|
||||
// copy for each server error code (error-copy.json) and the codes whose
|
||||
// server message is appended (error-detail-codes.json). Both are copied in as
|
||||
// Java resources, which ErrorCopy reads, so the two clients say the same thing
|
||||
// and a new code is added once.
|
||||
abstract class CopySharedCopyTask : DefaultTask() {
|
||||
@get:InputFiles
|
||||
@get:PathSensitive(PathSensitivity.NAME_ONLY)
|
||||
abstract val sources: ConfigurableFileCollection
|
||||
|
||||
@get:OutputDirectory
|
||||
abstract val outputDir: DirectoryProperty
|
||||
|
||||
@TaskAction
|
||||
fun copy() {
|
||||
val out = outputDir.get().asFile
|
||||
out.deleteRecursively()
|
||||
out.mkdirs()
|
||||
sources.forEach { it.copyTo(File(out, it.name), overwrite = true) }
|
||||
}
|
||||
}
|
||||
|
||||
val copySharedCopy = tasks.register<CopySharedCopyTask>("copySharedCopy") {
|
||||
sources.from(
|
||||
rootProject.file("../web/src/lib/styles/error-copy.json"),
|
||||
rootProject.file("../web/src/lib/styles/error-detail-codes.json"),
|
||||
)
|
||||
}
|
||||
|
||||
androidComponents {
|
||||
onVariants { variant ->
|
||||
variant.sources.resources?.addGeneratedSourceDirectory(copySharedCopy, CopySharedCopyTask::outputDir)
|
||||
}
|
||||
}
|
||||
|
||||
// Kotlin 2.x: `kotlinOptions { ... }` inside `android { }` is gone; the
|
||||
// modern shape is the top-level `kotlin { compilerOptions { ... } }` block,
|
||||
// which works whether Kotlin comes from AGP 9's built-in path or an
|
||||
|
||||
@@ -44,13 +44,15 @@ object ErrorCopy {
|
||||
}
|
||||
|
||||
/**
|
||||
* Codes whose server message carries specifics an admin needs in
|
||||
* order to act — which directory, which uid — that fixed copy cannot say.
|
||||
* For these the message follows the copy. Kept to a named set on
|
||||
* purpose: most server messages are internal detail. Mirrors web's
|
||||
* errors.ts.
|
||||
* Codes whose server message carries specifics an admin needs in order
|
||||
* to act (which directory, which uid) that fixed copy cannot say. For
|
||||
* these the message follows the copy. Kept to a named set on purpose:
|
||||
* most server messages are internal detail. The set is the web app's
|
||||
* error-detail-codes.json.
|
||||
*/
|
||||
private val DETAIL_CODES = setOf("library_not_writable", "file_delete_failed")
|
||||
private val DETAIL_CODES: Set<String> by lazy {
|
||||
json.decodeFromString<List<String>>(sharedResource("error-detail-codes.json")).toSet()
|
||||
}
|
||||
|
||||
private fun fromHttp(e: HttpException): String {
|
||||
val body = bodyFromHttp(e)
|
||||
@@ -69,48 +71,17 @@ object ErrorCopy {
|
||||
.getOrNull() ?: Body()
|
||||
}
|
||||
|
||||
private val TABLE: Map<String, String> = mapOf(
|
||||
"unknown" to "Something went wrong.",
|
||||
"unauthenticated" to "Your session has ended. Please sign in again.",
|
||||
"auth_required" to "You need to sign in to do that.",
|
||||
"forbidden" to "You don't have permission to do that.",
|
||||
"not_authorized" to "You don't have permission to do that.",
|
||||
"invalid_credentials" to "Wrong username or password.",
|
||||
"rate_limited" to "Too many attempts. Wait a few minutes and try again.",
|
||||
"wrong_password" to "Current password is incorrect.",
|
||||
"password_too_short" to "Password must be at least 8 characters.",
|
||||
"username_invalid" to "Username must be 3–32 characters: letters, numbers, underscores, hyphens.",
|
||||
"username_taken" to "That username is already taken.",
|
||||
"email_invalid" to "Enter a valid email address.",
|
||||
"email_taken" to "That email is already in use.",
|
||||
"invalid_token" to "That link has expired or already been used. Request a new one.",
|
||||
"invite_invalid" to "That invite has expired or already been used.",
|
||||
"invite_required" to "An invite is required to register on this server.",
|
||||
"last_admin" to "Can't demote or delete the last admin.",
|
||||
"no_email_on_file" to "No email is associated with that account.",
|
||||
"not_configured" to "This integration isn't set up yet.",
|
||||
"validation" to "Some fields aren't valid. Check and try again.",
|
||||
"missing_fields" to "Required fields are missing.",
|
||||
"missing_query" to "Search needs at least one keyword.",
|
||||
"invalid_id" to "Invalid identifier.",
|
||||
"invalid_body" to "Couldn't read the request.",
|
||||
"bad_request" to "Invalid request.",
|
||||
"bad_body" to "Couldn't read the request.",
|
||||
"bad_kind" to "Invalid request type.",
|
||||
"bad_paging" to "Invalid page size or offset.",
|
||||
"bad_reason" to "Invalid quarantine reason.",
|
||||
"system_playlist_readonly" to "System playlists can't be edited directly.",
|
||||
"connection_refused" to "Couldn't reach the server. Check the URL and try again.",
|
||||
"cleartext_public" to
|
||||
"This server is on the internet, so its URL must start with https://. " +
|
||||
"Plain http:// only works on your home network.",
|
||||
"track_not_found" to "That track no longer exists.",
|
||||
"library_not_writable" to "The music library isn't writable by the server.",
|
||||
"file_delete_failed" to "The file couldn't be deleted.",
|
||||
"album_not_found" to "That album no longer exists.",
|
||||
"artist_not_found" to "That artist no longer exists.",
|
||||
"playlist_not_found" to "That playlist no longer exists.",
|
||||
"user_not_found" to "That user no longer exists.",
|
||||
"not_found" to "That no longer exists.",
|
||||
)
|
||||
/**
|
||||
* The copy for each error code: the web app's error-copy.json, copied
|
||||
* into the app's resources at build time (app/build.gradle.kts,
|
||||
* copySharedCopy), so both clients word every error the same way.
|
||||
*/
|
||||
private val TABLE: Map<String, String> by lazy {
|
||||
json.decodeFromString<Map<String, String>>(sharedResource("error-copy.json"))
|
||||
}
|
||||
|
||||
private fun sharedResource(name: String): String =
|
||||
checkNotNull(ErrorCopy::class.java.getResourceAsStream("/$name")) {
|
||||
"$name is missing from the app's resources (copySharedCopy in app/build.gradle.kts)"
|
||||
}.use { it.reader().readText() }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.fabledsword.minstrel.auth
|
||||
|
||||
import com.fabledsword.minstrel.api.ErrorCopy
|
||||
|
||||
/**
|
||||
* The rules every new-password field shares (Settings › Password, an admin
|
||||
* resetting someone's password), worded as the web app words them
|
||||
@@ -12,8 +14,8 @@ object PasswordRules {
|
||||
/** The hint under a new-password field. */
|
||||
const val HINT = "At least $MIN_LENGTH characters."
|
||||
|
||||
/** Said when a new password is shorter than [MIN_LENGTH]. */
|
||||
const val TOO_SHORT = "Password must be at least $MIN_LENGTH characters."
|
||||
/** Said when a new password is shorter than [MIN_LENGTH]: the server's own wording for it. */
|
||||
val TOO_SHORT: String get() = ErrorCopy.messageFor("password_too_short")
|
||||
|
||||
/** Said when a password and its confirmation differ. */
|
||||
const val DIFFER = "Passwords do not match."
|
||||
|
||||
@@ -57,4 +57,23 @@ class ErrorCopyTest {
|
||||
ErrorCopy.fromThrowable(IOException("refused")),
|
||||
)
|
||||
}
|
||||
|
||||
// The copy is the web app's error-copy.json, so codes only the web had
|
||||
// before (a server shutting down, a bad setting) now read the same here,
|
||||
// and an unknown code still falls back rather than showing nothing.
|
||||
@Test
|
||||
fun theCopyIsTheWebAppsWording() {
|
||||
assertEquals("The server is shutting down. Try again in a moment.", ErrorCopy.messageFor("shutting_down"))
|
||||
assertEquals("Password must be at least 8 characters.", ErrorCopy.messageFor("password_too_short"))
|
||||
assertEquals("Something went wrong.", ErrorCopy.messageFor("no_such_code"))
|
||||
}
|
||||
|
||||
// The detail codes are the web's list too: invalid_setting names the
|
||||
// field and its range, which the fixed copy can't.
|
||||
@Test
|
||||
fun theWebsDetailCodesAppendTheServerMessage() {
|
||||
val e = httpError(400, """{"error":{"code":"invalid_setting","message":"volume must be 0-100"}}""")
|
||||
|
||||
assertEquals("${ErrorCopy.messageFor("invalid_setting")} volume must be 0-100", ErrorCopy.fromThrowable(e))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { copyForCode } from './error-copy';
|
||||
import detailCodesJson from '../styles/error-detail-codes.json';
|
||||
|
||||
/**
|
||||
* Returns the `code` field from an unknown error value, or 'unknown'
|
||||
@@ -12,23 +13,14 @@ export function errCode(err: unknown): string {
|
||||
* Codes whose server message carries specifics an admin needs in order to
|
||||
* act — which directory, which uid — that fixed copy cannot say. For these the
|
||||
* message follows the copy. Kept to a named list on purpose: most
|
||||
* server messages are internal detail and must never reach a toast. Mirrored
|
||||
* in Android's ErrorCopy.
|
||||
* server messages are internal detail and must never reach a toast. The list
|
||||
* is styles/error-detail-codes.json, which Android's ErrorCopy reads too:
|
||||
* library_not_writable and file_delete_failed name the directory and uid,
|
||||
* invalid_setting the field and its range, invalid_public_url the address
|
||||
* shape wanted, bad_folders the folder and its fault, invalid_backup_setting
|
||||
* the setting and why, destination_failed the destination's own refusal.
|
||||
*/
|
||||
const DETAIL_CODES: ReadonlySet<string> = new Set([
|
||||
'library_not_writable',
|
||||
'file_delete_failed',
|
||||
// The server names the field and its range.
|
||||
'invalid_setting',
|
||||
// The server says what shape of address it wants.
|
||||
'invalid_public_url',
|
||||
// The server names the folder and what is wrong with it.
|
||||
'bad_folders',
|
||||
// The server says which backup setting and why.
|
||||
'invalid_backup_setting',
|
||||
// The destination's own answer: the bucket's refusal, or the missing folder.
|
||||
'destination_failed'
|
||||
]);
|
||||
const DETAIL_CODES: ReadonlySet<string> = new Set(detailCodesJson);
|
||||
|
||||
/**
|
||||
* Returns user-facing copy for an unknown error value. Looks up the
|
||||
|
||||
@@ -49,5 +49,8 @@
|
||||
"destination_failed": "Couldn't reach the backup destination.",
|
||||
"backup_running": "A backup is already running.",
|
||||
"backup_not_ready": "Set up where backups go first.",
|
||||
"shutting_down": "The server is shutting down. Try again in a moment."
|
||||
"shutting_down": "The server is shutting down. Try again in a moment.",
|
||||
"auth_required": "You need to sign in to do that.",
|
||||
"not_found": "That no longer exists.",
|
||||
"cleartext_public": "This server is on the internet, so its URL must start with https://. Plain http:// only works on your home network."
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
[
|
||||
"library_not_writable",
|
||||
"file_delete_failed",
|
||||
"invalid_setting",
|
||||
"invalid_public_url",
|
||||
"bad_folders",
|
||||
"invalid_backup_setting",
|
||||
"destination_failed"
|
||||
]
|
||||
Reference in New Issue
Block a user