-
-
Notifications
You must be signed in to change notification settings - Fork 45
Clean error code handling for Personal ID and logs it to firebase #3362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
815ee6e
Clean error code handling for Personal ID and logs it to firebase
shubham1g5 b5e000c
correct argument order
shubham1g5 671863b
abstract error code handling to a util class
shubham1g5 aeb7aa4
move network logging helpers to utils
shubham1g5 1a6483b
Adds error code logging for connect network helper
shubham1g5 694a562
better manage error stream
shubham1g5 f507cc3
change log for unknown urls
shubham1g5 e4b484b
pass errorBody instead of error codes as we are more probably to have…
shubham1g5 dcab599
Merge branch 'commcare_2.60' into logErrorCodes
shubham1g5 87507be
fix arguments
shubham1g5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| package org.commcare.connect.network | ||
|
|
||
| import org.commcare.util.LogTypes | ||
| import org.javarosa.core.io.StreamsUtil | ||
| import org.javarosa.core.services.Logger | ||
| import org.json.JSONObject | ||
| import retrofit2.HttpException | ||
| import retrofit2.Response | ||
| import java.io.IOException | ||
| import java.io.InputStream | ||
| import java.nio.charset.StandardCharsets | ||
|
|
||
| object NetworkUtils { | ||
|
|
||
| @JvmStatic | ||
| fun getErrorBody(stream: InputStream?): String { | ||
| try { | ||
| if (stream != null) { | ||
| val errorBytes = StreamsUtil.inputStreamToByteArray(stream) | ||
| return String(errorBytes, StandardCharsets.UTF_8) | ||
| } | ||
| } catch (e: Exception) { | ||
| Logger.exception("Error parsing error_code", e); | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| /** | ||
| * Extracts error_code and error_sub_code from a JSON error response body. | ||
| * If the stream is null or parsing fails, returns empty strings for both codes. | ||
| * | ||
| * @param stream InputStream of the error response body | ||
| * @return Pair of error_code and error_sub_code | ||
| */ | ||
| @JvmStatic | ||
| fun getErrorCodes(errorBody: String): Pair<String, String> { | ||
| var errorCode = "" | ||
| var errorSubCode = "" | ||
| try { | ||
| val json = JSONObject(errorBody) | ||
| errorCode = json.optString("error_code", ""); | ||
| errorSubCode = json.optString("error_sub_code", ""); | ||
| } catch (e: Exception) { | ||
| Logger.exception("Error parsing error_code", e); | ||
| } | ||
| return Pair(errorCode, errorSubCode) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun logFailedResponse( | ||
| responseMessage: String, | ||
| responseCode: Int, | ||
| endPoint: String, | ||
| errorBody: String | ||
| ) { | ||
| var message = "Response Message: $responseMessage | Response Code: $responseCode" | ||
| message += if (errorBody.isNotEmpty()) " | error: $errorBody" else "" | ||
| var errorMessage = when (responseCode) { | ||
| 400 -> "Bad Request: $message" | ||
| 401 -> "Unauthorized: $message" | ||
| 404 -> "Not Found: $message" | ||
| 500 -> "Server Error: $message" | ||
| else -> "API Error: $message" | ||
|
|
||
| } | ||
| errorMessage += " for url ${endPoint ?: "unknown url"}" | ||
|
|
||
| Logger.log( | ||
| LogTypes.TYPE_ERROR_SERVER_COMMS, | ||
| errorMessage | ||
| ) | ||
| Logger.exception(LogTypes.TYPE_ERROR_SERVER_COMMS, Throwable(errorMessage)) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun logNetworkError(t: Throwable, endPoint: String) { | ||
| val message = t.message | ||
|
|
||
| var errorMessage = when (t) { | ||
| is IOException -> "Network Error: $message" | ||
| is HttpException -> "HTTP Error: $message" | ||
| else -> "Unexpected Error: $message" | ||
| } | ||
|
|
||
| errorMessage += " for url ${endPoint ?: "url not found"}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shubham1g5 Can we make it |
||
| Logger.log( | ||
| LogTypes.TYPE_ERROR_SERVER_COMMS, | ||
| errorMessage | ||
| ) | ||
| Logger.exception(errorMessage, t) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why there is change in this method? I cannot see its being used further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the same interface used by
ConnectNetworkHelperThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's the same interface used by
ConnectNetworkHelper