Exception
sinew-exception / sinew_exception: a coded error model.
What it holds
- Built-in types for connectivity, HTTP statuses, parsing, storage and polling:
NoInternetConnectionException,ValidationException,UnauthorizedException,NotFoundException,ServerException,ParseException,StorageFullExceptionand more. Each has a short type code. processCallandprocessUseCase: guards that turn whatever a block throws into aResult.ExceptionHandler: one rule in a guard's list, "is this a timeout?" and "make this exception for this call".ExceptionLayer: where a failure came from, a short code in itscode:Rrepository,UCuse case,VMview model or notifier,UTutility, andSVfor an app service.CrashReporter: where bugs (unexpected and parse failures) are reported.LocalizeranddisplayMessage: the text a page shows.
Guard a call
- Kotlin
- Dart
// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Errors.kt#guard
suspend fun loadOrder(api: OrderApi): Result<Order> = processCall(
module = "ORD",
function = "load",
layer = ExceptionLayer.Repository,
handlers = listOf(
ExceptionHandler(
matches = { it is SerializationException },
transform = { ParseException("ORD", ExceptionLayer.Repository, "load", cause = it) },
),
),
) { api.order(7) }
A SinewException from a lower layer passes through unchanged. Anything else goes to the first matching handler, or becomes an UnexpectedException. Unexpected and parse failures are reported. On Kotlin, cancellation is never a failure: it propagates.
Show an error
- Kotlin
- Dart
// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Errors.kt#display
fun errorText(error: SinewException, localizer: Localizer?): String = error.displayMessage(localizer)
The text is server text unchanged, or a local message key resolved by the Localizer, falling back to English (never the raw key). The code always ends the text, for example You're offline. (ORD-R-load-NIC).
Report bugs
- Kotlin
- Dart
// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Errors.kt#crash-reporter
fun installReporter() {
CrashReporter.install(CrashReporter { error -> println("reported ${error.code}") })
}
Good to know
- The codes and the
sinew.key prefix are reserved. Your own exceptions use other codes and keys. - Validation field messages are shown beside their fields, without a code.
- Pick the layer of the guard you write: a repository uses
Repository, a use caseUseCase, and an app service guards its steps withExceptionLayer.Service(Dart:ExceptionLayer.service), so its bugs readMODULE-SV-FUNCTION-TYPE.