@ -41,6 +41,7 @@ import org.koitharu.kotatsu.parsers.exception.ParseException
import org.koitharu.kotatsu.parsers.exception.TooManyRequestExceptions
import org.koitharu.kotatsu.parsers.util.ifNullOrEmpty
import org.koitharu.kotatsu.scrobbling.common.domain.ScrobblerAuthRequiredException
import java.io.File
import java.net.ConnectException
import java.net.NoRouteToHostException
import java.net.SocketException
@ -52,6 +53,8 @@ private const val MSG_NO_SPACE_LEFT = "No space left on device"
private const val MSG _CONNECTION _RESET = " Connection reset "
private const val IMAGE _FORMAT _NOT _SUPPORTED = " Image format not supported "
private val FNFE _MESSAGE _REGEX = Regex ( " ^(/[^ \\ s:]+)?.+? \\ s([A-Z]{2,6})? \\ s.+ $ " )
fun Throwable . getDisplayMessage ( resources : Resources ) : String = getDisplayMessageOrNull ( resources )
?: resources . getString ( R . string . error _occurred )
@ -86,7 +89,7 @@ private fun Throwable.getDisplayMessageOrNull(resources: Resources): String? = w
is UnsupportedFileException -> resources . getString ( R . string . text _file _not _supported )
is BadBackupFormatException -> resources . getString ( R . string . unsupported _backup _message )
is FileNotFoundException -> resources. getString ( R . string . file _not _found )
is FileNotFoundException -> parseMessage( resources ) ?: message
is AccessDeniedException -> resources . getString ( R . string . no _access _to _file )
is EmptyHistoryException -> resources . getString ( R . string . history _is _empty )
is ProxyConfigException -> resources . getString ( R . string . invalid _proxy _configuration )
@ -225,3 +228,35 @@ fun Throwable.isWebViewUnavailable(): Boolean {
@Suppress ( " FunctionName " )
fun NoSpaceLeftException ( ) = IOException ( MSG _NO _SPACE _LEFT )
fun FileNotFoundException . getFile ( ) : File ? {
val groups = FNFE_MESSAGE_REGEX . matchEntire ( message ?: return null ) ?. groupValues ?: return null
return groups . getOrNull ( 1 ) ?. let { File ( it ) }
}
fun FileNotFoundException . parseMessage ( resources : Resources ) : String ? {
/ *
Examples :
/ storage / 0000 - 0000 / Android / media / d1f08350 - 0 c25 - 460 b - 8f 50 - 008 e49de3873 . jpg . tmp : open failed : EROFS ( Read - only file system )
/ storage / emulated / 0 / Android / data / org . koitharu . kotatsu / cache / pages / fe06e192fa371e55918980f7a24c91ea . jpg : open failed : ENOENT ( No such file or directory )
/ storage / 0000 - 0000 / Android / data / org . koitharu . kotatsu / files / manga / e57d3af4 - 216e-48 b2 - 8432 - 1541 d58eea1e . tmp ( I / O error )
* /
val groups = FNFE_MESSAGE_REGEX . matchEntire ( message ?: return null ) ?. groupValues ?: return null
val path = groups . getOrNull ( 1 )
val error = groups . getOrNull ( 2 )
val baseMessageIs = when ( error ) {
" EROFS " -> R . string . no _write _permission _to _file
" ENOENT " -> R . string . file _not _found
else -> return null
}
return if ( path . isNullOrEmpty ( ) ) {
resources . getString ( baseMessageIs )
} else {
resources . getString (
R . string . inline _preference _pattern ,
resources . getString ( baseMessageIs ) ,
path ,
)
}
}