Skip to content

Instantly share code, notes, and snippets.

@duyp
Last active May 8, 2020 10:55
Show Gist options
  • Save duyp/fe25a734cd3ee98ccb58ba39f7d4b502 to your computer and use it in GitHub Desktop.
Save duyp/fe25a734cd3ee98ccb58ba39f7d4b502 to your computer and use it in GitHub Desktop.
// domain
sealed class ErrorEntity {
sealed class ApiError: ErrorEntity() {
// .....
}
sealed class FileError: ErrorEntity() {
object NotFound: FileError()
object ReadError: FileError()
}
}
// domain
interface FileRepository {
fun loadFile(fileName: String): Result<String>
}
// data layer
class AndroidFileRepository : FileRepository {
override fun loadFile(fileName: String): Result<String> {
return try {
val fileContent = File(fileName).inputStream().readBytes().toString(Charsets.UTF_8)
Result.Success(fileContent)
} catch (e: FileNotFoundException) {
Result.Error(ErrorEntity.FileError.NotFound)
} catch (e: IOException) {
Result.Error(ErrorEntity.FileError.ReadError)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment