Skip to content

Commit 316bf2d

Browse files
committed
create generic response error message
Signed-off-by: alperozturk <[email protected]>
1 parent 884c8cb commit 316bf2d

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

app/src/main/java/it/niedermann/owncloud/notes/share/repository/ShareRepository.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import it.niedermann.owncloud.notes.share.model.toOCShare
2323
import it.niedermann.owncloud.notes.shared.model.ApiVersion
2424
import it.niedermann.owncloud.notes.shared.model.Capabilities
2525
import it.niedermann.owncloud.notes.shared.model.NotesSettings
26+
import it.niedermann.owncloud.notes.shared.util.extensions.getErrorMessage
2627
import org.json.JSONObject
2728
import java.text.SimpleDateFormat
2829
import java.util.Date
@@ -210,7 +211,7 @@ class ShareRepository(private val applicationContext: Context, private val accou
210211

211212
val date = Date(chosenExpDateInMills)
212213

213-
return SimpleDateFormat("yyyy-MM-dd", Locale.US).format(date)
214+
return SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(date)
214215
}
215216

216217
fun capabilities(): Capabilities = notesRepository.capabilities
@@ -354,26 +355,19 @@ class ShareRepository(private val applicationContext: Context, private val accou
354355
Log_OC.d(tag, "Response successful: $createShareResponse")
355356
true to applicationContext.getString(R.string.note_share_created)
356357
} else {
357-
val errorBody = response.errorBody()?.string()
358-
if (errorBody == null) {
358+
val errorMessage = response.getErrorMessage()
359+
if (errorMessage == null) {
359360
return false to defaultErrorMessage
360361
}
361-
Log_OC.d(tag, "Response failed: $errorBody")
362-
false to (extractErrorMessage(errorBody) ?: defaultErrorMessage)
362+
Log_OC.d(tag, "Response failed: $errorMessage")
363+
false to errorMessage
363364
}
364365
} catch (e: Exception) {
365366
Log_OC.d(tag, "Exception while creating share", e)
366367
false to defaultErrorMessage
367368
}
368369
}
369370

370-
private fun extractErrorMessage(json: String): String {
371-
val jsonObject = JSONObject(json)
372-
return jsonObject.getJSONObject("ocs")
373-
.getJSONObject("meta")
374-
.getString("message")
375-
}
376-
377371
fun updateSharePermission(
378372
shareId: Long,
379373
permissions: Int? = null,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package it.niedermann.owncloud.notes.shared.util.extensions
2+
3+
import it.niedermann.owncloud.notes.shared.model.OcsResponse
4+
import org.json.JSONObject
5+
import retrofit2.Response
6+
7+
fun <T> Response<OcsResponse<T>>.getErrorMessage(): String? {
8+
if (isSuccessful) {
9+
return null
10+
}
11+
12+
val errorBody = errorBody()?.string() ?: return null
13+
val jsonObject = JSONObject(errorBody)
14+
return jsonObject.getJSONObject("ocs")
15+
.getJSONObject("meta")
16+
.getString("message")
17+
}

0 commit comments

Comments
 (0)