-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add error utils, fix stripe error message and charge data class
- Loading branch information
1 parent
27471d8
commit 83bba15
Showing
6 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
app/src/main/java/org/fossasia/openevent/general/order/Charge.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/src/main/java/org/fossasia/openevent/general/utils/Error.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.fossasia.openevent.general.utils | ||
|
||
import org.fossasia.openevent.general.utils.StringUtils.isEmpty | ||
|
||
class Error { | ||
|
||
var title: String? = null | ||
var detail: String? = null | ||
var pointer: String? = null | ||
|
||
override fun toString(): String { | ||
|
||
if (isEmpty(title)) { | ||
if (!isEmpty(detail)) { | ||
return (if (isEmpty(pointer)) { | ||
detail | ||
} else { | ||
"$detail - $pointer" | ||
}).toString() | ||
} | ||
} else { | ||
return if (isEmpty(pointer)) { | ||
"$title: $detail" | ||
} else { | ||
"$title: $detail - $pointer" | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
app/src/main/java/org/fossasia/openevent/general/utils/ErrorUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package org.fossasia.openevent.general.utils | ||
|
||
import timber.log.Timber | ||
import org.json.JSONObject | ||
import org.fossasia.openevent.general.utils.HttpErrors.BAD_REQUEST | ||
import org.fossasia.openevent.general.utils.HttpErrors.CONFLICT | ||
import org.fossasia.openevent.general.utils.HttpErrors.FORBIDDEN | ||
import org.fossasia.openevent.general.utils.HttpErrors.METHOD_NOT_ALLOWED | ||
import org.fossasia.openevent.general.utils.HttpErrors.NOT_FOUND | ||
import org.fossasia.openevent.general.utils.HttpErrors.REQUEST_TIMEOUT | ||
import org.fossasia.openevent.general.utils.HttpErrors.UNAUTHORIZED | ||
import org.fossasia.openevent.general.utils.HttpErrors.UNPROCESSABLE_ENTITY | ||
import org.fossasia.openevent.general.utils.StringUtils.isEmpty | ||
import retrofit2.HttpException | ||
|
||
const val ERRORS = "errors" | ||
const val SOURCE = "source" | ||
const val POINTER = "pointer" | ||
const val DETAIL = "detail" | ||
const val TITLE = "title" | ||
|
||
const val POINTER_LENGTH = 3 | ||
|
||
object ErrorUtils { | ||
|
||
fun getMessage(throwable: Throwable): Error { | ||
var error = Error() | ||
if (throwable is HttpException) { | ||
when (throwable.code()) { | ||
BAD_REQUEST, | ||
UNAUTHORIZED, | ||
FORBIDDEN, | ||
NOT_FOUND, | ||
METHOD_NOT_ALLOWED, | ||
REQUEST_TIMEOUT -> error = getErrorTitleAndDetails(throwable) | ||
UNPROCESSABLE_ENTITY, | ||
CONFLICT -> error = getErrorDetails(throwable) | ||
else -> error.detail = throwable.message | ||
} | ||
return error | ||
} | ||
|
||
if (isEmpty(error.detail)) | ||
error.detail = throwable.message | ||
return error | ||
} | ||
|
||
fun getErrorDetails(throwable: Throwable): Error { | ||
val error = Error() | ||
if (throwable is HttpException) { | ||
val responseBody = throwable.response()?.errorBody() | ||
|
||
try { | ||
val jsonObject = JSONObject(responseBody?.string()) | ||
val jsonArray = JSONObject(jsonObject.getJSONArray(ERRORS).get(0).toString()) | ||
val errorSource = JSONObject(jsonArray.get(SOURCE).toString()) | ||
|
||
try { | ||
val pointedField = getPointedField(errorSource.getString(POINTER)) | ||
if (pointedField == null) { | ||
error.detail = jsonArray.get(DETAIL).toString() | ||
} else { | ||
error.pointer = pointedField | ||
error.detail = jsonArray.get(DETAIL).toString().replace(".", "") | ||
} | ||
} catch (e: Exception) { | ||
error.detail = jsonArray.get(DETAIL).toString() | ||
} | ||
} catch (e: Exception) { | ||
Timber.e(e) | ||
} | ||
} | ||
return error | ||
} | ||
|
||
fun getErrorTitleAndDetails(throwable: Throwable): Error { | ||
val error = Error() | ||
if (throwable is HttpException) { | ||
val responseBody = throwable.response()?.errorBody() | ||
|
||
try { | ||
val jsonObject = JSONObject(responseBody?.string()) | ||
val jsonArray = JSONObject(jsonObject.getJSONArray(ERRORS).get(0).toString()) | ||
val errorSource = JSONObject(jsonArray.get(SOURCE).toString()) | ||
|
||
try { | ||
val pointedField = getPointedField(errorSource.getString(POINTER)) | ||
|
||
if (pointedField == null) { | ||
error.detail = jsonArray.get(DETAIL).toString() | ||
} else { | ||
error.pointer = pointedField | ||
error.detail = jsonArray.get(DETAIL).toString().replace(".", "") | ||
} | ||
error.title = jsonArray.get(TITLE).toString() | ||
} catch (e: Exception) { | ||
error.title = jsonArray.get(TITLE).toString() | ||
error.detail = jsonArray.get(DETAIL).toString() | ||
} | ||
} catch (e: Exception) { | ||
Timber.e(e) | ||
} | ||
} | ||
return error | ||
} | ||
|
||
fun getPointedField(pointerString: String?): String? { | ||
return if (pointerString == null || isEmpty(pointerString)) | ||
null | ||
else { | ||
val path = pointerString.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
if (path.size > POINTER_LENGTH) | ||
path[path.size - 1] | ||
else | ||
null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters