/// Stable application error identities; independent of interface language. enum AppErrorCode { operationFailed, agentRestartHint, loadRecentProjectsFailed, loadProjectsFailed, projectAccessFailed, projectLocationNotWritable, projectLocationUnavailable, projectLocationCreateFailed, projectMissingDirectory, recentProjectSaveFailed, lastProjectSaveFailed, enterProjectName, projectSwitched, notLocalMedia, revealMediaFailed, openMatchingProject, openTimelineFailed, projectMismatch, openProjectFirst, localFilesOnly, foldersUnsupported, mediaImportCancelled, fileAccessFailed, registerMediaFailed, mediaNoLongerExists, mediaNoTracks, invalidMediaDuration, mediaInfoUnsupported, mediaInfoFailed, saveTimelineFailed, projectNameInvalid, projectLocationDefaultUnavailable, chooseProjectLocation, timelineMissing, timelineInUse, timelineHasBackgroundTasks, timelineRequired, projectSessionClosed, timelineSessionClosed, mediaReadFailed, mediaImportFailed, unsupportedMediaType, mediaNoVideoTracks, mediaDurationOutOfRange, resourceChecksumFailed, enterName, agentLocationUnavailable, agentLocationBusy, startBackgroundRemovalFailed, subtitleEngineUnavailable, timelineChanged, resourceDownloadHttpFailed, diagnostic, } /// An error remains structured until the presentation layer chooses its wording. final class AppFailure { const AppFailure(this.code, {this.diagnostic, this.httpStatusCode}); final AppErrorCode code; final String? diagnostic; final int? httpStatusCode; bool get requiresProjectLocation => switch (code) { AppErrorCode.projectLocationNotWritable || AppErrorCode.projectLocationUnavailable || AppErrorCode.projectLocationCreateFailed || AppErrorCode.projectLocationDefaultUnavailable || AppErrorCode.chooseProjectLocation => true, _ => false, }; @override bool operator ==(Object other) => other is AppFailure && code == other.code && diagnostic == other.diagnostic && httpStatusCode == other.httpStatusCode; @override int get hashCode => Object.hash(code, diagnostic, httpStatusCode); @override String toString() => diagnostic ?? code.name; } /// Keeps existing StateError catch boundaries while carrying a stable identity. final class AppException extends StateError { AppException(AppErrorCode code, {String? diagnostic, int? httpStatusCode}) : this.fromFailure( AppFailure( code, diagnostic: diagnostic, httpStatusCode: httpStatusCode, ), ); AppException.fromFailure(this.failure) : super(failure.toString()); final AppFailure failure; }