-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #22 from snuhcs-course/test/authRepository-frontend
Test/AuthRepository frontend
- Loading branch information
Showing
8 changed files
with
508 additions
and
72 deletions.
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
50 changes: 50 additions & 0 deletions
50
frontend/app/src/main/java/com/example/speechbuddy/data/remote/models/ErrorResponseMapper.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,50 @@ | ||
package com.example.speechbuddy.data.remote.models | ||
|
||
import android.util.Log | ||
import com.example.speechbuddy.domain.models.ErrorResponse | ||
import okhttp3.ResponseBody | ||
import org.json.JSONArray | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
|
||
class ErrorResponseMapper { | ||
fun mapToDomainModel(response: ResponseBody): ErrorResponse { | ||
try { | ||
val errorJson = JSONObject(response.charStream().readText()).optJSONObject("error") | ||
|
||
if (errorJson != null) { | ||
val code = errorJson.optInt("code", -1) | ||
val messageJson = errorJson.opt("message") | ||
|
||
// if "message" in the ResponseBody is a list | ||
if (messageJson is JSONArray) { | ||
val firstMessage = messageJson.optJSONObject(0) | ||
if (firstMessage != null) { | ||
val keys = firstMessage.keys() | ||
if (keys.hasNext()) { | ||
val key = keys.next() | ||
val description = firstMessage.optJSONArray(key).optString(0) | ||
return ErrorResponse(code, key, description) | ||
} | ||
} | ||
} | ||
// if "message" in the ResponseBody is a single json | ||
else if (messageJson is JSONObject) { | ||
val keys = messageJson.keys() | ||
if (keys.hasNext()) { | ||
val key = keys.next() | ||
val description = messageJson.optString(key) | ||
return ErrorResponse(code, key, description) | ||
} | ||
} | ||
} | ||
|
||
// Return a default ErrorResponse if the responseBody structure doesn't match predefined cases | ||
return ErrorResponse() | ||
} catch (e: JSONException) { | ||
// Handle the JSON parsing error | ||
Log.e("ErrorResponseMapper", "JSON parsing error: ${e.message}") | ||
return ErrorResponse() // Return a default ErrorResponse in case of JSON parsing error | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
frontend/app/src/main/java/com/example/speechbuddy/domain/models/ErrorResponse.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,11 @@ | ||
package com.example.speechbuddy.domain.models | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
data class ErrorResponse( | ||
val code: Int = -1, | ||
val key: String = "Unknown Error With Error Response", | ||
val description: String = "Unknown Error With Error Response" | ||
) : Parcelable |
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
Oops, something went wrong.