Skip to content

Commit eb134b9

Browse files
authored
Add error handling and cursor validation.
1 parent 6133150 commit eb134b9

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

utils/src/main/java/ai/elimu/content_provider/utils/converter/CursorToVideoGsonConverter.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,50 @@ object CursorToVideoGsonConverter {
1010

1111
fun getVideoGson(cursor: Cursor): VideoGson {
1212
Log.i(TAG, "getVideoGson")
13-
1413
Log.i(TAG, "Arrays.toString(cursor.getColumnNames()): "
1514
+ cursor.columnNames.contentToString())
16-
15+
if (cursor.isBeforeFirst && !cursor.moveToFirst()) {
16+
throw IllegalArgumentException("Cursor must be positioned on a valid row")
17+
}
1718
val columnId = cursor.getColumnIndex("id")
19+
if (columnId == -1) {
20+
throw IllegalArgumentException("Column 'id' not found in cursor")
21+
}
1822
val id = cursor.getLong(columnId)
1923
Log.i(TAG, "id: $id")
2024

2125
val columnRevisionNumber = cursor.getColumnIndex("revisionNumber")
26+
if (columnRevisionNumber == -1) {
27+
throw IllegalArgumentException("Column 'revisionNumber' not found in cursor")
28+
}
2229
val revisionNumber = cursor.getInt(columnRevisionNumber)
2330
Log.i(TAG, "revisionNumber: $revisionNumber")
2431

2532
val columnTitle = cursor.getColumnIndex("title")
33+
if (columnTitle == -1) {
34+
throw IllegalArgumentException("Column 'title' not found in cursor")
35+
}
2636
val title = cursor.getString(columnTitle)
2737
Log.i(TAG, "title: \"$title\"")
2838

2939
val columnVideoFormat = cursor.getColumnIndex("videoFormat")
40+
if (columnVideoFormat == -1) {
41+
throw IllegalArgumentException("Column 'videoFormat' not found in cursor")
42+
}
3043
val videoFormatAsString = cursor.getString(columnVideoFormat)
3144
Log.i(TAG, "videoFormatAsString: $videoFormatAsString")
32-
val videoFormat = VideoFormat.valueOf(videoFormatAsString)
45+
val videoFormat = try {
46+
VideoFormat.valueOf(videoFormatAsString)
47+
} catch (e: IllegalArgumentException) {
48+
Log.e(TAG, "Invalid video format: $videoFormatAsString", e)
49+
throw IllegalArgumentException("Invalid video format: $videoFormatAsString", e)
50+
}
3351
Log.i(TAG, "videoFormat: $videoFormat")
34-
3552
val video = VideoGson()
3653
video.id = id
3754
video.revisionNumber = revisionNumber
3855
video.title = title
3956
video.videoFormat = videoFormat
40-
4157
return video
4258
}
4359
}

0 commit comments

Comments
 (0)